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
| //运算符重载
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
// 构造函数
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
// 赋值运算符重载
Complex& operator=(const Complex& other) {
if (this != &other) {
real = other.real;
imag = other.imag;
}
return *this;
}
// 成员函数版本的算术运算符重载
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex operator*(const Complex& other) const {
return Complex(real * other.real - imag * other.imag,
real * other.imag + imag * other.real);
}
Complex operator/(const Complex& other) const {
double denominator = other.real * other.real + other.imag * other.imag;
if (denominator == 0.0) {
throw std::invalid_argument("Division by zero");
}
return Complex((real * other.real + imag * other.imag) / denominator,
(imag * other.real - real * other.imag) / denominator);
}
// 成员函数版本的自增自减运算符重载
// 前置自增
Complex& operator++() {
++real;
return *this;
}
// 后置自增
Complex operator++(int) {
Complex temp = *this;
++real;
return temp;
}
// 前置自减
Complex& operator--() {
--real;
return *this;
}
// 后置自减
Complex operator--(int) {
Complex temp = *this;
--real;
return temp;
}
// 友元函数版本的算术运算符重载
friend Complex operator+(const Complex& a, const Complex& b);
friend Complex operator-(const Complex& a, const Complex& b);
friend Complex operator*(const Complex& a, const Complex& b);
friend Complex operator/(const Complex& a, const Complex& b);
// 友元函数版本的输入输出运算符重载
friend std::ostream& operator<<(std::ostream& os, const Complex& c);
friend std::istream& operator>>(std::istream& is, Complex& c);
};
// 友元函数版本的算术运算符实现
Complex operator+(const Complex& a, const Complex& b) {
return Complex(a.real + b.real, a.imag + b.imag);
}
Complex operator-(const Complex& a, const Complex& b) {
return Complex(a.real - b.real, a.imag - b.imag);
}
Complex operator*(const Complex& a, const Complex& b) {
return Complex(a.real * b.real - a.imag * b.imag,
a.real * b.imag + a.imag * b.real);
}
Complex operator/(const Complex& a, const Complex& b) {
double denominator = b.real * b.real + b.imag * b.imag;
if (denominator == 0.0) {
throw std::invalid_argument("Division by zero");
}
return Complex((a.real * b.real + a.imag * b.imag) / denominator,
(a.imag * b.real - a.real * b.imag) / denominator);
}
// 友元函数版本的输入输出运算符实现
std::ostream& operator<<(std::ostream& os, const Complex& c) {
os << c.real << " + " << c.imag << "i";
return os;
}
std::istream& operator>>(std::istream& is, Complex& c) {
is >> c.real >> c.imag;
return is;
}
int main() {
Complex a(3.0, 4.0), b(1.0, 2.0);
Complex result;
// 成员函数版本的算术运算
result = a + b;
std::cout << "a + b = " << result << std::endl;
result = a - b;
std::cout << "a - b = " << result << std::endl;
result = a * b;
std::cout << "a * b = " << result << std::endl;
result = a / b;
std::cout << "a / b = " << result << std::endl;
// 前置和后置自增自减
std::cout << "a before ++: " << a << std::endl;
std::cout << "a++: " << a++ << std::endl;
std::cout << "a after a++: " << a << std::endl;
std::cout << "++a: " << ++a << std::endl;
std::cout << "a before --: " << a << std::endl;
std::cout << "a--: " << a-- << std::endl;
std::cout << "a after a--: " << a << std::endl;
std::cout << "--a: " << --a << std::endl;
// 友元函数版本的算术运算
result = operator+(a, b);
std::cout << "operator+(a, b) = " << result << std::endl;
result = operator-(a, b);
std::cout << "operator-(a, b) = " << result << std::endl;
result = operator*(a, b);
std::cout << "operator*(a, b) = " << result << std::endl;
result = operator/(a, b);
std::cout << "operator/(a, b) = " << result << std::endl;
// 输入输出运算符
std::cout << "Enter a new value for a (real and imaginary parts): ";
std::cin >> a;
std::cout << "New value of a: " << a << std::endl;
return 0;
}
|