#include<iostream>
using namespace std;
class Complex
{
float Real, Image;
public:
Complex(float r = 0, float i = 0) { Real = r; Image = i; }
void Show()
{
cout << "Real=" << Real << '\t' << "Image=" << Image << '\n';
}
friend Complex operator *(Complex &, Complex &);
Complex operator /(Complex &); //重载运算符
friend Complex operator -(Complex &, Complex &);
Complex operator +(Complex &);
void display();
};
Complex operator *(Complex &c1, Complex &c2)
{
Complex t;
t.Real = c1.Real * c2.Real - c1.Image * c2.Image;
t.Image = c1.Image*c2.Real + c1.Real* c2.Image;
return t;
}
Complex Complex::operator /(Complex &c)
{
Complex t;
t.Real = (Real *c.Real + Image * c.Image) / (c.Real*c.Real + c.Image * c.Image);
t.Image = (Image *c.Real - Real * c.Image) / (c.Real*c.Real + c.Image * c.Image);
return t;
}
Complex operator -(Complex &c1, Complex &c2)
{
Complex t;
t.Real = c1.Real - c2.Real;
t.Image = c1.Image - c2.Image;
return t;
}
Complex Complex::operator +(Complex &c)
{
Complex t;
t.Real = c.Real + Real;
t.Image = c.Image + Image;
return t;
}
void Complex::display()
{
cout << Real << "+" << Image << "i" << endl;
}
int main()
{
Complex c1(1, 2), c2(3, 4), c3, c4, c5, c6;
cout << "设c1=1+2i,c2=3+4i\n";
c3 = c1 + c2;
cout << "c3 = c1 + c2 = ";
c3.display();
c4 = c1 - c2;
cout << "c4 = c1 - c2 = ";
c4.display();
c5 = c1*c2;
cout << "c5 = c1 * c2 = ";
c5.display();
c6 = c1 / c2;
cout << "c6 = c1 / c2 = ";
c6.display();
return 0;
}