C++面对对象:实现complex类

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

 

原文链接: https://www.cnblogs.com/karinto/p/17140266.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C++面对对象:实现complex类

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/319425

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月24日 下午3:18
下一篇 2023年2月24日 下午3:18

相关推荐