复数的运算
#include <iostream>using namespace std;
class Complex{
public:
Complex(double r=0.0,double i=0.0);
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);
void display();
private:
double real;
double imag;
};
Complex::Complex(double r,double i)
{ real=r;imag=i; }
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;
temp.real=a.real*b.real+a.imag*b.imag;
temp.imag=a.real*b.imag-a.imag*b.real;
return temp;
}
void Complex::display()
{ cout<<real;
if(imag>0) cout<<"+";
if(imag!=0) cout<<imag<<"i\n";
}
int main()
{ double a,b,c,d;
cin>>a>>b>>c>>d;
Complex A1(a,b),A2(c,d),A3,A4,A5,A6;
A1.display();
A2.display();
A3=A1+A2;
A3.display();
A4=A1-A2;
A4.display();
A5=A1*A2;
A5.display();
A6=A1/A2;
A6.display();
return 0;
}
不知道怎么加上操作界面?比如 ,有人能帮忙改吗?