简单矩阵运算类_纯属练手,取mod的时候不怎么好用的说-_-

  1. #include <iostream>
  2. using namespace std;
  3. #define Error {cout<<"Error!"<<endl;return (*this);}
  4. class Mat
  5. {
  6. public:
  7. const static int MAX_MAT_SIZE=10;
  8. inline void init(int l,int c);
  9. inline Mat operator*(const Mat& b);
  10. inline void operator*=(const Mat& b);
  11. inline Mat operator+(const Mat& b);
  12. inline void operator+=(const Mat& b);
  13. inline Mat operator-(const Mat& b);
  14. inline void operator-=(const Mat& b);
  15. inline Mat operator^(int k);
  16. inline void show();
  17. void in(int l,int c);
  18. private:
  19.     int mat[MAX_MAT_SIZE][MAX_MAT_SIZE];
  20.     long long moudle;
  21.     int size_l,size_c;
  22. };
  23. inline void Mat::init(int l,int c)
  24. {
  25.     int i,j;
  26.     for(i=0;i<l;i++)for(j=0;j<c;j++)mat[i][j]=0;
  27.     size_l=l;size_c=c;
  28.     moudle=10000;
  29. }
  30. inline Mat Mat::operator *(const Mat& b)
  31. {
  32.     if(size_c!=b.size_l) Error
  33.     int i,j,k;Mat tmp;
  34.     tmp.init(size_l,b.size_c);
  35.     for(i=0;i<size_l;i++)
  36.         for(k=0;k<b.size_c;k++)
  37.             for(j=0;j<size_c;j++)
  38.             {tmp.mat[i][k]+=mat[i][j]*b.mat[j][k];if(tmp.mat[i][k]>=tmp.moudle)tmp.mat[i][k]%=tmp.moudle;}
  39.     return tmp;
  40. }
  41. inline void Mat::operator *=(const Mat &b)
  42. {
  43.     (*this)=(*this)*b;
  44. }
  45. inline Mat Mat::operator +(const Mat& b)
  46. {
  47.     if(size_l!=b.size_l||size_c!=b.size_c) Error
  48.     int i,j;Mat tmp;
  49.     tmp.init(size_l,b.size_c);
  50.     for(i=0;i<size_l;i++)
  51.         for(j=0;j<size_c;j++)
  52.         {tmp.mat[i][j]=mat[i][j]+b.mat[i][j];if(tmp.mat[i][j]>=tmp.moudle)tmp.mat[i][j]%=tmp.moudle;}
  53.     return tmp;
  54. }
  55. inline void Mat::operator +=(const Mat& b)
  56. {
  57.     (*this)=(*this)+b;
  58. }
  59. inline Mat Mat::operator -(const Mat& b)
  60. {
  61.     if(size_l!=b.size_l||size_c!=b.size_c) Error
  62.     int i,j;Mat tmp;
  63.     tmp.init(size_l,b.size_c);
  64.     for(i=0;i<size_l;i++)
  65.         for(j=0;j<size_c;j++)
  66.             tmp.mat[i][j]=mat[i][j]-b.mat[i][j];
  67.     return tmp;
  68. }
  69. inline void Mat::operator -=(const Mat& b)
  70. {
  71.     (*this)=(*this)-b;
  72. }
  73. inline Mat Mat::operator ^(int k)
  74. {
  75.     if(k==1) return (*this);
  76.     Mat tmp=(*this)*(*this);
  77.     if(k&1) return (tmp^(k/2))*(*this);
  78.     else return tmp^(k/2);
  79. }
  80. inline void Mat::show()
  81. {
  82.  int i,j;
  83.   for(i=0;i<size_l;cout<<mat[i++][j]<<endl)
  84.       for(j=0;j<size_c-1;j++) cout<<mat[i][j]<<' ';
  85. }
  86. void Mat::in(int l,int c)
  87. {
  88.     for(int i=0;i<l;i++)for(int j=0;j<c;j++)cin>>mat[i][j];
  89. }
  90. int main()
  91. {
  92.     int l,c,k;
  93.     Mat a,b;
  94.     while(cin>>l>>c)
  95.         {
  96.             a.init(l,c);
  97.             a.in(l,c);
  98.             cin>>k;
  99.             b=a^k;
  100.             b.show();
  101.         }
  102.     return 0;
  103. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值