#include
using namespace std;
class complex
{
private:
double real,imag;
public:
complex(int r=0.0,int i=0.0)
{real=r;
imag=i;
}
~complex(){}
void input()
{cin>>real>>imag;
}
void output()
{cout<
}
void set(double r,double i)
{real=r;
imag=i;
}
double get_r()
{return real;
}
double get_i()
{return imag;}
friend complex add(complex c1,complex c2);
friend complex sub(complex c1,complex c2);
friend complex mul(complex c1,complex c2);
friend complex div(complex c1,complex c2);
};
complex add(complex c1,complex c2)//c=add(a,b)
{
complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return c3;
}
complex sub(complex c1,complex c2)
{
complex c3;
c3.real=c1.real-c2.real;
c3.imag=c1.imag-c2.imag;
return c3;
}
complex mul(complex c1,complex c2)
{ complex c3;
c3.real=c1.real*c2.real-c1.imag*c2.imag;
c3.imag =c1.real*c2.imag+c1.imag*c2.real;
return c3;
}
complex div(complex c1,complex c2)
{ complex c3;
int t;
c3.real=(c1.real*c2.real+c1.imag*c2.imag)/t;
c3.imag=(c1.imag*c2.real-c1.real*c2.imag)/t;
t=c2.real*c2.real-c2.imag*c2.imag;
return c3;
}
void menu()
{
cout<<"1 加法\n";
cout<<"2 减法\n";
cout<<"3 乘法\n";
cout<<"4 除法\n";
cout<<"0 退出\n";
cout<<"请选择:";
}
void main()
{
do{
menu();
int choose;
cin>>choose;
complex c1,c2,c3;
switch(choose)
{
case 1:cout<<"请输入第一个操作数:";
c1.input();
cout<<"请输入第二个操作数:";
c2.input();
c3=add(c1,c2);
cout<<"c1+c2=";
c3.output();
break;
case 2:cout<<"请输入第一个操作数:";
c1.input();
cout<<"请输入第二个操作数:";
c2.input();
c3=sub(c1,c2);
cout<<"c1-c2=";
c3.output();
break;
case 3:cout<<"请输入第一个操作数:";
c1.input();
cout<<"请输入第二个操作数:";
c2.input();
c3=mul(c1,c2);
cout<<"c1*c2=";
c3.output();
break;
case 4:cout<<"请输入第一个操作数:";
c1.input();
cout<<"请输入第二个操作数:";
c2.input();
c3=div(c1,c2);
cout<<"c1/c2=";
c3.output();
break;
case 0: exit(0);
default:cout<<"你的选择有误,请重新选择";
}
}while(1);
}
如果是为了作业的话,我建议你好好学!仅供参考!
#include
using namespace std;
class complex
{
friend ostream& operator<<(ostream&,const complex&);
friend istream& operator>>(istream&,complex&);
private:
double realpart,imagepart;
public:
complex(){};
complex operator+(complex);
complex operator-(complex);
complex operator*(complex);
complex operator/(complex);
};
ostream& operator<<(ostream& cout,const complex& data)
{
cout<
cout<
return cout;
}
istream& operator>>(istream& cin,complex& data)
{
char c,d;
cin>>data.realpart>>c>>data.imagepart>>d;
if(c=='-')
data.imagepart=-data.imagepart;
return cin;
}
complex complex::operator+(complex data)
{
complex a;
a.realpart=realpart+data.realpart;
a.imagepart=imagepart+data.imagepart;
return a;
}
complex complex::operator*(complex data)
{
complex a;
a.realpart=realpart*data.realpart;
a.imagepart=imagepart*data.imagepart;
return a;
}
complex complex::operator-(complex data)
{
complex a;
a.realpart=realpart-data.realpart;
a.imagepart=imagepart-data.imagepart;
return a;
}
complex complex::operator/(complex data)
{
complex a;
a.realpart=realpart/data.realpart;
a.imagepart=imagepart/data.imagepart;
return a;
}
int main()
{
complex a,b;
cin>>a;
cin>>b;
cout< cout<
}