Link:http://blog.csdn.net/wr132/article/details/43577347
解题思想:注意要用Double ,不能用int 。复数运算公式:点击打开链接
AC code:
#include<iostream> using namespace std; void add(double a,double b,double c,double d) { cout<<a+c; if(b+d>=0) cout<<"+"; cout<<b+d; cout<<'i'<<endl; } void sub(double a,double b,double c,double d) { cout<<a-c; if(b-d>=0) cout<<"+"; cout<<b-d; cout<<'i'<<endl; } void mul(double a,double b,double c,double d) { cout<<a*c-b*d; if(a*d+b*c>=0) cout<<"+"; cout<<a*d+b*c; cout<<'i'<<endl; } void div(double a,double b,double c,double d) { if(c==0&&d==0) { cout<<"error"<<endl; return; } cout<<(a*c+b*d)/(c*c+d*d); if(b*c-a*d>=0) cout<<"+"; cout<<(b*c-a*d)/(c*c+d*d); cout<<'i'<<endl; } int main() { char op; double a,b,c,d; cin>>a>>b>>op>>c>>d; if(op=='+') add(a,b,c,d); if(op=='-') sub(a,b,c,d); if(op=='*') mul(a,b,c,d); if(op=='/') div(a,b,c,d); return 0; }