#include<cstdio> #include<cstdlib> #include<cstring> using namespace std; int main() { double a,b,c,d,n,m; char ch; scanf("%lf %lf %c %lf %lf",&a,&b,&ch,&c,&d); if(ch=='+'){ n=a+c;m=b+d; if(m==0)printf("%g\n",n); else if(m<0&&n!=0)printf("%g%gi\n",n,m); else if(m>0&&n!=0)printf("%g+%gi\n",n,m); else if(n==0&&m!=0)printf("%gi\n",m); } else if(ch=='-'){ n=a-c;m=b-d; if(m==0)printf("%g\n",n); else if(m<0&&n!=0)printf("%g%gi\n",n,m); else if(m>0&&n!=0)printf("%g+%gi\n",n,m); else if(n==0&&m!=0)printf("%gi\n",m); } else if(ch=='*'){ n=a*c+b*d*-1;m=a*d+b*c; if(m==0)printf("%g\n",n); else if(m<0&&n!=0)printf("%g%gi\n",n,m); else if(m>0&&n!=0)printf("%g+%gi\n",n,m); else if(n==0&&m!=0)printf("%gi\n",m); } else if(ch=='/'){ double ans=c*c+d*d; if(ans==0)printf("error\n"); else { d=d*-1; a=a/ans;b=b/ans; n=a*c+b*d*-1;m=a*d+b*c; if(m==0)printf("%g\n",n); else if(m<0&&n!=0)printf("%g%gi\n",n,m); else if(m>0&&n!=0)printf("%g+%gi\n",n,m); else if(n==0&&m!=0)printf("%gi\n",m); } } return 0; }
原文地址:http://blog.csdn.net/r1986799047/article/details/43853561