源码
(因为是昨晚写的,分析过程忘了,大家就直接看源码好了,反正还蛮简单的hhh)
#include <iostream>
using namespace std;
class complex
{
public:
complex(int i = 0, int j = 0) :a(i), b(j) {}
~complex() {};
complex operator+(complex y)
{
complex temp;
temp.a = a + y.a;
temp.b = b + y.b;
return temp;
}
complex operator-(complex y)
{
complex temp;
temp.a = a - y.a;
temp.b = b - y.b;
return temp;
}
complex operator*(complex y)
{
complex temp;
temp.a = a * y.a - b * y.b;
temp.b = a * y.b + b * y.a;
return temp;
}
complex operator/(complex y)
{
complex temp;
temp.a = (a * y.a + b * y.b) / (y.a * y.a + y.b * y.b);
temp.b = (b * y.a + a * y.b) / (y.a * y.a + y.b * y.b);
return temp;
}
void getcomplex(float& aa, float& bb)
{
aa = a;
bb = b;
}
private:
float a, b;
};
ostream& operator<<(ostream& os, complex& s)
{
float a, b;
s.getcomplex(a, b);
os << a << "+" << b << "i";
return os;
}
int main()
{
complex x(1,1),y(1,1),z;
z = x + y;
cout << z << endl;
z = x - y;
cout << z << endl;
z = x * y;
cout << z << endl;
z = x / y;
cout << z << endl;
return 0;
}