|
写这个题的原因就是我看见矩阵,就想起来了上学期写c语言的时候的煎熬(还没学矩阵)以及当时学的二维数组指针的痛苦,于是我决定写一下二维数组指针的解法:
这里附上大神写的方法,我就不多赘述了(https://blog.csdn.net/weixin_42426940/article/details/107571118)
下面是我的代码:
#include<bits/stdc++.h>
using namespace std;
class Matrix
{
private:
int **ppp;
int hang,lie;
bool flag;
public:
friend istream& operator>>(istream& in,Matrix & abc)
{
in>>abc.hang>>abc.lie;
abc.ppp = new int*[abc.hang];
for(int i=0;i<abc.hang;i++)
abc.ppp[i]=new int [abc.lie];
for(int i=0;i<abc.hang;i++)
{
for(int j=0;j<abc.lie;j++)
{
in>>abc.ppp[i][j];
}
}
return in;
}
friend ostream& operator<<(ostream& ou,Matrix & abc)
{
if(abc.flag == false){
ou<<"Error"<<endl;
return ou;
}
for(int i = 0;i<abc.hang;i++)
{
for(int j=0;j<abc.lie;j++)
{
if(j==0)
ou<<abc.ppp[i][j];
else ou<<" "<<abc.ppp[i][j];
}
ou<<endl;
}
// ou<<endl;
return ou;
}
Matrix operator+(Matrix &another)
{
Matrix abc;
abc.flag = true;
if(another.hang != hang||another.lie != lie)
{
abc.flag=false;
return abc;
}
abc.hang = hang;
abc.lie = lie;
abc.ppp = new int*[hang];
for(int i=0;i<abc.hang;i++)
abc.ppp[i] = new int[lie];
for(int i=0;i<hang;i++)
{
for(int j=0;j<lie;j++)
{
abc.ppp[i][j] = ppp[i][j]+another.ppp[i][j];
}
}
return abc;
}
Matrix operator*(Matrix &another)
{
Matrix abc;
abc.flag = true;
if(another.hang != lie)
{
abc.flag=false;
return abc;
}
abc.hang = hang;
abc.lie = another.lie;
abc.ppp = new int*[hang];
for(int i=0;i<abc.hang;i++)
abc.ppp[i] = new int[another.lie];
for(int i=0;i<hang;i++)
{
for(int j=0;j<another.lie;j++)
{
abc.ppp[i][j] =0;
for(int k=0;k<lie;k++)
abc.ppp[i][j]+=another.ppp[k][j]*ppp[i][k];
}
}
return abc;
}
};
记录当下,我的编程之路。