直接上代码
#include<iostream>
using namespace std;
int n;
class node
{
public:
int data[20][20];
int size;
node()
{
size=0;
}
node(const node & a)
{
this->size=a.size;
int limit=a.size;
for(int i=0;i<limit;i++)
for(int j=0;j<limit;j++)
(this->data)[i][j]=(a.data)[i][j];
}
friend ostream& operator <<(ostream&,const node&);
friend istream& operator >>(istream&,const node&);
friend node operator * (const node&,const node&);
};
node operator * (const node&a,const node&b)
{
node res;
res.size=n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int sum=0;
for(int t=0;t<n;t++)
sum+=(a.data)[i][t]*(b.data)[t][j];
(res.data)[i][j]=sum;
}
}
return res;
}
ostream& operator <<(ostream& o,const node& x)
{
int limit=x.size;
for(int i=0;i<limit;i++)
{
for(int j=0;j<limit;j++)
o<<(x.data)[i][j]<<' ';
cout<<endl;
}
return o;
}
istream& operator >>(istream& o,node& t)
{
cout<<"n=";
o>>n;
t.size=n;
cout<<"请输入n*n矩阵"<<endl;
for(int i=0;i<t.size;i++)
for(int j=0;j<t.size;j++)
{
int x;
o>>x;
(t.data)[i][j]=x;
}
return o;
}
int main()
{
int choice;
cout<<"Choice 1:a*b(a,b为矩阵)\nChoice 2:a^b(a为矩阵,b为int数)\n";
cin>>choice;
switch(choice)
{
case 1:
{
node a;
node b;
cin>>a;
cout<<"-------------"<<endl;
cin>>b;
cout<<"-------------"<<endl;
cout<<a*b;
break;
}
case 2:
{
node a;
int b;
cin>>a;
cout<<"-------------\nb=";
cin>>b;
node temp=a;
for(int i=1;i<=b;i++)
{
cout<<"a^"<<i<<endl;
cout<<temp;
temp=temp*a;
}
}
}
}