这个是我们计算方法老师布置的作业,希望能对大家有用。
问题描述:
请自定义一个支持线性代数的矩阵概念的类Matrix。
功能介绍:
能实现线性代数矩阵的加、减、数乘、矩阵相乘。同时能实现计算方法里面的求矩阵的范数,但是不能求范数-2.
代码展示:
矩阵类的方法和属性的实现:
class Matrix
{
private:
int* p=NULL;
int rows, cols;
public:
Matrix(int r, int c);
Matrix()
{
rows = 0;
cols = 0;
}
Matrix(Matrix& b);
void input();
Matrix mulit(int n);//矩阵的数乘;
Matrix operator + (Matrix& c);
Matrix operator = (const Matrix& b);
Matrix operator-(Matrix& c);
Matrix operator*(Matrix& c);
Matrix operator~();
int MatrixHanFanShu1();
int MatrixHanFanShu2();
void show();
void show1();
~Matrix()
{
if (p) delete[] p;
}
};
构造函数:
Matrix::Matrix(int r, int c)
{
rows = r;
cols = c;
p = new int[r * c];
}
拷贝函数:
Matrix::Matrix(Matrix& b)
{
rows = b.rows;
cols = b.cols;
p = new int[b.rows * b.cols];
for (int i = 0; i < (b.rows * b.cols); i++)
{
p[i] = b.p[i];
}
}
输入和输出函数:
void Matrix::input()
{
for (int i = 0; i < rows * cols; i++) {
cout << "请输入矩阵的第" << i + 1 << "个数据:";
cin >> p[i];
}
}
void Matrix::show()
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << p[rows * i + j] << " ";
}
cout << endl;
}
}
转置矩阵的输出:
void Matrix::show1()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << p[i * cols + j] << " ";
}
cout << endl;
}
}
矩阵类方法的实现:
Matrix Matrix::operator+(Matrix& c)
{ //矩阵+运算,双目运算符
if (rows != c.rows && cols != c.cols)
{
cout << "你输入的矩阵不满足矩阵加法运算!" << endl;
}
else
{
Matrix temp(rows, cols);
for (int i = 0; i < (rows * cols); i++)
{
temp.p[i] = p[i] + c.p[i];
}
return temp;
}
}
Matrix Matrix::operator-(Matrix& c)
{
if (rows != c.rows && cols != c.cols)
{
cout << "你输入的矩阵不满足矩阵减法法运算!" << endl;
}
else
{
Matrix temp(rows, cols);
for (int i = 0; i < (rows * cols); i++)
{
temp.p[i] = p[i] - c.p[i];
}
return temp;
}
}
Matrix Matrix::operator*(Matrix& c)
{
Matrix temp(rows, c.cols);
if (cols != c.rows)
{
cout << "你输入的矩阵不满足矩阵的乘法运算!" << endl;
}
else
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < c.cols; j++)
{
temp.p[i * rows + j] = 0;
for (int k = 0; k < c.rows; k++)
{
temp.p[i * rows + j] += p[c.rows * i + k] * c.p[c.cols * k + j];
}
}
}
return temp;
}
}
int Matrix::MatrixHanFanShu1()
{
int i, j;
//int count = 0;
int n;
int data[1000] = { 0 };
for (i = 0; i < rows; i++)
{
int sum = 0;
for (j = 0; j < cols; j++)
{
n = p[i * rows + j];
sum += abs(n);
}
data[i] = sum;
}
sort(data, data + rows);
return data[rows-1];
}
int Matrix::MatrixHanFanShu2()
{
int i, j;
int n;
int data[1000] = { 0 };
for (i = 0; i < rows; i++)
{
int sum = 0;
for (j = 0; j < cols; j++)
{
n = p[j*rows+i];
sum += abs(n);
}
data[i] = sum;
}
sort(data, data + rows);
return data[rows - 1];
}
Matrix Matrix::operator~()
{
Matrix temp(cols, rows);
int i;
cout << "rows=" << rows << " " << "cols=" << cols << endl;
for (i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++)
{
temp.p[i * rows + j] = p[j * cols + i];
}
}
/*for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
cout << temp.p[i * rows + j] << " ";
}
}*/
return temp;
}
Matrix Matrix::mulit(int n)
{
Matrix temp(rows, cols);
for (int i = 0; i < rows * cols; i++) {
temp.p[i] = n * p[i];
}
return temp;
}
Matrix Matrix::operator=(const Matrix& b)
{ //复制运算符=重载,只能重载为成员函数
for (int i = 0; i < (b.rows * b.cols); i++)
{
p[i] = b.p[i];
}
return *this;
}
菜单函数:
void menu() {
cout << "\t****************************************************" << endl;
cout << "\t********* 矩阵得操作 *************" << endl;
cout << "\t********* 1:矩阵的加法 *************" << endl;
cout << "\t********* 2:矩阵的减法 *************" << endl;
cout << "\t********* 3:数与矩阵乘法 *************" << endl;
cout << "\t********* 4:矩阵与矩阵乘法 *************" << endl;
cout << "\t********* 5:矩阵行范数 *************" << endl;
cout << "\t********* 6:矩阵列范数 *************" << endl;
cout << "\t********* 7:矩阵转置 *************" << endl;
cout << "\t********* 0:退出程序 *************" << endl;
cout << "\t****************************************************" << endl;
}
主函数:
int main()
{
int x, y;
cout << "请输入A矩阵的行数x=";
cin >> x;
cout << "请输入A矩阵的列数y=";
cin >> y;
Matrix A(x, y);
cout << "输入矩阵A的值:\n";
A.input();
cout << endl;
int h, l;
cout << "请输入B矩阵的行数h=";
cin >> h;
cout << "请输入B矩阵的列数h=";
cin >> l;
Matrix B(h, l);
cout << "输入矩阵B的值:\n";
B.input();
Matrix C(x, y);
Matrix C1(x, l);//装矩阵A和矩阵B乘;
Matrix C2(y, x);//装矩阵A的转置;
Matrix C3(x, y);//装矩阵A的2-范数;
int select;
while (1)
{
menu();
cout << "请输入你的选择:";
cin >> select;
cout << endl << endl;
switch (select)
{
case 1:
C = A + B;
A.show();
cout << "+\n";
B.show();
cout << "=\n";
C.show();
system("pause");
break;
case 2:
C = A - B;
A.show();
cout << "-\n";
B.show();
cout << "=\n";
C.show();
system("pause");
break;
case 3:
int n;
cout << "请输入数乘值:";
cin >>n;
C = A.mulit(n);
cout << "矩阵A数乘的结果:\n";
C.show();
cout << "矩阵B数乘的结果:\n";
C = B.mulit(n);
C.show();
system("pause");
break;
case 4:
C1 = A * B;
cout << "矩阵A*B的结果:\n";
A.show();
cout << "*\n";
B.show();
cout << "=\n";
C1.show();
cout << endl;
cout << "矩阵B*A的结果:\n";
C1 = B * A;
B.show();
cout << "*\n";
A.show();
cout << "=\n";
C1.show();
system("pause");
break;
case 5:
int m;
m = A.MatrixHanFanShu1();
cout << "||A||的行范数=" << m << endl;
system("pause");
break;
case 6:
int max;
max = A.MatrixHanFanShu2();
cout << "||A||的列范数=" << max<< endl;
system("pause");
break;
case 7:
C2 = A.operator~();
cout << "矩阵A的转置如下:" << endl;
C2.show1();
C3 =C2*A;
cout << "矩阵C2*A的结果:\n";
C2.show();
cout << "*\n";
A.show();
cout << "=\n";
C3.show();
cout << endl;
system("pause");
break;
case 0:
exit(0);
}
}
return 0;
}
总结:
今天天气非常寒冷,今天在图书馆学习。慢慢努力吧!