复数运算符.vcxproj
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
public:
Complex(double r = 0.0, double m = 0.0) :real(r), magic(m)
{
cout << "Complex instructor!" << endl;
}
~Complex()
{
cout << "Complex destructor!" << endl;
}
void display() const
{
if (real == 0)
cout << magic << "i" << endl;
else
{
if (magic > 0)
cout << real << "+" << magic << "i" << endl;
else if (magic < 0)
cout << real << magic << "i" << endl;
else
cout << real << endl;
}
}
Complex operator +(const Complex& c2) const;
Complex operator -(const Complex& c2) const;
Complex operator *(const Complex& c2) const;
Complex operator /(const Complex& c2) const;
friend Complex operator + (const double& d, const Complex& c);
friend Complex operator - (const double& d, const Complex& c);
friend Complex operator + (const Complex& c, const double& d);
friend Complex operator - (const Complex& c, const double& d);
friend ostream& operator << (ostream& out, const Complex& c);//operator"<<"
friend Complex operator / (const double& d, const Complex& c);
friend Complex operator * (const Complex& c, const double& d);
friend Complex operator / (const Complex& c, const double& d);
private:
double real, magic;
};
Complex Complex::operator +(const Complex& c2) const
{
return Complex(real + c2.real, magic + c2.magic);//copy
}
Complex Complex::operator -(const Complex& c2) const
{
return Complex(real - c2.real, magic - c2.magic);//copy
}
Complex Complex::operator *(const Complex& c2) const
{
return Complex(real * c2.real - magic * c2.magic, c2.real * magic + real*c2.magic);//copy
}
Complex Complex::operator /(const Complex& c2) const
{
double k = pow(c2.real, 2) + pow(c2.magic, 2);
return Complex((real * c2.real + magic * c2.magic)/k, (c2.real * magic - real * c2.magic)/k);//copy
}
Complex operator + (const double& d, const Complex& c)
{
return Complex(d + c.real, c.magic);//copy
}
Complex operator - (const double& d, const Complex& c)
{
return Complex(d - c.real, -c.magic);//copy
}
Complex operator + (const Complex& c, const double& d)
{
return Complex(c.real + d, c.magic);//copy
}
Complex operator / (const double& d, const Complex& c)
{
double k = pow(c.real, 2) + pow(c.magic, 2);
return Complex(d * c.real / k, - d * c.magic / k);//copy
}
Complex operator - (const Complex& c, const double& d)
{
return Complex(c.real - d, c.magic);//copy
}
Complex operator * (const Complex& c, const double& d)
{
return Complex(c.real * d, c.magic * d);//copy
}
Complex operator / (const Complex& c, const double& d)
{
return Complex(c.real / d, c.magic / d);//copy
}
ostream& operator<<(ostream& out, const Complex& c)
{
if (c.real == 0)
cout << c.magic << "i" << endl;
else
{
if (c.magic > 0)
cout << c.real << "+" << c.magic << "i" << endl;
else if (c.magic < 0)
cout << c.real << c.magic << "i" << endl;
else
cout << c.real << endl;
}
return out;
}
int main()
{
Complex C1(5, 4), C2(7,2), C3;
cout << "C1=" << C1 << endl;
cout << "C2=" << C2 << endl;
C3 = C1 + C2;
cout << "C3=" << C3 << endl;
C3 = C1 - C2;
cout << "C3=" << C3 << endl;
C3 = C1 * C2;
cout << "C3=" << C3 << endl;
C3 = C1 / C2;
cout << "C3=" << C3 << endl;
double d;
cin >> d;
C3 = d + C1;
cout << "C3=" << C3 << endl;
C3 = d - C1;
cout << "C3=" << C3 << endl;
C3 = C1 + d;
cout << "C3=" << C3 << endl;
C3 = C1 - d;
cout << "C3=" << C3 << endl;
C3 = C1 * d;
cout << "C3=" << C3 << endl;
C3 = C1 / d;
cout << "C3=" << C3 << endl;
C3 = d / C1;
cout << "C3=" << C3 << endl;
return 0;
}