#include<iostream>
using namespace std;
int m, m1, n, n1;
class Rectangle
{
public:
Rectangle();
void input();
Rectangle operator+(Rectangle& A);
Rectangle operator-(Rectangle& B);
Rectangle operator*(Rectangle& C);
Rectangle operator=(Rectangle D);
void display();
private:
double x[10][10];
};
Rectangle::Rectangle()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
x[i][j] = 0;
}
}
}
void Rectangle::input()
{
if (n1 != 0)
{
for (int i = 0; i < m1; i++)
{
for (int j = 0; j < n1; j++)
{
cin >> x[i][j];
}
}
}
else
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin >> x[i][j];
}
}
}
Rectangle Rectangle::operator+(Rectangle& A)
{
Rectangle temp;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
temp.x[i][j] = x[i][j] + A.x[i][j];
return temp;
}
Rectangle Rectangle::operator-(Rectangle& B)
{
Rectangle temp;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
temp.x[i][j] = x[i][j] - B.x[i][j];
return temp;
}
Rectangle Rectangle::operator*(Rectangle& C)
{
Rectangle temp;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
for(int c=0;c<n1;c++)
temp.x[i][c] += x[i][j] * C.x[j][c];
return temp;
}
Rectangle Rectangle::operator=(Rectangle D)
{
if (n1 != 0)
n = n1;
for (int i = 0; i <m; i++)
{
for (int j = 0; j <n; j++)
x[i][j] =D.x[i][j];
}
return*this;
}
void Rectangle::display()
{
if (n1 != 0)
n = n1;
cout << "计算后的矩阵值是:" ;
for (int i = 0; i <m; i++)
{
cout << endl;
for (int j = 0; j <n; j++)
cout << x[i][j] << ' ';
}
cout << endl;
}
void main()
{
int y;
Rectangle c1, c2, c3;
cout << "两矩阵相加输入1;\n两矩阵相减输入2;\n两矩阵相乘输入3;\n:";
cin >> y;
if (y == 1)
{
cout << "请输入矩阵行数和列数:";
cin >> m >> n;
cout << "请输入第一个矩阵:" << endl;
c1.input();
cout << "请输入第两个矩阵:" << endl;
c2.input();
c3 = c1 + c2;
c3.display();
}
if (y == 2)
{
cout << "请输入矩阵行数和列数:";
cin >> m >> n;
cout << "请输入第一个矩阵:" << endl;
c1.input();
cout << "请输入第两个矩阵:" << endl;
c2.input();
c3 = c1 - c2;
c3.display();
}
if (y == 3)
{
cout << "请输入第一个矩阵行数和列数:";
cin >> m >> n;
cout << "请输入第一个矩阵:" << endl;
c1.input();
cout << "请输入第二个矩阵行数和列数:";
cin >> m1 >> n1;
cout << "请输入第两个矩阵:" << endl;
c2.input();
c3 = c1*c2;
c3.display();
}
}
用c++设计一个矩阵类,要求在矩阵类中重载“+”、“-”、“*”、“=”运算符。
最新推荐文章于 2023-04-26 20:27:53 发布