|
这个程序我是在VC++6.0上运行的
#include <iostream>
using namespace std;
class complex
{
public:
complex(){real=0;image=0;}
complex(double a,double b):real(a),image(b){}
complex operator+(complex &);
complex operator-(complex &);
complex operator*(complex &);
complex operator/(complex &);
void display()
{
cout<<real;
if (image>0)
cout<<"+";
cout<<image<<"i"<<endl;
}
private:
double real;
double image;
};
complex complex::operator+(complex &temp)
{
return complex(real+temp.real,image+temp.image);
}
complex complex::operator-(complex &temp)
{
return complex(real-temp.real,image-temp.image);
}
complex complex::operator*(complex &temp)
{
return complex(real*temp.real-image*temp.image,real*temp.image+image*temp.real);
}
complex complex::operator/(complex &temp)
{
complex c;
c.real=(real*temp.real+image*temp.image)/(temp.real*temp.real+temp.image*temp.image);
c.image=(real*temp.image*(-1)+image*temp.real)/(temp.real*temp.real+temp.image*temp.image);
return c;
}
int main()
{
complex c1(15,3),c2(5,1),c3;
c3=c1+c2;
c3.display();
c3=c1-c2;
c3.display();
c3=c1*c2;
c3.display();
c3=c1/c2;
c3.display();
return 0;
}
运行结果为20+4i
10+2i
72+30i
30i
我对最后一个结果不明白,我认为是3 |
|