#include <iostream.h>
//定义Complex类==============================================================================
class Complex
{public:
Complex(){real=0;imag=0;}//默认构造函数
Complex(double r,double i){real=r;imag=i;}//构造函数
void setComplex(double r,double i);//重新设置复数
Complex operator+(Complex &c2);//运算符+重载声明
Complex operator-(Complex &c2);//运算符-重载声明
Complex operator*(Complex &c2);//运算符*重载声明
Complex operator/(Complex &c2);//运算符/重载声明
friend ostream & operator<<(ostream&,Complex&);//重载流插入运算符“<<”,用于复数输出
friend istream & operator>>(istream&,Complex&);//重载流提取运算符“>>”,用于复数输入
void complexAdd();
void complexSubtract();
void complexMultiply();
void complexDivide();
friend void complexCompare();
friend double mod(const Complex &c);//求模长的平方
private:
double real;
double imag;
};
//==========================================================================
//================成员函数类外定义==========================================
void Complex::setComplex(double r,double i)
{
real=r;imag=i;
}
//----------------------------------------------
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
//----------------------------------------------
Complex Complex::operator-(Complex &c2)
{
Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
//----------------------------------------------
Complex Complex::operator*(Complex &c2)
{
Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;
}
//----------------------------------------------
Complex Complex::operator/(Complex &c2)
{
Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;
}
//-----------------------------------------------
ostream & operator<<(ostream &output,Complex &c)
{
if(c.imag>=0)
output<<"("<<c.real<<"+"<<c.imag<<"i)";
else
output<<"("<<c.real<<" "<<c.imag<<"i)";
return output;
}
//--------------------------------------------------
istream & operator>>(istream &input,Complex &c)
{
cout<<"请输入一个复数,实部和虚部,中间用空格隔开:";
input>>c.real>>c.imag;
return input;
}
//=======================================================================
//=================功能函数(普通函数)的定义============================
//复数加法
void complexAdd()
{
int num ;
cout<<"请输入操作数(参加加法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
while(num>10||num<0)//使输入的操作数<=10
{
cout<<"您输入的数大于10或小于0,请重新输入!"<<endl;
cout<<"请输入操作数(参加加法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
}
if(num==0)cout<<"没有输入任何复数n";
else
{
Complex total(0.0,0.0);
for(int i=0;i<num;i++)
{
Complex c;
cin>>c;
total=total+c;
}
cout<<"这"<<num<<"个复数累加的和为"<<total<<endl;
}
}
//复数减法
void complexSubtract()
{
int num ;
cout<<"请输入操作数(参加减法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
while(num>10||num<0)//使输入的操作数<=10
{
cout<<"您输入的数大于10或小于0,请重新输入!"<<endl;
cout<<"请输入操作数(参加减法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
}
if(num==0)cout<<"没有输入任何复数nnn";
else
{
Complex total(0.0,0.0);
cin>>total;
for(int i=2;i<=num;i++)
{
Complex c;
cin>>c;
total=total-c;
}
cout<<"这"<<num<<"个复数累减的和为"<<total<<endl;
}
}
//复数乘法
void complexMultiply()
{
int num ;
cout<<"输入操作数(参加乘法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
while(num>10||num<0)//使输入的操作数<=10
{
cout<<"您输入的数大于10或小于0,请重新输入!"<<endl;
cout<<"输入操作数(参加乘法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
}
if(num==0)cout<<"没有输入任何复数nnn";
else
{
Complex total(1.0,0.0);//注意此处的初值
for(int i=1;i<=num;i++)
{
Complex c;
cin>>c;
total=total*c;
}
cout<<"这"<<num<<"个复数累乘的和为"<<total<<endl;
}
}
//复数除法
void complexDivide()
{
int num ;
cout<<"输入操作数(参加除法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
while(num>10||num<0)//使输入的操作数<=10
{
cout<<"您输入的数大于10或小于0,请重新输入!"<<endl;
cout<<"输入操作数(参加除法的复数)的个数(注意操作数<=10并且>=0):";
cin>>num ;
}
if(num==0)cout<<"没有输入任何复数nnn";
else
{
Complex total;
cin>>total;//把参加除法的第一个数赋值给total
for(int i=2;i<=num;i++)
{
Complex c;
cin>>c;
if(!mod(c))
{
cout<<"除数为零,输入有误!请重新输入nn";
cin>>c;
}
total=total/c;
}
cout<<"这"<<num<<"个复数累除的商为"<<total<<endl;
}
}
//两个复数的比较函数
void complexCompare() //两复数比较函数
{
Complex c1,c2;
cout<<"请输入两个要比较的复数:n";
cin>>c1;
cin>>c2;
if((c1.real==c2.real)&&(c1.imag==c2.imag)) cout<<"这两个复数相等n";
else if(mod(c1)>mod(c2)) cout<<c1<<"的模大于"<<c2<<"的模n";//比较模长
else if(mod(c1)<mod(c2)) cout<<c2<<"的模大于"<<c1<<"的模n";//比较模长
else cout<<"这两个复数的模相等n";//比较模长
}
//求模长平方函数
double mod(const Complex &c)
{
return(c.real*c.real+c.imag*c.imag);
}
//主函数
int main()
{
int choice;
cout<<"这是一个简单的复数加减乘除计算器,请输入功能选择:"<<endl;
cout<<"0.退出 1.加法 2,减法 3.乘法 4.除法 5.复数大小(模)的比较nn";
cin>>choice;//输入功能选择
do
{
if(choice==0) {cout<<"nn欢迎下次继续使用复数计算器!nn"; break;}
else if(choice==1) complexAdd();
else if(choice==2) complexSubtract();
else if(choice==3) complexMultiply();
else if(choice==4) complexDivide();
else if(choice==5) complexCompare();
else cout<<"nn输入错误,请重新输入!nn";
cout<<"nnn这是一个简单的复数加减乘除计算器,请输入功能选择:"<<endl<<endl;
cout<<"0.退出 1.加法 2,减法 3.乘法 4.除法 5.复数大小(模)的比较n";
cin>>choice;//输入功能选择
}while(choice!=0);
return 0;
}