1个回答
展开全部
#include<iostream.h>
class complex
{
private:
double real;
double imag;
public:
complex()
{
real=imag=0;
}
complex(double rpart,double ipart)
{
real=rpart;
imag=ipart;
}
complex operator+(const complex &com)
{
complex temp;
temp.real=real+com.real;
temp.imag=imag+com.imag;
return temp;
}
complex operator-(const complex &com)
{
complex temp;
temp.real=real-com.real;
temp.imag=imag-com.imag;
return temp;
}
complex operator*(const complex &com)
{
complex temp;
temp.real=real*com.real-imag*com.imag;
temp.imag=real*com.imag+imag*com.real;
return temp;
}
complex operator/(const complex &com)
{
complex temp;
temp.real=(real*com.real+imag*com.imag)/(com.real*com.real+com.imag*com.imag);
temp.imag=(imag*com.real-real*com.imag)/(com.real*com.real+com.imag*com.imag);
return temp;
}
void display()
{
if(imag>=0)
cout<<"("<<real<<"+"<<imag<<"i)";
else
cout<<"("<<real<<imag<<"i)";
}
};
int main(void)
{
double a,b,c,d;
cout<<"输入两个复数,即a,b,c,d的值:";
cin>>a>>b>>c>>d;
complex n1(a,b),n2(c,d);
cout<<"两个复数值为:";
cout<<"A=";
n1.display();
cout<<" B=";
n2.display();
cout<<endl;
cout<<endl<<endl<<"两个复数的加法:"<<endl;
complex result1;
result1=n1+n2;
n1.display();
cout<<"+";
n2.display();
cout<<"=";
result1.display();
cout<<endl<<endl<<"两个复数的减法:"<<endl;
complex result2;
result2=n1-n2;
n1.display();
cout<<"-";
n2.display();
cout<<"=";
result2.display();
cout<<endl<<endl<<"两个复数的乘法:"<<endl;
complex result3;
result3=n1*n2;
n1.display();
cout<<"×";
n2.display();
cout<<"=";
result3.display();
cout<<endl<<endl<<"两个复数的除法:"<<endl;
if(c==0&&d==0)
cout<<"不能进行运算,除数不能为0"<<endl;
else
{
complex result4;
result4=n1/n2;
n1.display();
cout<<"÷";
n2.display();
cout<<"=";
result4.display();
}
cout<<endl;
return 0;
}
class complex
{
private:
double real;
double imag;
public:
complex()
{
real=imag=0;
}
complex(double rpart,double ipart)
{
real=rpart;
imag=ipart;
}
complex operator+(const complex &com)
{
complex temp;
temp.real=real+com.real;
temp.imag=imag+com.imag;
return temp;
}
complex operator-(const complex &com)
{
complex temp;
temp.real=real-com.real;
temp.imag=imag-com.imag;
return temp;
}
complex operator*(const complex &com)
{
complex temp;
temp.real=real*com.real-imag*com.imag;
temp.imag=real*com.imag+imag*com.real;
return temp;
}
complex operator/(const complex &com)
{
complex temp;
temp.real=(real*com.real+imag*com.imag)/(com.real*com.real+com.imag*com.imag);
temp.imag=(imag*com.real-real*com.imag)/(com.real*com.real+com.imag*com.imag);
return temp;
}
void display()
{
if(imag>=0)
cout<<"("<<real<<"+"<<imag<<"i)";
else
cout<<"("<<real<<imag<<"i)";
}
};
int main(void)
{
double a,b,c,d;
cout<<"输入两个复数,即a,b,c,d的值:";
cin>>a>>b>>c>>d;
complex n1(a,b),n2(c,d);
cout<<"两个复数值为:";
cout<<"A=";
n1.display();
cout<<" B=";
n2.display();
cout<<endl;
cout<<endl<<endl<<"两个复数的加法:"<<endl;
complex result1;
result1=n1+n2;
n1.display();
cout<<"+";
n2.display();
cout<<"=";
result1.display();
cout<<endl<<endl<<"两个复数的减法:"<<endl;
complex result2;
result2=n1-n2;
n1.display();
cout<<"-";
n2.display();
cout<<"=";
result2.display();
cout<<endl<<endl<<"两个复数的乘法:"<<endl;
complex result3;
result3=n1*n2;
n1.display();
cout<<"×";
n2.display();
cout<<"=";
result3.display();
cout<<endl<<endl<<"两个复数的除法:"<<endl;
if(c==0&&d==0)
cout<<"不能进行运算,除数不能为0"<<endl;
else
{
complex result4;
result4=n1/n2;
n1.display();
cout<<"÷";
n2.display();
cout<<"=";
result4.display();
}
cout<<endl;
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询