实现复数矩阵的加减乘除

时间:2025-02-14 12:19:10
/* user: sky48 language: c++ for dream for young */ #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; double eps; bool correct; struct comp //complex 结构体 { comp(double a=0,double b=0){ real=a, imag=b; } //complex 构造函数 comp operator + (comp& a){ return comp(real+a.real,imag+a.imag); } //complex 重载运算符 + comp operator - (comp& a){ return comp(real-a.real,imag-a.imag); } //complex 重载运算符 - comp operator * (comp& a) {return comp(real*a.real-imag*a.imag,real*a.imag+imag*a.real); } //complex 重载运算符 * void disp(); //complex 输出函数 double real,imag; }; class matrix //矩阵 类 { public: friend istream &operator >> (istream&,matrix&); //matrix 重载运算符 >> friend ostream &operator << (ostream&,matrix&); //matrix 重载运算符 << friend matrix &operator + (matrix&,matrix&); //matrix 重载运算符 + friend matrix &operator * (matrix&,matrix&); //matrix 重载运算符 * friend matrix &operator - (matrix&,matrix&); //matrix 重载运算符 - friend matrix &operator ! (matrix&); //matrix 重载运算符 ! void display(); //matrix 输出函数 void reads(); //matrix 读入函数 int n,m; private: comp s[25][25]; }; void gets(double &real,double &imag,char tmp[]){ // 复数读入 int p=0,f=1; int v=0,v1=0,v2=1; double v3; while(!isdigit(tmp[p])){if(tmp[p]=='-') f=-1; if(tmp[p]=='i'){ imag=f; return;} p++;} v=tmp[p++]-48; while( isdigit(tmp[p])) v=(v<<1)+(v<<3)+tmp[p]-48, p++; //读入优化 if(tmp[p]=='.'){ p++; while(isdigit(tmp[p])) v2=(v2<<1)+(v2<<3), v1=(v1<<1)+(v1<<3)+tmp[p]-48, p++; } v3=double(v1)/double(v2)+double(v); v3*=f; //兼容double类型读入优化 if(tmp[p]=='i'){ real=0; imag=v3; return ; } //纯虚数判断 if(tmp[p]!='+' && tmp[p]!='-'){ real=v3, imag=0; return ; } //复数判断 real=v3; f=v2=1, v=v1=v3=0; if(tmp[p]=='-') f=-1; p++; if(tmp[p]=='i'){ imag=f; return;} v=tmp[p++]-48; while( isdigit(tmp[p])) v=(v<<1)+(v<<3)+tmp[p]-48, p++; //虚数读入优化 if(tmp[p]=='.'){ p++; while(isdigit(tmp[p])) v2=(v2<<1)+(v2<<3), v1=(v1<<1)+(v1<<3)+tmp[p]-48, p++; } //兼容double虚数读入优化 v3=double(v1)/double(v2)+double(v); v3*=f; imag=v3; } istream& operator>>(istream& input,matrix& a){ cout<<"now input the matrix:(please input a+bi while a or b can be zero)\n"; char tmp[20]; rep(i,1,a.n) rep(j,1,a.m){ input>>tmp; gets(a.s[i][j].real,a.s[i][j].imag,tmp); //读入并存储在real和imag中 } return input; } ostream& operator<<(ostream& ouput,matrix& a){ cout<<"the row and line is "<<a.n<<" "<<a.m<<endl; //输出行和列 rep(i,1,a.n){ rep(j,1,a.m) a.s[i][j].disp(), cout<<" "; cout<<endl; } //输出矩阵 } matrix &operator + (matrix &a,matrix &b){ //matrix 重载运算符 + a.n=max(b.n,a.n), a.m=max(b.m,a.m); rep(i,1,a.n) rep(j,1,a.m) a.s[i][j]=a.s[i][j]+b.s[i][j]; return a; } matrix &operator - (matrix &a,matrix &b){ //matrix 重载运算符 - a.n=max(b.n,a.n), a.m=max(b.m,a.m); rep(i,1,a.n) rep(j,1,a.m) a.s[i][j]=a.s[i][j]-b.s[i][j]; return a; } matrix &operator * (matrix &a,matrix &b){ //matrix 重载运算符 * if(a.m!=b.n) correct=1, cout<<"the multiplication is error."<<endl; int t=b.m; comp f[25][25]; rep(i,1,a.n) rep(j,1,b.m){ f[i][j]=comp(0,0); rep(k,1,a.m) f[i][j]=a.s[i][k]*b.s[k][j]+f[i][j]; } a.m=b.m; rep(i,1,a.n) rep(j,1,a.m) a.s[i][j]=f[i][j]; return a; } matrix &operator ! (matrix &a){ //matrix 重载运算符 ! swap(a.n,a.m); comp tmp(0,0); int t=max(a.n,a.m); rep(i,1,t) rep(j,1,i-1) tmp=a.s[i][j], a.s[i][j]=a.s[j][i], a.s[j][i]=tmp, a.s[i][j].imag*=-1; return a; } void matrix::display(){ //matrix 输出函数 rep(i,1,n) rep(j,1,m) s[i][j].disp(); } void matrix::reads(){ //matrix 读入行和列 cout<<"please input the row and line of matrix:"; cin>>n>>m; } void comp::disp(){ //matrix 输出函数 if(fabs(imag)<eps) printf("%12.2lf",real); //如果为实数 else { if(fabs(real)<eps) printf("%11.2lfi",imag); //如果为纯虚数 else printf("%5.2lf%+06.2lfi",real,imag); //如果为复数 } } int main () { // freopen("","r",stdin); eps=0.00001; matrix mat1,mat2; mat1.reads(); cin>>mat1; //读入行列和矩阵 mat2.reads(); cin>>mat2; char typ; cout<<"what operator you wanna do:"; cin>>typ; //读入操作的类型 if(typ=='+') mat1=mat1+mat2; if(typ=='-') mat1=mat1-mat2; if(typ=='*') mat1=mat1*mat2; if(typ=='!') mat1=!mat1; if(!correct) cout<<mat1; return 0; }