1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| #include<bits/stdc++.h> using namespace std; class Complex { public: Complex(double x, double y) : re(x), im(y){} Complex() : re(0), im(0){} double real() const { return re; } double imag() const { return im; } Complex &operator+=(const Complex &c); template <typename T> Complex &operator+=(const T num);
Complex &operator-=(const Complex &c); template <typename T> Complex &operator-=(const T num);
Complex &operator*=(const Complex &c); template <typename T> Complex &operator*=(const T num);
Complex conj() { return Complex(this->re, -this->im); }
double length() { return pow(this->re * this->re + this->im * this->im, 0.5); }
private: double re, im; friend Complex &_plus(Complex *, const Complex &); friend Complex &_minus(Complex *, const Complex &); friend Complex &_times(Complex *, const Complex &); };
inline Complex &_plus(Complex *ths, const Complex &c) { ths->re += c.re; ths->im += c.im; return *ths; }
inline Complex &_minus (Complex *ths, const Complex &c) { ths->re -= c.re; ths->im -= c.im; return *ths; }
inline Complex &_times(Complex *ths, const Complex &c) { ths->re = ths->re * c.re - ths->im * c.im; ths->im = ths->re * c.im + ths->im * c.re; return *ths; }
inline Complex & Complex::operator += (const Complex & c) { return _plus(this, c); }
template <typename T> inline Complex & Complex::operator += (const T num) { this->re += num; return *this; }
inline Complex & Complex::operator -= (const Complex & c) { return _minus(this, c); }
template <typename T> inline Complex & Complex::operator -= (const T num) { this->re -= num; return *this; }
inline Complex & Complex::operator*=(const Complex & c) { return _times(this, c); }
template <typename T> inline Complex & Complex::operator *= (const T num) { this->re *= num; this->im *= num; return *this; }
inline Complex operator+ (const Complex & x,const Complex & y) { return Complex(x.real() + y.real(), x.imag() + y.imag()); }
template <typename T> inline Complex operator+(const Complex &c, T num) { return Complex(c.real() + num, c.imag()); }
template <typename T> inline Complex operator+(T num, const Complex &c) { return Complex(c.real() + num, c.imag()); }
inline Complex operator-(const Complex &x, const Complex &y) { return Complex(x.real() - y.real(), x.imag() - y.imag()); }
template <typename T> inline Complex operator-(const Complex &c, T num) { return Complex(c.real() - num, c.imag()); }
template <typename T> inline Complex operator-(T num, const Complex &c) { return Complex(num - c.real(), -c.imag()); }
inline Complex operator-(const Complex &c) { return Complex(-c.real(), -c.imag()); }
inline Complex operator*(const Complex &x, const Complex &y) { return Complex(x.real() * y.real() - x.imag() * y.imag(), x.real() * y.imag() + x.imag() * y.real());
}
template <typename T> inline Complex operator*(const Complex &c, T num) { return Complex(c.real() * num, c.imag() * num); }
template <typename T> inline Complex operator*(T num, const Complex &c) { return Complex(c.real() * num, c.imag() * num); } inline ostream& operator <<(ostream& os,const Complex & c) { if (c.real() && c.imag()) { if (c.imag() >0) { return os << c.real() << "+" << c.imag() << "i"; } else { return os << c.real() << c.imag() << "i"; } } else if (c.real()) { return os << c.real(); } else { return os << c.imag() << "i"; } }
inline bool operator==(const Complex &x, const Complex &y) { return (x.real() == y.real()) && (x.imag() == y.imag()); }
inline bool operator!=(const Complex &x, const Complex &y) { return (x.real() != y.real()) || (x.imag() != y.imag()); }
int main() { Complex x(3,4); Complex y(3, -4); cout << x.length() << endl; cout << x * y << endl; x += 2; cout << x << endl; cout << -x << endl; cout << (x == y.conj())<<endl; cout << (x.conj() != y) << endl; cout << x + y << endl; cout << 1 + x << endl; cout << y + 3 << endl; cout << 2 * x << endl; return 0; }
|