加,减,乘,运算
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix operator+(Matrix &,Matrix &);
friend Matrix operator -(Matrix&,Matrix &);
friend Matrix operator* (Matrix&,Matrix &);
void input();
void display();
private:
int mat[2][2];
};
Matrix::Matrix()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
mat[i][j]=0;
}
}
}
Matrix operator+(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
}
}
return c;
}
Matrix operator-(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c.mat[i][j]=a.mat[i][j]-b.mat[i][j];
}
}
return c;
}
Matrix operator*(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c.mat[i][j]=a.mat[i][j]*b.mat[i][j];
}
}
return c;
}
void Matrix::input()
{
cout<<"请输入矩阵;"<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cin>>mat[i][j];
}
}
}
void Matrix::display()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
Matrix a,b,c;
a.input();
b.input();
cout<<"a的矩阵为:"<<endl;
a.display();
cout<<"b的矩阵为:"<<endl;
b.display();
c=a+b;
cout<<"a+b的矩阵为:"<<endl;
c.display();
c=a-b;
cout<<"a-b的矩阵为:"<<endl;
c.display();
cout<<"a*b的矩阵为:"<<endl;
c=a*b;
c.display();
return 0;
}