-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_1_ComplexNumber.cpp
More file actions
285 lines (261 loc) · 6.18 KB
/
6_1_ComplexNumber.cpp
File metadata and controls
285 lines (261 loc) · 6.18 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <stdio.h>
#include <ctype.h>
#include <iostream>
int GetLen(const char* str)
{
int i = -1;
while (str[++i] != '\0') {};
return i;
}
class Complex
{
double real = 0;
double img = 0;
double GetReal(const char* str, int begin, int end) const;
public:
Complex(double real, double img) : real(real), img(img) {};
Complex(const Complex& cp) : real(cp.real), img(cp.img) {};
Complex Plus(const Complex& p) const;
Complex Plus(double preal, double pimg) const;
Complex operator+(const Complex& p);
Complex operator-(const Complex& p);
Complex operator*(const Complex& p);
Complex operator/(const Complex& p);
Complex& operator=(const Complex& c);
Complex& operator+=(const Complex& c); // operator+ 사용
Complex& operator-=(const Complex& c); // operator- 사용
Complex& operator*=(const Complex& c); // operator* 사용
Complex& operator/=(const Complex& c); // operator/ 사용
//Complex operator+(const char* str); // 문자열 -> 복소수 생성자, operator+ 사용
//Complex operator-(const char* str); // 문자열 -> 복소수 생성자, operator- 사용
//Complex operator*(const char* str); // 문자열 -> 복소수 생성자, operator* 사용
//Complex operator/(const char* str); // 문자열 -> 복소수 생성자, operator/ 사용
Complex(const char* str); // 문자열 -> 복소수 생성자
void Print() const;
friend Complex operator+(const Complex& p1, const Complex& p2);
friend std::ostream& operator<<(std::ostream& os, const Complex& c);
};
Complex Complex::Plus(double preal, double pimg) const
{
Complex temp(*this);
temp.real += preal;
temp.img += pimg;
return temp;
}
Complex Complex::Plus(const Complex& p) const
{
Complex temp(*this);
temp.real += p.real;
temp.img += p.img;
return temp;
}
// const (Rvalue) 로 반환하면 타입 불일치로 연속 연산 불가능
// 매개변수로 받을 때 타입: const Complex& => 참조할 수 있는 non-const Lvalue여야 함
Complex Complex::operator+(const Complex& p)
{
Complex temp(real + p.real, img + p.img);
return temp;
}
Complex Complex::operator-(const Complex& p)
{
Complex temp(real - p.real, img - p.img);
return temp;
}
Complex Complex::operator*(const Complex& p)
{
Complex temp(real * p.real - img * p.img, real * p.img + img * p.real);
return temp;
}
Complex Complex::operator/(const Complex& p)
{
Complex temp((real * p.real + img * p.img) / (p.real * p.real + p.img * p.img),
(p.real * img - real * p.img) / (p.real * p.real + p.img * p.img));
return temp;
}
// 마찬가지로, non-const Lvalue여야 확장 참조 연산(ex 비교연산)도 가능하다.
// 깊은 복사가 필요한 멤버 변수가 없으므로, 선언이나 정의 없이 default 사용 가능
Complex& Complex::operator=(const Complex& c)
{
real = c.real;
img = c.img;
return *this;
}
Complex& Complex::operator+=(const Complex& c)
{
*this = *this + c;
return *this;
}
Complex& Complex::operator-=(const Complex& c)
{
*this = *this - c;
return *this;
}
Complex& Complex::operator*=(const Complex& c)
{
*this = *this * c;
return *this;
}
Complex& Complex::operator/=(const Complex& c)
{
*this = *this / c;
return *this;
}
//Complex Complex::operator+(const char* str)
//{
// Complex temp(str);
// return *this + temp;
//}
//Complex Complex::operator-(const char* str)
//{
// Complex temp(str);
// return *this - temp;
//}
//Complex Complex::operator*(const char* str)
//{
// Complex temp(str);
// return *this * temp;
//}
//Complex Complex::operator/(const char* str)
//{
// Complex temp(str);
// return *this / temp;
//}
// 문자열 -> 복소수 생성자
Complex::Complex(const char* str)
{
int begin = 0, len = GetLen(str);
real = 0, img = 0;
if (len == 0)
{
return;
}
int pos_i = -1;
for (int i = 0; i < len; ++i)
{
if (str[i] == 'i')
{
pos_i = i;
break;
}
}
// i가 없을 때는 실수부만 처리
if (pos_i == -1)
{
real = GetReal(str, begin, len - 1);
}
// i 전은 실수부, 후는 허수부로 처리, i 직전 부호값 처리
else
{
real = GetReal(str, begin, pos_i - 1);
img = GetReal(str, pos_i + 1, len - 1);
if (pos_i >= 1 && str[pos_i - 1] == '-')
{
img *= -1;
}
}
}
// 문자열 -> 실수 읽기
double Complex::GetReal(const char* str, int begin, int end) const
{
if (begin > end)
{
return 0;
}
bool minus = false;
if (str[begin] == '-')
{
minus = true;
}
if (str[begin] == '-' || str[begin] == '+')
{
++begin;
}
bool is_int = true;
double num = 0, decimal = 1;
for (int i = begin; i <= end; ++i)
{
if (isdigit(str[i]) && is_int)
{
num *= 10;
num += str[i] - '0';
}
// 첫 . 만 가능
else if (str[i] == '.' && is_int)
{
is_int = false;
}
else if (isdigit(str[i]) && !is_int)
{
decimal /= 10;
num += decimal * (str[i] - '0');
}
else
{
break;
}
}
// 음수는 마지막에 처리해줘야 수 읽기의 덧셈 연산이 정상적으로 수행됨
if (minus)
{
num *= -1;
}
return num;
}
void Complex::Print() const
{
printf("(%f, %f)\n", real, img);
}
// 전역 함수
Complex operator+(const Complex& p1, const Complex& p2)
{
// 객체 총 2번 생성 ( O )
// Complex temp(p1);
// return temp.operator+(p2);
// 무한 재귀 호출 (operator+(p1, p2)) ( X )
// return p1 + p2;
// p1 is Read-Only (const) ( X )
// return p1.operator+(p2);
// 따라서 const 객체의 멤버 함수 호출 시, const 함수만 호출할 수 있다.
// friend 선언 시 접근 가능, 객체 총 1번 생성 ( O )
Complex temp(p1.real + p2.real, p1.img + p2.img);
return temp;
}
std::ostream& operator<<(std::ostream& os, const Complex& c)
{
os << "(" << c.real << ", " << c.img << ")" ;
return os;
}
int main(void)
{
Complex a(1, 2);
Complex b(3, -2);
Complex c(0, 0);
c = a * b + a / b + a + b;
c.Print();
printf("\n");
Complex a1 = b; // Copy Constructor
Complex a2(0, 0);
a2 = b; // Assignment Operator
a += b;
a.Print();
b.Print();
printf("\n");
a = Complex(0, 0);
a.Print();
// "-1.1 + i3.9.23" 일 경우 img => 3.9
a = a + "-1.1 + i3.923";
a.Print();
a = a - "1.2 -i1.823";
a.Print();
a = a * "2.3+i22";
a.Print();
a = a / "-12+i55";
a.Print();
printf("\n");
a = Complex(0, 0);
a.Print();
a = "-1.1 + i3.923" + a;
a.Print();
a = "-1.1 + i3.923" + a;
std::cout << "value of a: " << a << std::endl;
}