📄 3-3.cpp
字号:
#include <iostream>
using namespace std;
class Matrix
{
public: /*外部借口,公有成员函数*/
Matrix(int NewL,int NewR) /*构造函数*/
{
lines=NewL;
rows=NewR;
NewRec =new int*[lines];
for(int i=0;i<rows;i++)
{
*(NewRec+i)=new int[lines];
}
}
Matrix(Matrix &M) /*拷贝构造函数*/
{
lines=M.lines;
rows=M.rows;
NewRec=M.NewRec;
}
~Matrix(){} /*析构函数*/
/* {
for(int i=0;i<rows;i++)
{
delete *(NewRec+i);
}
delete NewRec;
}*/
void GetIn()
{
int i,j;
for (i=0;i<lines;i++)
for (j=0;j<rows;j++)
{
cin >> NewRec[i][j];
}
return;
}
void GetOut()
{
int i,j;
for (i=0;i<lines;i++)
{
for (j=0;j<rows;j++)
{
cout << NewRec[i][j] << "\t";
}
cout << endl;
}
return;
}
int **GetRec() {return NewRec;}
int GetL() {return lines;}
int GetR() {return rows;}
private: /*私有数据成员*/
int lines,rows;
int **NewRec;
};
void Plus (int **a,int **b,int **c,int l,int r)
{
int i,j;
for (i=0;i<l;i++)
{
for (j=0;j<r;j++)
{
*(*(c+i)+j)= *(*(a+i)+j)+(*(*(b+i)+j));
}
}
return;
}
void Minus (int **a,int **b,int **c,int l,int r)
{
int i,j;
for (i=0;i<l;i++)
{
for (j=0;j<r;j++)
{
*(*(c+i)+j)= *(*(a+i)+j)-(*(*(b+i)+j));
}
}
return;
}
int main()
{
int L1,R1,L2,R2;
cout << "请输入矩阵A1大小:" << endl;
cin >> L1 >> R1;
cout << "请输入矩阵A2大小:" << endl;
cin >> L2 >> R2;
Matrix A1(L1,R1);
Matrix A2(L2,R2);
Matrix A3(L1,R1);
cout << "请输入矩阵A1的数据:" << endl;
A1.GetIn();
cout << "矩阵A1的数据为:" << endl;
A1.GetOut();
cout << "请输入矩阵A2的数据:" << endl;
A2.GetIn();
cout << "矩阵A2的数据为:" << endl;
A2.GetOut();
Plus(A1.GetRec(),A2.GetRec(),A3.GetRec(),L1,R1);
cout << "矩阵A3的数据(相加)为:" << endl;
A3.GetOut();
Minus(A1.GetRec(),A2.GetRec(),A3.GetRec(),L1,R1);
cout << "矩阵A3的数据(相减)为:" << endl;
A3.GetOut();
///////////////////////////////////////////////////////////////////////
Matrix *Ptr1=new Matrix (L1,R1);
Matrix *Ptr2=new Matrix (L2,R2);
Matrix *Ptr3=new Matrix (L1,R1);
cout << "请输入矩阵A1的数据:" << endl;
Ptr1->GetIn ();
cout << "请输入矩阵A2的数据:" << endl;
Ptr2->GetIn ();
Plus (Ptr1->GetRec(),Ptr2->GetRec(),Ptr3->GetRec(),L1,R1);
cout << "矩阵A3的数据(相加)为:" << endl;
Ptr3->GetOut ();
Minus (Ptr1->GetRec(),Ptr2->GetRec(),Ptr3->GetRec(),L1,R1);
cout << "矩阵A3的数据(相减)为:" << endl;
Ptr3->GetOut ();
delete Ptr1;
delete Ptr2;
delete Ptr3;
/////////////////////////////////////////////////////////
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -