求高手修改一下下面这个程序实现矩阵的加法和减法。c++类
发布网友
发布时间:2022-04-12 00:25
我来回答
共1个回答
热心网友
时间:2022-04-12 01:55
/简单改了一下,运行环境VC2005;
//代码如下:
#include "stdafx.h"
#include<iostream>
#include<math.h>
using namespace std;
int i,j;
class matrix
{
private:
double a[2][2],b[2][2],c[2][2];
public:
matrix();
void add();
void decrease();
void printResult();
void printMtrix_A();
void printMtrix_B();
protected:
void printMatrix(const double mtx[2][2]);
};
matrix::matrix()
{
cout<<"plese enter matrix a:"<<endl;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>a[i][j];
cout<<"plese enter matrix b:"<<endl;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
cin>>b[i][j];
}
void matrix::add()
{
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]+b[i][j];
}
void matrix::decrease()
{
for(i=0;i<2;i++)
for(j=0;j<2;j++)
c[i][j]=a[i][j]-b[i][j];
}
void matrix::printMatrix(const double mtx[2][2])
{
printf("\n");
for (int i=0;i<2;++i)
{
for (int j=0;j<2;++j)
{
printf(" %.1f ",mtx[i][j]);
}
printf("\n");
}
}
void matrix::printMtrix_A()
{
printf("matrix A:");
printMatrix(a);
return;
}
void matrix::printMtrix_B()
{
printf("matrix B:");
printMatrix(b);
return;
}
void matrix::printResult()
{
printf("Result matrix c:");
printMatrix(c);
return;
}
int main()
{
matrix jie;
cout<<" ###########################################"<<endl;
cout<<" ##### 欢迎您使用矩阵函数包系统。 ######"<<endl;
cout<<" ##### 系统功能: ######"<<endl;
cout<<" ##### 计算两个矩阵相加,相减; ######"<<endl;
cout<<" ##### 数乘矩阵;矩阵转置;求逆 ######"<<endl;
cout<<" ##### 矩阵 ######"<<endl;
cout<<" ###########################################"<<endl;
cout<<"请选择您需要的运算,按回车键结束"<<endl;
cout<<"******************************************************************"<<endl;
cout<<"1,矩阵相加"<<endl;
cout<<"2,矩阵相减"<<endl;
cout<<"3,数乘矩阵"<<endl;
cout<<"4,矩阵相乘"<<endl;
cout<<"5,矩阵转置"<<endl;
cout<<"6,逆矩阵 "<<endl;
cout<<"7,退出 "<<endl;
cout<<"******************************************************************"<<endl;
int x;
do
{
cin>>x;
switch (x)
{
case 1:
{
jie.add ();
jie.printMtrix_A();
printf("加上");
jie.printMtrix_B();
jie.printResult();
break;
};
case 2:
{
jie.decrease();
jie.printMtrix_A();
printf("减去");
jie.printMtrix_B();
jie.printResult();
break;
};
default:
cout<<"\n********您选择错误,请重试.********"<<endl;
break;
}
} while (7!=x);
printf("#########再次感谢您使用本系统,合作愉快!############\n");
printf("******************************************************************\n");
return 0;
}