-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.h
93 lines (79 loc) · 1.92 KB
/
complex.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
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
#pragma once
#include <cmath>
#include <sstream>
#include <iostream>
// 复数类
class complex
{
public:
float real;
float imag;
// 构造函数
complex(float real, float imag);
// 模值
float modulus() const
{
return sqrtf(powf(this->real, 2) + powf(this->imag, 2));
}
// 共轭
complex conj() const {
return complex(real, -imag);
}
// 重载复数运算符
complex operator+(const complex& c);
complex operator-(const complex& c);
complex operator*(const complex& c);
complex operator/(const complex& c);
complex operator+=(const complex& c) { return *this = *this + c; }
complex operator-=(const complex& c) { return *this = *this - c; }
complex operator=(const complex& c) { this->real = c.real; this->imag = c.imag; return *this; }
// 重载==运算符
bool operator==(const complex& c) const {
return this->real == c.real && this->imag == c.imag;
}
// 重载输出运算符
friend std::ostream& operator<<(std::ostream& os, const complex& c)
{
std::ostringstream temp;
if (c.real == 0 && c.imag == 0)
{
temp << 0;
}
else
{
if (c.real != 0)
{
temp << c.real;
}
if (c.imag != 0)
{
temp << (c.imag > 0 ? "+j" : "-j") << abs(c.imag);
}
}
os << temp.str();
return os;
}
};
complex::complex(float real, float imag)
{
this->real = real;
this->imag = imag;
}
inline complex complex::operator+(const complex& c)
{
return complex(this->real + c.real, this->imag + c.imag);
}
inline complex complex::operator-(const complex& c)
{
return complex(this->real - c.real, this->imag - c.imag);
}
inline complex complex::operator*(const complex& c)
{
return complex(this->real * c.real - this->imag * c.imag, this->real * c.imag + this->imag * c.real);
}
inline complex complex::operator/(const complex& c)
{
float denominator = c.real * c.real + c.imag * c.imag;
return complex((this->real * c.real + this->imag * c.imag) / denominator,
(this->imag * c.real - this->real * c.imag) / denominator);
}