3,881
社区成员
发帖
与我相关
我的任务
分享
matrix::matrix(int _row,int _column ,int *par) //含有参数的构造函数
{
setrow(_row);
setcolumn(_column);
p = new int(_row * _column);
for (int i = 0; i < row * column; i++)
{
p[i] = par[i];
}
}
matrix::matrix(matrix & m) //拷贝构造函数
{
row = m.row;
column = m.column;
p = new int (m.row * m.column);
for (int i = 0; i < row * column; i++)
{
p[i] = m.p[i];
}
}
ostream & operator<<( ostream & left, const matrix & right)
{
for (int i = 0; i < right.row; i++)
{
for (int j = 0; j < right.column; j++)
{
left << right.p[i * right. column + j] <<" ";
}
cout << endl;
}
return left;
}
istream & operator>>( istream & left, matrix & right)
{
for (int i = 0; i < right.row; i++)
{
for (int j = 0; j < right.column; j++)
{
left >> right.p[i * right. column + j];
}
cout << endl;
}
return left;
}
int main()
{
int arow = 0 ,brow = 0 ,acolumn = 0 ,bcolumn = 0;
int choice;
cout << "请输入矩阵a的行数:" <<endl;
cin >> arow;
cout << "请输入矩阵a的列数:" <<endl;
cin >> acolumn;
int *pa = new int[arow * acolumn];
cout << "请输入a的元素:" <<endl;
for (int i = 0; i < arow * acolumn ; i++)
{
cin >> pa[i];
}
cout << "请输入矩阵b的行数:" <<endl;
cin >> brow;
cout << "请输入矩阵b的列数:" <<endl;
cin >> bcolumn;
int *pb = new int[brow * bcolumn];
cout << "请输入b的元素:" <<endl;
for (int j = 0; j < brow * bcolumn ; j++)
{
cin >> pb[j];
}
//创建矩阵a、b
matrix a(arow ,acolumn ,pa);
matrix b(brow ,bcolumn ,pb);
cout << "******选择对矩阵的操作******" <<endl;
cout << "1.显示被创建的矩阵" <<endl;
cout << "2.判断矩阵a、b是否相等" <<endl;
cout << "3.矩阵加法" <<endl;
cout << "4.矩阵减法" <<endl;
cout << "5.矩阵乘法" <<endl;
cout << "6.退出" <<endl;
cin >> choice;
while (choice!=6)
{
switch (choice)
{
case 1:
cout << "矩阵a为:\n" << a << endl;
cout << "矩阵b为:\n" << b << endl;
break;
………
………