-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_1_Overloading.cpp
More file actions
271 lines (249 loc) · 4.55 KB
/
6_1_Overloading.cpp
File metadata and controls
271 lines (249 loc) · 4.55 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
#include <stdio.h>
#include <chrono>
int GetStrLen(const char* str) // C 문자열 길이 반환
{
int len = 0;
while (str[len] != '\0')
{
++len;
}
return len;
}
class MyString
{
int len = 0;
char* data = nullptr;
int mem_capacity = 0;
public:
MyString();
MyString(char c); // 문자로 문자열 생성
MyString(const char* origin); // C 문자열로 생성
MyString(const MyString& origin); // copy constructor
~MyString();
int GetLen() const; // return len
int GetCapacity() const; // return capacity
void Reserve(int size); // reallocation
void PrintStr() const; // print
void PrintStr(int idx) const; // print from idx
char At(int idx) const; // 범위 내 문자 반환
bool IsEqual(const MyString& compare) const;// 문자열 같은지 비교
bool IsEqual(const char* compare) const;
int CmpSize(const MyString& compare) const; // 문자열 사전 순 비교
int CmpSize(const char* compare) const;
bool operator==(MyString& compare) const;
};
MyString::MyString() : len(0), data(nullptr), mem_capacity(0) {}
MyString::MyString(char c)
{
mem_capacity = len = 1;
data = new char[1];
data[0] = c;
}
MyString::MyString(const char* origin)
{
mem_capacity = len = GetStrLen(origin);
data = new char[len];
for (int i = 0;i < len; ++i)
{
data[i] = origin[i];
}
}
MyString::MyString(const MyString& origin)
{
len = origin.GetLen();
mem_capacity = origin.GetCapacity();
if (mem_capacity < len)
{
mem_capacity = len;
}
data = new char[mem_capacity];
for (int i = 0; i < len; ++i)
{
data[i] = origin.data[i];
}
}
MyString::~MyString()
{
if (data)
{
delete[] data;
}
}
int MyString::GetLen() const
{
return len;
}
int MyString::GetCapacity() const
{
return mem_capacity;
}
void MyString::Reserve(int size)
{
if (size <= mem_capacity)
{
return;
}
mem_capacity = size;
char* temp = data;
data = new char[mem_capacity];
for (int i = 0; i < len; ++i)
{
data[i] = temp[i];
}
if (temp)
{
delete[] temp;
}
}
void MyString::PrintStr() const
{
for (int i = 0; i < len; ++i)
{
printf("%c", data[i]);
}
printf("\n");
}
void MyString::PrintStr(int idx) const
{
if (idx < 0 || idx >= len)
{
return;
}
for (int i = idx; i < len; ++i)
{
printf("%c", data[i]);
}
printf("\n");
}
char MyString::At(int idx) const
{
if (idx < 0 || idx >= len)
{
return NULL;
}
return data[idx];
}
bool MyString::IsEqual(const MyString& compare) const// 문자열 같은지 비교
{
if (compare.GetLen() != len)
{
return false;
}
for (int i = 0; i < len; ++i)
{
if (data[i] != compare.data[i])
{
return false;
}
}
return true;
}
bool MyString::IsEqual(const char* compare) const
{
for (int i = 0; i < len; ++i)
{
if ((compare[i] == '\0' && len != 0) || data[i] != compare[i])
{
return false;
}
}
if (compare[len] != '\0')
{
return false;
}
return true;
}
int MyString::CmpSize(const MyString& compare) const// 문자열 사전순 비교
{
int cplen = compare.GetLen();
int minlen;
if (cplen < len)
{
minlen = cplen;
}
else
{
minlen = len;
}
for (int i = 0; i < minlen; ++i)
{
if (data[i] < compare.data[i])
{
return -1;
}
if (data[i] > compare.data[i])
{
return 1;
}
}
if (len == cplen)
{
return 0;
}
else if (len < cplen)
{
return -1;
}
return 1;
}
int MyString::CmpSize(const char* compare) const
{
for (int i = 0; i < len; ++i)
{
if (data[i] < compare[i])
{
return -1;
}
if (data[i] > compare[i])
{
return 1;
}
}
int cplen = GetStrLen(compare);
if (len == cplen)
{
return 0;
}
return -1;
}
bool MyString::operator==(MyString& compare) const
{ // little performance difference
return IsEqual(compare); // or return !CmpSize(compare);
}
int main(void)
{
MyString s1("a word");
MyString s2("sentence");
MyString s3("sentence");
if (s1 == s2)
{
printf("s1 == s2\n");
}
else
{
printf("s1 != s2\n");
}
if (s2 == s3)
{
printf("s2 == s3\n");
}
else
{
printf("s2 != s3\n");
}
// Performance: IsEqual vs CmpSize
MyString str1("Hello, world!");
MyString str2("Hello, world!");
// IsEqual()
auto start = std::chrono::high_resolution_clock::now();
bool result = str1 == str2;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_time = end - start;
printf("IsEqual() elapsed time: %f seconds\n", elapsed_time.count());
// CmpSize()
start = std::chrono::high_resolution_clock::now();
result = str1.CmpSize(str2) == 0;
end = std::chrono::high_resolution_clock::now();
elapsed_time = end - start;
printf("CmpSize() elapsed time: %f seconds\n", elapsed_time.count());
}