-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_operators.cpp
More file actions
123 lines (120 loc) · 2.26 KB
/
complex_operators.cpp
File metadata and controls
123 lines (120 loc) · 2.26 KB
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
#include<iostream>
#include<iomanip>
#include<cmath>
#include"complex_operators.h"
//单个复数的运算符重载
//加号+
Complex operator + (Complex& c1, Complex& c2)
{
Complex c3;
c3.re = c1.re + c2.re;
c3.im = c1.im + c2.im;
return c3;
}
Complex operator + (double& d1, Complex& c2)
{
Complex c3;
c3.re = d1 + c2.re;
c3.im = c2.im;
return c3;
}
Complex operator + (Complex& c1, double& d2)
{
Complex c3;
c3.re = c1.re + d2;
c3.im = c1.im;
return c3;
}
//减号-
Complex operator - (Complex& c1, Complex& c2)
{
Complex c3;
c3.re = c1.re - c2.re;
c3.im = c1.im - c2.im;
return c3;
}
Complex operator - (double& d1, Complex& c2)
{
Complex c3;
c3.re = d1 - c2.re;
c3.im = -c2.im;
return c3;
}
Complex operator - (Complex& c1, double& d2)
{
Complex c3;
c3.re = c1.re - d2;
c3.im = c1.im;
return c3;
}
//乘号*
Complex operator * (Complex& c1, Complex& c2)
{
Complex c3;
c3.re = c1.re * c2.re - c1.im * c2.im;
c3.im = c1.re * c2.im + c1.im * c2.re;
return c3;
}
Complex operator * (double& d1, Complex& c2)
{
Complex c3;
c3.re = d1 * c2.re;
c3.im = d1 * c2.im;
return c3;
}
Complex operator * (Complex& c1, double& d2)
{
Complex c3;
c3.re = c1.re * d2;
c3.im = c1.im * d2;
return c3;
}
//除号/
Complex operator / (Complex& c1, Complex& c2)
{
Complex c3;
double temp = c2.re * c2.re + c2.im * c2.im;
c3.re = (c1.re * c2.re + c1.im * c2.im) / temp;
c3.im = (c1.im * c2.re - c1.re * c2.im) / temp;
return c3;
}
Complex operator / (double& d1, Complex& c2)
{
Complex c3;
double temp = c2.re * c2.re + c2.im * c2.im;
c3.re = d1 * c2.re / temp;
c3.im = -d1 * c2.im / temp;
return c3;
}
Complex operator / (Complex& c1, double& d2)
{
Complex c3(c1.re / d2, c1.im / d2);
return c3;
}
//输出<<,输入>>
ostream& operator << (ostream& out, Complex& c1)
{
if (c1.im >= 0)
out << c1.re << "+" << c1.im << "j";
else if (c1.im < 0)
out << c1.re << c1.im << "j";
return out;
}
istream& operator >> (istream& in, Complex& c1) //先输入实部,后输入虚部
{
in >> c1.re >> c1.im;
return in;
}
//共轭!
Complex Complex::operator !()
{
Complex c1;
c1.re = this->re;
c1.im = -(this->im);
return c1;
}
//求模值
double Complex::modulus()
{
return sqrt(re * re + im * im);
}