C++程序设计题, 帮忙谢谢 关于数组的

1.矩阵的乘法
2.求满足三角形a*a=b(b+c)且c>b的整数解

只会前面那题。

封装了一个矩阵类,运算部分只添了矩阵相乘,至于相加,转置等可自行添加。

代码如下:

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

class Matrix

{

public:

Matrix(){row = col = 0;a = NULL;} //默认构造函数

Matrix(int r,int c); //带参数的构造函数

Matrix(Matrix& m);   //拷贝构造函数

~Matrix();  //析构函数

void Set_Row_Col(int r,int c); //设置行列

void Destroy();     //销毁

void Clear();       //清零

void Input(istream& in);     //输入

void Output(ostream& out);   //输出

bool IsEmpty(){ return !a;}  //判断矩阵是否为空

friend Matrix operator *(Matrix& A,Matrix& B); //重载乘法运算符

private:

int row;

int col;

int **a;

};

Matrix::Matrix(int r,int c):row(r),col(c)

{

a = new int*[r];

for(int i = 0;i < r;i++)

a[i]  = new int[c];

}

Matrix::Matrix(Matrix& m)

{

Destroy();

int i,j;

row = m.row;

col = m.col;

a = new int*[m.row];

for(int i = 0;i < m.row;i++)

a[i]  = new int[m.col];

for(i = 0;i < row;i++)

for(j = 0;j < col;j++)

a[i][j] = m.a[i][j];

}

Matrix::~Matrix()

Destroy();

}

void Matrix::Set_Row_Col(int r,int c)

{

Destroy();

row = r;

col = c;

a = new int*[r];

for(int i = 0;i < r;i++)

a[i]  = new int[c];

}

void Matrix::Destroy()

{

if(a) return;  

for(int i = 0;i < row;i++)

delete[] a[i];

delete[] a;

}

void Matrix::Clear()

{

int i,j;

for(i = 0;i < row;i++)

for(j = 0;j < col;j++)

a[i][j] = 0;

}

void Matrix::Input(istream& in)

{

int i,j;

int r,c;

in>>r>>c;

if(r != row || c != col) //如果输入的行列大小与原先不符,重置

Set_Row_Col(r,c);

for(i = 0;i < row;i++)

for(j = 0;j < col;j++)

in>>a[i][j];

}

void Matrix::Output(ostream& out)

{

int i,j;

for(i = 0;i < row;i++)

{

for(j = 0;j < col;j++)

cout<<setiosflags(ios_base::left)<<setw(4)<<a[i][j];

out<<endl;

}

}

Matrix operator *(Matrix& A,Matrix& B)

{

if(A.col != B.row)  

return Matrix();  //如果不能作乘法,返回空矩阵

int i,j,k;

int r,c;

r = A.row;

c = B.col;

Matrix C(r,c);

C.Clear();

for(i = 0;i < r;i++)

for(j = 0;j < c;j++)

for(k = 0;k < A.col;k++)

C.a[i][j] += A.a[i][k]*B.a[k][j]; 

return C;

}

int main()

{

int r,c;

Matrix A,B,C;

ifstream ifile;

ifile.open("data.txt",ios_base::in);

if(!ifile) 

{

cout<<!"不能打开文件"<<endl;

return 1;

}

A.Input(ifile);

B.Input(ifile);

C = A * B;

if(!C.IsEmpty())

C.Output(cout);

return 0;

一般地,矩阵的输入通过读文件比较方便,因此输入输出函数用输入输出流作参数。

需要读写文件时,可通过传入ifstream/oftream 对象实现;

需要从屏幕输入输出,可传入cin/cout 对象。

测试数据如下时

3 3

7 8 9

4 5 6

1 2 3

3 5

7 8 9 10 11

4 5 6 7 8

1 2 3 4 5

输出到屏幕如图。

封装类是为了方便使用,如果不常用到矩阵,仅为实现算法,直接截出乘法实现的函数即可。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-03-13
//这是两个矩阵相乘的代码 至于第二个很简单的 你仔细分析下就会做了
//无非就是判断嘛 很简单的 希望你自己能做出来
#include<iostream>
using namespace std;
const int A[2][4]=
{
{1,0,3,-1},
{2,1,0,2}
};
const int B[4][3]=
{
{4,1,0},
{-1,1,3},
{2,0,1},
{1,3,4}
};
int main()
{
int C[2][3];
for(int i=0;i<2;i++)
{
for(int q=0;q<3;q++)
{
int k=0,sum=0;
for(int j=0;j<4;j++)
{
k=(A[i][j])*(B[j][q]);

sum+=k;
}
C[i][q]=sum;
cout<<C[i][q]<<" ";
}

cout<<endl;
}

system("PAUSE");
return 0;
}
第2个回答  2012-03-23
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

class Matrix
{
public:
Matrix(){row = col = 0;a = NULL;} //默认构造函数
Matrix(int r,int c); //带参数的构造函数
Matrix(Matrix& m); //拷贝构造函数
~Matrix(); //析构函数

void Set_Row_Col(int r,int c); //设置行列
void Destroy(); //销毁
void Clear(); //清零
void Input(istream& in); //输入
void Output(ostream& out); //输出
bool IsEmpty(){ return !a;} //判断矩阵是否为空

friend Matrix operator *(Matrix& A,Matrix& B); //重载乘法运算符

private:
int row;
int col;
int **a;
};

Matrix::Matrix(int r,int c):row(r),col(c)
{
a = new int*[r];
for(int i = 0;i < r;i++)
a[i] = new int[c];
}

Matrix::Matrix(Matrix& m)
{
Destroy();

int i,j;
row = m.row;
col = m.col;

a = new int*[m.row];
for(int i = 0;i < m.row;i++)
a[i] = new int[m.col];

for(i = 0;i < row;i++)
for(j = 0;j < col;j++)
a[i][j] = m.a[i][j];

}
Matrix::~Matrix()
{
Destroy();
}

void Matrix::Set_Row_Col(int r,int c)
{
Destroy();

row = r;
col = c;
a = new int*[r];
for(int i = 0;i < r;i++)
a[i] = new int[c];
}

void Matrix::Destroy()
{
if(a) return;
for(int i = 0;i < row;i++)
delete[] a[i];
delete[] a;
}

void Matrix::Clear()
{
int i,j;
for(i = 0;i < row;i++)
for(j = 0;j < col;j++)
a[i][j] = 0;
}

void Matrix::Input(istream& in)
{
int i,j;
int r,c;

in>>r>>c;
if(r != row || c != col) //如果输入的行列大小与原先不符,重置
Set_Row_Col(r,c);

for(i = 0;i < row;i++)
for(j = 0;j < col;j++)
in>>a[i][j];
}

void Matrix::Output(ostream& out)
{
int i,j;
for(i = 0;i < row;i++)
{
for(j = 0;j < col;j++)
cout<<setiosflags(ios_base::left)<<setw(4)<<a[i][j];
out<<endl;
}
}

Matrix operator *(Matrix& A,Matrix& B)
{
if(A.col != B.row)
return Matrix(); //如果不能作乘法,返回空矩阵

int i,j,k;
int r,c;

r = A.row;
c = B.col;

Matrix C(r,c);
C.Clear();

for(i = 0;i < r;i++)
for(j = 0;j < c;j++)
for(k = 0;k < A.col;k++)
C.a[i][j] += A.a[i][k]*B.a[k][j];
return C;
}

int main()
{
int r,c;
Matrix A,B,C;

ifstream ifile;
ifile.open("data.txt",ios_base::in);
if(!ifile)
{
cout<<!"不能打开文件"<<endl;
return 1;
}

A.Input(ifile);
B.Input(ifile);

C = A * B;
if(!C.IsEmpty())
C.Output(cout);

return 0;
}
相似回答