class Complex {
double real;
double imag;
public:
friend istream& operator>>(istream& is, Complex& c);
friend ostream& operator <<(ostream& os, const Complex& a);
Complex() {
real = 0;
imag = 0;
}
Complex(double a, double b) :real(a), imag(b) {}
//+
Complex operator +(const Complex& a)
{
Complex b;
b.imag = imag + a.imag;
b.real = real + a.real;
return b;
}
//-
Complex operator -(const Complex& a)
{
Complex b;
b.imag = imag - a.imag;
b.real = real - a.real;
return b;
}
//*
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag,
real * other.imag + imag * other.real);
}
// /
Complex operator/(const Complex& other) const {
double denominator = other.real * other.real + other.imag * other.imag;
return Complex((real * other.real + imag * other.imag) / denominator,
(imag * other.real - real * other.imag) / denominator);
}
};
ostream& operator <<(ostream& os, const Complex& a)
{
cout << a.real << "+" << a.imag << "i" << endl;
return os;
}
istream& operator>>(istream& is, Complex& c)
{
cout << "请输入实部和虚部:" << endl;
is >> c.real >> c.imag;
return is;
}
int main()
{
Complex a = { 4,5 };
Complex b = { 2,3 };
Complex c = a + b;
Complex d;
cin >> d;
Complex e = c / d;
cout << "c\t" << c << endl;
cout << "d\t" << d << endl;
cout << "e\t" << e << endl;
return 0;
}