关于C++ 判断语句的问题
今天做了道题目,是关于复数相加的,我被卡在了实现复数相加后的输出问题上。题目要求输出格式如下:(1-2i)+(3+4i)=4+2i
(1-2i)+(1+2i)=2
(4)+(-2i)=4-2i
(-1+i)+(1)=i
要分别判断每一个复数的实部和虚部是否为0,为0 的话就不输出。以下是我写的程序,想请大家帮我看下,怎么用比较简便的方法来判断每个复数的实部和虚部是否为0,以便实现正确的输出格式,谢谢!
#include <iostream>
using namespace std;
class Complex
{
double real,image;
public:
void set( double a,double b)
{
real=a;
image=b;
}
Complex operator +(const Complex &z);
};
Complex Complex::operator +(const Complex &z)
{
Complex s;
s.real=real+z.real;
s.image=image+z.image;
if(real==0&&image!=0&&z.real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
cout<<"("<< noshowpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
else if(image==0&&real!=0&&z.real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
cout<<"("<< noshowpos<<real<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
else if(z.real==0&&image!=0&&real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
else if(z.image==0&&image!=0&&z.real!=0&&real!=0&&s.real!=0&&s.image!=0)
cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real<<")"<<"="<< noshowpos<<s.real<<showpos<<s.image<<"i"<<endl;
else if(s.real==0&&image!=0&&z.real!=0&&z.image!=0&&real!=0&&s.image!=0)
cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.image<<"i"<<endl;
else if(s.image==0&&image!=0&&z.real!=0&&z.image!=0&&s.real!=0&&real!=0)
cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real <<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real<<endl;
else if(z.real==0&&image==0&&real!=0&&z.image!=0&&s.real!=0&&s.image!=0)
cout<<"("<< noshowpos<<real<<")"<<"+"<< noshowpos<<"("<<z.image <<")"<<"="<< noshowpos<<s.real <<showpos<<s.image<<"i"<<endl;
else if(z.image==0&&s.real==0&&image!=0&&z.real!=0&&real!=0&&s.image!=0)
cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real<<")"<<"="<< noshowpos<<s.image<<"i"<<endl;
else
cout<<"("<< noshowpos<<real<<showpos<<image<<"i"<<")"<<"+"<< noshowpos<<"("<<z.real<<showpos<<z.image<<"i"<<")"<<"="<< noshowpos<<s.real <<showpos<<s.image<<"i"<<endl;
return s;
}
int main()
{ double n,m,n1,m1;
while(cin>>n>>m>>n1>>m1)
{
if(n1==0&&m1==0)
cout<<"非法输入"<<endl;
else
{
Complex t;
Complex t1;
Complex t2;
t. set(n,m);
t1. set(n1,m1);
t2=t+t1;
}
}
return 0;
}