标签:
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 struct num{ 7 double r,e; 8 }; 9 num add(num a,num b){ 10 num c; 11 c.r=a.r+b.r; 12 c.e=a.e+b.e; 13 return c; 14 } 15 num sub(num a,num b){ 16 b.r=-b.r; 17 b.e=-b.e; 18 return add(a,b); 19 } 20 num mul(num a,num b){ 21 num c; 22 c.r=a.r*b.r-a.e*b.e; 23 c.e=a.r*b.e+a.e*b.r; 24 return c; 25 } 26 num div(num a,num b){ 27 num c; 28 b.e=-b.e; 29 c=mul(a,b); 30 double m=b.e*b.e+b.r*b.r; 31 c.r=c.r/m; 32 c.e=c.e/m; 33 return c; 34 } 35 int main(){ 36 //freopen("D:\\input.txt","r",stdin); 37 num a,b,c; 38 char cal; 39 while(cin>>a.r>>a.e>>cal>>b.r>>b.e){//scanf("%f %f",&a.r,&a.e),cin>>cal,scanf("%f %f",&cal,&b.r,&b.e)){ 40 //cout<<cal<<endl; 41 switch(cal){ 42 case ‘+‘:{ 43 c=add(a,b); 44 cout<<c.r; 45 if(c.e>0){ 46 cout<<"+"; 47 } 48 cout<<c.e<<"i"<<endl; 49 break; 50 } 51 case ‘-‘:{ 52 c=sub(a,b); 53 cout<<c.r; 54 if(c.e>0){ 55 cout<<"+"; 56 } 57 cout<<c.e<<"i"<<endl; 58 break; 59 } 60 case ‘*‘:{ 61 c=mul(a,b); 62 cout<<c.r; 63 if(c.e>0){ 64 cout<<"+"; 65 } 66 cout<<c.e<<"i"<<endl; 67 break; 68 } 69 case ‘/‘:{ 70 if(b.r==0&&b.e==0){ 71 cout<<"error"<<endl; 72 73 } 74 else{ 75 c=div(a,b); 76 cout<<c.r; 77 if(c.e>0){ 78 cout<<"+"; 79 } 80 cout<<c.e<<"i"<<endl; 81 } 82 break; 83 } 84 } 85 } 86 return 0; 87 }
标签:
原文地址:http://www.cnblogs.com/Deribs4/p/4393313.html