-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining5_1.cpp
More file actions
354 lines (311 loc) · 9.4 KB
/
training5_1.cpp
File metadata and controls
354 lines (311 loc) · 9.4 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// 연산자 오버로딩
/*
c++에서는 사용자 정의 연산자를 사용가능
(리턴타입) operator(연산자) (연산자가 받는 인자)
bool MyString::operator==(MyString& str){
return !compare(str);
}
str1 == str2
str1.operator==(str2)로 변환됨.
사칙연산시 리턴타입이 중요
class Num이 있을 때
Num& operator+ (const Num& n){
num+=n.num;
return *this;
}
이면 레퍼런스 반환으로 속도는 더 빨라지지만
b+a+b를 했을때
(b.+(a)).+(b)가 일어나므로
b에 a가 더해지게 돼어 +(b) 시에 사실상 a가 한번 더 더해진다
이런 결과를 막기위해 사칙연산시 리턴타입은 값을 반환하는 것이 좋다.
대입연산시 에는 레퍼런스를 반환하는 것이좋다.
불필요한 복사를 방지한다.
*/
#include <string.h>
#include <iostream>
class MyString
{
char *string_content; // 문자열 데이터를 가리키는 포인터
int string_length; // 문자열 길이
int memory_capacity; // 현재 할당된 용량
public:
// 문자 하나로 생성
MyString(char c);
// 문자열로 부터 생성
MyString(const char *str);
// 복사 생성자
MyString(const MyString &str);
~MyString();
int length() const;
int capacity() const;
void reserve(int size);
void print() const;
void println() const;
char at(int i) const;
int compare(MyString &str);
bool operator==(MyString &str);
};
MyString::MyString(char c)
{
string_content = new char[1];
string_content[0] = c;
memory_capacity = 1;
string_length = 1;
}
MyString::MyString(const char *str)
{
string_length = strlen(str);
memory_capacity = string_length;
string_content = new char[string_length];
for (int i = 0; i != string_length; i++)
string_content[i] = str[i];
}
MyString::MyString(const MyString &str)
{
string_length = str.string_length;
string_content = new char[string_length];
for (int i = 0; i != string_length; i++)
string_content[i] = str.string_content[i];
}
MyString::~MyString() { delete[] string_content; }
int MyString::length() const { return string_length; }
void MyString::print() const
{
for (int i = 0; i != string_length; i++)
std::cout << string_content[i];
}
void MyString::println() const
{
for (int i = 0; i != string_length; i++)
std::cout << string_content[i];
std::cout << std::endl;
}
int MyString::capacity() const { return memory_capacity; }
void MyString::reserve(int size)
{
if (size > memory_capacity)
{
char *prev_string_content = string_content;
string_content = new char[size];
memory_capacity = size;
for (int i = 0; i != string_length; i++)
string_content[i] = prev_string_content[i];
delete[] prev_string_content;
}
// 만일 예약하려는 size 가 현재 capacity 보다 작다면
// 아무것도 안해도 된다.
}
char MyString::at(int i) const
{
if (i >= string_length || i < 0)
return 0;
else
return string_content[i];
}
int MyString::compare(MyString &str)
{
// (*this) - (str) 을 수행해서 그 1, 0, -1 로 그 결과를 리턴한다
// 1 은 (*this) 가 사전식으로 더 뒤에 온다는 의미. 0 은 두 문자열
// 이 같다는 의미, -1 은 (*this) 사 사전식으러 더 앞에 온다는 의미이다.
for (int i = 0; i < std::min(string_length, str.string_length); i++)
{
if (string_content[i] > str.string_content[i])
return 1;
else if (string_content[i] < str.string_content[i])
return -1;
}
// 여기 까지 했는데 끝나지 않았다면 앞 부분 까지 모두 똑같은 것이 된다.
// 만일 문자열 길이가 같다면 두 문자열은 아예 같은 문자열이 된다.
if (string_length == str.string_length)
return 0;
// 참고로 abc 와 abcd 의 크기 비교는 abcd 가 더 뒤에 오게 된다.
else if (string_length > str.string_length)
return 1;
return -1;
}
bool MyString::operator==(MyString &str)
{
return !compare(str); // str 과 같으면 compare 에서 0 을 리턴한다.
}
class Complex
{
double real, img;
public:
Complex(double real, double img) : real(real), img(img) {}
Complex(const char *str);
Complex operator+(const Complex &c) const;
Complex operator+(const char *c) const;
Complex operator-(const Complex &c) const;
Complex operator-(const char *c) const;
Complex operator*(const Complex &c) const;
Complex operator*(const char *c) const;
Complex operator/(const Complex &c) const;
Complex operator/(const char *c) const;
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);
void show()
{
std::cout << real << "+" << img << "i" << std::endl;
}
double get_number(const char *str, int from, int to) const;
};
Complex Complex::operator+(const Complex &c) const
{
Complex temp(real + c.real, img + c.img);
return temp;
}
Complex Complex::operator-(const Complex &c) const
{
Complex temp(real - c.real, img - c.img);
return temp;
}
Complex Complex::operator*(const Complex &c) const
{
Complex temp(real * c.real - img * c.img, real * c.img + img * c.real);
return temp;
}
Complex Complex::operator/(const Complex &c) const
{
Complex temp(
(real * c.real + img * c.img) / (c.real * c.real + c.img * c.img),
(img * c.real - real * c.img) / (c.real * c.real + c.img * c.img));
return temp;
}
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(const char *str) //사실 이 생성자를 만들어주면 +-*/도 자동으로 가능해진다. 자체적으로 변환해서 사칙연산하기 때문
//단 연산자 오버로딩시에 확실히 인자를 const로 받아야한다.
{
// 입력 받은 문자열을 분석하여 real 부분과 img 부분을 찾아야 한다.
// 문자열의 꼴은 다음과 같습니다 "[부호](실수부)(부호)i(허수부)"
// 이 때 맨 앞의 부호는 생략 가능합니다. (생략시 + 라 가정)
int begin = 0, end = strlen(str);
img = 0.0;
real = 0.0;
// 먼저 가장 기준이 되는 'i' 의 위치를 찾는다.
int pos_i = -1;
for (int i = 0; i != end; i++)
{
if (str[i] == 'i')
{
pos_i = i;
break;
}
}
// 만일 'i' 가 없다면 이 수는 실수 뿐이다.
if (pos_i == -1)
{
real = get_number(str, begin, end - 1);
return;
}
// 만일 'i' 가 있다면, 실수부와 허수부를 나누어서 처리하면 된다.
real = get_number(str, begin, pos_i - 1);
img = get_number(str, pos_i + 1, end - 1);
if (pos_i >= 1 && str[pos_i - 1] == '-')
img *= -1.0;
}
Complex Complex::operator+(const char *str) const
{
Complex temp(str);
return (*this) + temp;
}
Complex Complex::operator-(const char *str) const
{
Complex temp(str);
return (*this) - temp;
}
Complex Complex::operator*(const char *str) const
{
Complex temp(str);
return (*this) * temp;
}
Complex Complex::operator/(const char *str) const
{
Complex temp(str);
return (*this) / temp;
}
double Complex::get_number(const char *str, int from, int to) const
{
bool minus = false;
if (from > to)
return 0;
if (str[from] == '-')
minus = true;
if (str[from] == '-' || str[from] == '+')
from++; // 부호 붙였을때 다음자리부터 읽음.
double num = 0.0;
double decimal = 1.0;
bool integer_part = true;
for (int i = from; i <= to; i++)
{
if (isdigit(str[i]) && integer_part)
{
num *= 10.0;
num += (str[i] - '0');
}
else if (str[i] == '.')
integer_part = false;
else if (isdigit(str[i]) && !integer_part)
{
decimal /= 10.0;
num += ((str[i] - '0') * decimal);
}
else
break;
}
if (minus) num *= -1.0;
return num;
}
int main()
{
MyString str1("a word");
MyString str2("sentence");
MyString str3("sentence");
if (str1 == str2)
std::cout << "str1 와 str2 같다." << std::endl;
else
std::cout << "st1 와 str2 는 다르다." << std::endl;
if (str2 == str3)
std::cout << "str2 와 str3 는 같다." << std::endl;
else
std::cout << "st2 와 str3 는 다르다" << std::endl;
Complex num1(1, 3);
Complex num2(2, 4);
Complex num3 = num1 + num2;
Complex num4 = num1 / num2;
num3.show();
num4.show();
num1 = num2 = num3; // num1.=(num2.=(num3)) 동일 연산시 우측부터 처리
// n1 =a n2= b n3=c였을때
// n1 n2 n3 = c가된다.
num1.show();
num2.show();
num3.show();
}