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

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
//complex.h
#ifndef __MYCOMPLEX_H__
#define __MYCOMPLEX_H__
 
class complex;
complex& __doapl(complex*, const complex&); //友元可以在类外声明
complex& __doami(complex*, const complex&);
complex& __doaml(complex*, const complex&);
 
 
class complex{
public:
    complex(double r = 0, double i = 0) :re(r), im(i) {}
    complex(const complex& x) { re = x.real(); im = x.imag(); }
 
    complex& operator += (const complex&);
    complex& operator -= (const complex&);
    complex& operator *= (const complex&);
 
    double real() const { return re; }
    double imag() const { return im; }
 
private:
    double re, im;
 
    friend complex& __doapl(complex*, const complex&);//TODO plus
    friend complex& __doami(complex*, const complex&);//TODO minus
    friend complex& __doaml(complex*, const complex&);//TODO multiply
};
 
//友元函数不是类的成员函数, 没有class
inline complex& __doapl(complex* ths, const complex& r){
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}
 
inline complex& __doami(complex* ths, const complex& r){
    ths->re -= r.re;
    ths->im -= r.im;
    return *ths;
}
 
//(a+bi)(c+di) = (ac-bd)+(ad+bc)i
inline complex& __doaml(complex* ths, const complex& r){
    double f = ths->re * r.re - ths->im * r.im;
    ths->im = ths->re * r.im + ths->im * r.re;
    ths->re = f;
    return *ths;
}
 
inline complex& complex::operator *= (const complex& r){
    return __doaml(this, r);
}
 
inline complex& complex::operator += (const complex& r){
    return __doapl(this, r);
}
 
inline complex& complex::operator -= (const complex& r){
    return __doami(this, r);
}
 
/* ------------------ */
 
inline double imag(const complex& x){
    return x.imag();
}
 
inline double real(const complex& x){
    return x.real();
}
 
inline complex operator + (const complex& x, const complex& y){ //区分+=, 不用写在public里, 要考虑多种情况
    return complex(real(x) + real(y), imag(x) + imag(y));
}
 
inline complex operator + (const complex& x, double y){
    return complex(real(x) + y, imag(x));
}
 
inline complex operator + (double x, const complex& y){
    return complex(x + real(y), imag(y));
}
 
inline complex operator - (const complex& x, const complex& y){
    return complex(real(x) - real(y), imag(x) - imag(y));
}
 
inline complex operator - (const complex& x, double y){
    return complex(real(x) - y, imag(x));
}
 
inline complex operator - (double x, const complex& y){
    return complex(x - real(y), -imag(y));
}
 
//(a+bi)(c+di) = (ac-bd)+(ad+bc)i
inline complex operator * (const complex& x, const complex& y){
    return complex(real(x) * real(y) - imag(x) * imag(y),
        real(x) * imag(y) + imag(x) * real(y));
}
 
inline complex operator * (const complex& x, double y){
    return complex(real(x) * y, imag(x) * y);
}
 
inline complex operator * (double x, const complex& y){
    return complex(x * real(y), x * imag(y));
}
 
inline complex operator / (const complex& x, double y){
    return complex(real(x) / y, imag(x) / y);
}
 
//取正
inline complex operator + (const complex& x){
    return x;
}
 
inline complex operator - (const complex& x){
    return complex(-real(x), -imag(x));
}
 
inline bool operator == (const complex& x, const complex& y){
    return real(x) == real(y) && imag(x) == imag(y);
}
 
inline bool operator == (const complex& x, double y){
    return real(x) == y && imag(x) == 0;
}
 
inline bool operator == (double x, const complex& y){
    return x == real(y) && imag(y) == 0;
}
 
inline bool operator != (const complex& x, const complex& y){
    return real(x) != real(y) || imag(x) != imag(y);
}
 
inline bool operator != (const complex& x, double y){
    return real(x) != y || imag(x) != 0;
}
 
inline bool operator != (double x, const complex& y){
    return x != real(y) || imag(y) != 0;
}
 
/* ------------------ */
 
#include <cmath>
 
//复数的极坐标定义
inline complex polar(double r, double t){
    return complex(r * cos(t), r * sin(t));
}
 
//共轭复数
inline complex conj(const complex& x){
    return complex(real(x), -imag(x));
}
 
//复数的向量的模, 模值平方
inline double norm(const complex& x){
    return real(x) * real(x) + imag(x) * imag(x);
}
 
#endif // !__MYCOMPLEX_H__

  

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
//complex.cpp
#include<iostream>
#include"complex.h"
 
using namespace std;
 
ostream& operator << (ostream& os, const complex& x){
    return os << "(" << real(x) << ", " << imag(x) << "i)";
}
 
int main(){
    complex c1(3, 2);
    complex c2(5, 0);
    cout << c1 << endl;
    cout << c2 << endl;
 
    complex c3;
    complex c4(2);
    complex c31(c3);
    complex c41(c4);
    cout << c3 << " " << c4 << endl;
    cout << c31 << " " << c41 << endl;
 
     
    complex c5(7, 3);
    cout << c1 + c3 << endl;
 
    complex c6 = c1 * c5;
    cout << c5 << " " << c6 << " " << -c6 << endl;
     
    cout << c6 - 2 << endl;
    cout << c6 - c1 << endl;
    cout << c6 * 9 << endl;
    cout << c6 / 2 << endl;
     
    cout << conj(c6) << endl;
    cout << norm(c6) << endl;
    cout << polar(10, 4) << endl;
 
    cout << (c1 += c2) << endl;
 
    cout << (c1 == c2) << endl;
    cout << (c1 != c2) << endl;
    cout << +c2 << endl;
    cout << -c2 << endl;
 
    cout << (c2 - 2) << endl;
    cout << (5 + c2) << endl;
 
    return 0;
}

  执行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(3, 2i)
(5, 0i)
(0, 0i) (2, 0i)
(0, 0i) (2, 0i)
(3, 2i)
(7, 3i) (15, 23i) (-15, -23i)
(13, 23i)
(12, 21i)
(135, 207i)
(7.5, 11.5i)
(15, -23i)
754
(-6.53644, -7.56802i)
(8, 2i)
0
1
(5, 0i)
(-5, -0i)
(3, 0i)
(10, 0i)

  

posted @   karinto  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示