1 #ifndef __MYCOMPLEX_H__ 2 #define __MYCOMPLEX_H__ 3 4 class complex; 5 complex& __doapl(complex*, const complex&); //友元可以在类外声明 6 complex& __doami(complex*, const complex&); 7 complex& __doaml(complex*, const complex&); 8 9 10 class complex{ 11 public: 12 complex(double r = 0, double i = 0) :re(r), im(i) {} 13 complex(const complex& x) { re = x.real(); im = x.imag(); } 14 15 complex& operator += (const complex&); 16 complex& operator -= (const complex&); 17 complex& operator *= (const complex&); 18 19 double real() const { return re; } 20 double imag() const { return im; } 21 22 private: 23 double re, im; 24 25 friend complex& __doapl(complex*, const complex&);//TODO plus 26 friend complex& __doami(complex*, const complex&);//TODO minus 27 friend complex& __doaml(complex*, const complex&);//TODO multiply 28 }; 29 30 //友元函数不是类的成员函数, 没有class 31 inline complex& __doapl(complex* ths, const complex& r){ 32 ths->re += r.re; 33 ths->im += r.im; 34 return *ths; 35 } 36 37 inline complex& __doami(complex* ths, const complex& r){ 38 ths->re -= r.re; 39 ths->im -= r.im; 40 return *ths; 41 } 42 43 //(a+bi)(c+di) = (ac-bd)+(ad+bc)i 44 inline complex& __doaml(complex* ths, const complex& r){ 45 double f = ths->re * r.re - ths->im * r.im; 46 ths->im = ths->re * r.im + ths->im * r.re; 47 ths->re = f; 48 return *ths; 49 } 50 51 inline complex& complex::operator *= (const complex& r){ 52 return __doaml(this, r); 53 } 54 55 inline complex& complex::operator += (const complex& r){ 56 return __doapl(this, r); 57 } 58 59 inline complex& complex::operator -= (const complex& r){ 60 return __doami(this, r); 61 } 62 63 /* ------------------ */ 64 65 inline double imag(const complex& x){ 66 return x.imag(); 67 } 68 69 inline double real(const complex& x){ 70 return x.real(); 71 } 72 73 inline complex operator + (const complex& x, const complex& y){ //区分+=, 不用写在public里, 要考虑多种情况 74 return complex(real(x) + real(y), imag(x) + imag(y)); 75 } 76 77 inline complex operator + (const complex& x, double y){ 78 return complex(real(x) + y, imag(x)); 79 } 80 81 inline complex operator + (double x, const complex& y){ 82 return complex(x + real(y), imag(y)); 83 } 84 85 inline complex operator - (const complex& x, const complex& y){ 86 return complex(real(x) - real(y), imag(x) - imag(y)); 87 } 88 89 inline complex operator - (const complex& x, double y){ 90 return complex(real(x) - y, imag(x)); 91 } 92 93 inline complex operator - (double x, const complex& y){ 94 return complex(x - real(y), -imag(y)); 95 } 96 97 //(a+bi)(c+di) = (ac-bd)+(ad+bc)i 98 inline complex operator * (const complex& x, const complex& y){ 99 return complex(real(x) * real(y) - imag(x) * imag(y), 100 real(x) * imag(y) + imag(x) * real(y)); 101 } 102 103 inline complex operator * (const complex& x, double y){ 104 return complex(real(x) * y, imag(x) * y); 105 } 106 107 inline complex operator * (double x, const complex& y){ 108 return complex(x * real(y), x * imag(y)); 109 } 110 111 inline complex operator / (const complex& x, double y){ 112 return complex(real(x) / y, imag(x) / y); 113 } 114 115 //取正 116 inline complex operator + (const complex& x){ 117 return x; 118 } 119 120 inline complex operator - (const complex& x){ 121 return complex(-real(x), -imag(x)); 122 } 123 124 inline bool operator == (const complex& x, const complex& y){ 125 return real(x) == real(y) && imag(x) == imag(y); 126 } 127 128 inline bool operator == (const complex& x, double y){ 129 return real(x) == y && imag(x) == 0; 130 } 131 132 inline bool operator == (double x, const complex& y){ 133 return x == real(y) && imag(y) == 0; 134 } 135 136 inline bool operator != (const complex& x, const complex& y){ 137 return real(x) != real(y) || imag(x) != imag(y); 138 } 139 140 inline bool operator != (const complex& x, double y){ 141 return real(x) != y || imag(x) != 0; 142 } 143 144 inline bool operator != (double x, const complex& y){ 145 return x != real(y) || imag(y) != 0; 146 } 147 148 /* ------------------ */ 149 150 #include <cmath> 151 152 //复数的极坐标定义 153 inline complex polar(double r, double t){ 154 return complex(r * cos(t), r * sin(t)); 155 } 156 157 //共轭复数 158 inline complex conj(const complex& x){ 159 return complex(real(x), -imag(x)); 160 } 161 162 //复数的向量的模, 模值平方 163 inline double norm(const complex& x){ 164 return real(x) * real(x) + imag(x) * imag(x); 165 } 166 167 #endif // !__MYCOMPLEX_H__
1 #include<iostream> 2 #include"complex.h" 3 4 using namespace std; 5 6 ostream& operator << (ostream& os, const complex& x){ 7 return os << "(" << real(x) << ", " << imag(x) << "i)"; 8 } 9 10 int main(){ 11 complex c1(3, 2); 12 complex c2(5, 0); 13 cout << c1 << endl; 14 cout << c2 << endl; 15 16 complex c3; 17 complex c4(2); 18 complex c31(c3); 19 complex c41(c4); 20 cout << c3 << " " << c4 << endl; 21 cout << c31 << " " << c41 << endl; 22 23 24 complex c5(7, 3); 25 cout << c1 + c3 << endl; 26 27 complex c6 = c1 * c5; 28 cout << c5 << " " << c6 << " " << -c6 << endl; 29 30 cout << c6 - 2 << endl; 31 cout << c6 - c1 << endl; 32 cout << c6 * 9 << endl; 33 cout << c6 / 2 << endl; 34 35 cout << conj(c6) << endl; 36 cout << norm(c6) << endl; 37 cout << polar(10, 4) << endl; 38 39 cout << (c1 += c2) << endl; 40 41 cout << (c1 == c2) << endl; 42 cout << (c1 != c2) << endl; 43 cout << +c2 << endl; 44 cout << -c2 << endl; 45 46 cout << (c2 - 2) << endl; 47 cout << (5 + c2) << endl; 48 49 return 0; 50 }
标签:real,return,imag,C++,complex,inline,const,面对 From: https://www.cnblogs.com/karinto/p/17140266.html