#include<iostream.h>
#include<math.h>
class complex{
public:
complex();
void setComplex(double r,double i);
double fabs(complex a);//求复数绝对值(模)
void print();//复数信息的输出
friend complex operator +(complex a,complex b);//求复数加法运算
friend complex operator -(complex a,complex b);//求复数减法运算
friend complex operator *(complex a,complex b);//求复数乘法运算
friend complex operator /(complex a,complex b); //求复数除法运算
complex pow(complex a,int n);//求复数乘方运算
friend complex operator ++(complex &a);//求复数自加运算
friend complex operator --(complex &a);//求复数自减运算
private:
double real;
double imag;
};
complex::complex()//初始化信息
{
real=0;
imag=0;
}
void complex::setComplex(double r,double i)
{
real=r;
imag=i;
}
double complex::fabs(complex a)//求复数绝对值(模)
{
double temp;
temp=sqrt(a.real*a.real+a.imag*a.imag);
return temp;
}
complex operator +(complex a,complex b)//求复数加法运算
{
complex temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
complex operator -(complex a,complex b)//求复数减法运算
{
complex temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
complex operator *(complex a,complex b)//求复数乘法运算
{
complex temp;
temp.real=a.real*b.real-a.imag*b.imag;
temp.imag=a.real*b.imag+a.imag*b.real;
return temp;
}
complex operator/(complex a,complex b)//求复数除法运算
{
complex temp;
double t;
t=1/(b.real*b.real+b.imag*b.imag);
temp.real=(a.real*b.real+a.imag*b.imag)*t;
temp.imag=(b.real*a.imag-b.imag*a.real)*t;
return temp;
}
complex complex::pow(complex a,int n)//求复数乘方运算
{
complex temp;
int i;
temp.real=a.real;
temp.imag=a.imag;
for(i=1;i<n;i++)
{
temp.real=a.real*a.real-a.imag*a.imag;
temp.imag=a.real*a.imag+a.imag*a.real;
}
return temp;
}
complex operator ++(complex &a)//求复数自加运算
{
++a.real;
return a;
}
complex operator --(complex &a)//求复数自减运算
{
--a.real;
return a;
}
void complex::print()//输出复数信息
{
cout<<real;
if(imag>0) cout<<"+";
if(imag!=0) cout<<imag<<"i\n";
}
void main()
{
complex A1,A2,A3,A4,A5,A6,A7,A8,A9;
double a,b,c,d;
short i,j,k,m,n=1;
while (n==1)
{
cout<<"两个数的运算请按1,一个数的运算请按2:"<<endl;
cin>>i;
if(i==1)
{
cout<<"请输入第一个数的实部";
cin>>a;
cout<<"请输入第一个数的虚部";
cin>>b;
cout<<"请输入第二个数的实部";
cin>>c;
cout<<"请输入第二个数的虚部";
cin>>d;
A1.setComplex(a,b);
A2.setComplex(c,d);
cout<<"n你输入的初始数据为A1=";
A1.print();
cout<<"A2=";
A2.print();
cout<<"加法运算请按1,减法运算请按2,乘法运算请按3,除法运算请按4:"<<endl;
cin>>j;
switch(j)
{
case 1:
A3=A1+A2;
cout<<"A1+A2=";
A3.print();
break;
case 2:
A4=A1-A2;
cout<<"A1-A2=";
A4.print();
break;
case 3:
A5=A1*A2;
cout<<"A1*A2=";
A5.print();
break;
case 4:
A6=A1/A2;
cout<<"A1/A2=";
A6.print();
break;
default:
return ;
}
}
else if(i==2)
{
cout<<"请输入复数的实部";
cin>>a;
cout<<"请输入复数的虚部";
cin>>b;
A1.setComplex(a,b);
A2.setComplex(a,b);
cout<<"你输入的初始数据为A=";
A1.print();
cout<<"求绝对值请按1,求乘方运算请按2,自加请按3,自减请按4:";
cin>>k;
switch(k)
{
case 1:
cout<<"|A|=";
cout<<A1.fabs(A1)<<endl;
break;
case 2:
cout<<"请输入乘方数";
cin>>m;
A7=A1.pow(A1,m);
cout<<"A的"<<m<<"次乘方为:";
A7.print();
break;
case 3:
A8=(++A1);
cout<<"++A1=";
A8.print();
break;
case 4:
A9=(--A2);
cout<<"--A=";
A9.print();
break;
default:
return ;
}
}
else
{
cout<<"输入错误,请重新输入:"<<endl;
}
cout<<"继续吗?继续请按1,退出请按2";
cin>>n;
}
}