컴퓨터 언어 정리/C++ 언어

22 string 클래스 - 직접 일부 구현

photoner 2020. 10. 12. 14:18
728x90
반응형

-

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
#include <iostream>
#include <string>
#include <cstring>
 
namespace mystd
{
    using std::cout;
    using std::cin;
    using std::endl;
    using std::printf;
    using std::ostream;
    using std::istream;
 
    class string
    {
    private:
        char * str;
        int str_len;
    public:
        string();
        string(const string& r_string);
        string(const char * str);
        ~string();
 
        string& operator=(const string& r_string);
        string& operator=(const char * str);
        const string operator+(const char * str);
        const string operator+(const string& r_string);
        string& operator+=(const char * str);
        string& operator+=(const string& r_string);
        bool operator== (const string& r_string);
        bool operator== (const char * str);
 
        friend ostream& operator<<(ostream& os, const string& r_string);
        friend const string operator+(const char * str, const string& r_string);
        friend char * operator+=(char * str, const string& r_string);
        friend bool operator==(const char * str, const string& r_string);
    };
    istream& operator>> (istream& is, string& r_string);
    ostream& operator<< (ostream& os, const string& r_string);
    const string operator+(const char * str, const string& r_string);
    char * operator+=(char * str, const string& r_string);
    bool operator==(const char * str, const string& r_string);
};
 
using namespace mystd;
 
int main(void)
{
    string str1 = "I like "// 복사 생성자
    string str2 = "string class"// 복사 생성자
    string str3 = str1 + str2; // 복사 생성자, + 연산자 오버로딩
 
    cout<<"------ str1 ------"<<endl;
    cout<<str1<<endl// << 연산자 오버로딩 전역
    cout<<"------ str2 ------"<<endl;
    cout<<str2<<endl// << 연산자 오버로딩 전역
    cout<<"------ str3 ------"<<endl;
    cout<<str3<<endl// << 연산자 오버로딩 전역
 
    string str4;
    str4 = "HELLO";
    cout<<"------ str4 ------"<<endl;
    cout<<str4<<endl;
 
    string str5;
    str5 = str3 + str4 ;
    cout<<"------ str5 ------"<<endl;
    cout<<str5<<endl;
 
    string str6;
    str6 = str4 + " AAA" ;
    cout<<"------ str6 ------"<<endl;
    cout<<str6<<endl;
 
    string str7;
    str7 =  "AAA " + str4 ;
    cout<<"------ str7 ------"<<endl;
    cout<<str7<<endl;
 
    string str8 = "asdf";
    string str9 = "fdsa";
 
    str8 += str9;
    str9 += "asdf";
    cout<<"------ str8 ------"<<endl;
    cout<<str8<<endl;
    cout<<"------ str9 ------"<<endl;
    cout<<str9<<endl;
 
    cout<<"------ str11 ------"<<endl;
    string str10="222222222";
    char str11[30= "fdjskalfjklsaf";
    str11+=str10;
    printf("%s\n",str11+=str10);
 
    string str12 = "ZXC1";
    string str13 = "ZXC";
    if(str12 == str13)
        cout<<"동일한 문자열이다1"<<endl;
    else
        cout<<"동일하지 않은 문자열이다1"<<endl;
 
    char str14[10= "ZXC";
    if(str12 == str14)
        cout<<"동일한 문자열이다2"<<endl;
    else
        cout<<"동일하지 않은 문자열이다2"<<endl;
 
    if(str14 == str12)
        cout<<"동일한 문자열이다3"<<endl;
    else
        cout<<"동일하지 않은 문자열이다3"<<endl;
 
    cout<<"문자열 입력 : ";
    cin>>str4; // >> 연산자 오버로딩 전역
    cout<<"입력한 문자열 : "<<str4<<endl// << 연산자 오버로딩 전역
 
    return 0;
}
 
 
mystd::string::string() : str(NULL), str_len(0) { }
mystd::string::string(const string& r_string) : str_len(r_string.str_len)
{
    this->str = new char[r_string.str_len+1];
    strcpy(this->str, r_string.str);
}
mystd::string::string(const char * str) : str_len(strlen(str))
{
    this->str = new char[strlen(str)+1];
    strcpy(this->str, str);
}
mystd::string::~string()
{
    delete []this->str;
}
 
string& mystd::string::operator=(const string& r_string)
{
    this->str_len = r_string.str_len;
 
    if(this->str == NULL)
    {
        this->str = new char[this->str_len+1];
    }
    else
    {
        delete []this->str;
        this->str = new char[this->str_len+1];
    }
    strcpy(this->str, r_string.str);
    return *this;
}
 
string& mystd::string::operator=(const char * str)
{
    this->str_len = strlen(str);
 
    if(this->str == NULL)
    {
        this->str = new char[this->str_len+1];
    }
    else
    {
        delete []this->str;
        this->str = new char[this->str_len+1];
    }
 
    strcpy(this->str, str);
    return *this;
}
const string mystd::string::operator+(const char * str)
{
    char * tmp_str = new char[this->str_len + strlen(str) + 1];
    string tmp;
 
    strcpy(tmp_str, this->str);
    strcpy(tmp_str + this->str_len, str);
 
    tmp = tmp_str; // 대입 연산자 오버로딩
    delete []tmp_str;
    return tmp;
}
const string mystd::string::operator+(const string& r_string)
{
    char * tmp_str = new char[this->str_len + r_string.str_len + 1];
    string tmp;
 
    strcpy(tmp_str, this->str);
    strcpy(tmp_str + this->str_len, r_string.str);
 
    tmp = tmp_str; // 대입 연산자 오버로딩
    delete []tmp_str;
    return tmp;
}
string& mystd::string::operator+=(const char * str)
{
    int tmp_strlen = this->str_len + strlen(str);
    char * tmp_str = new char[tmp_strlen + 1];
 
    strcpy(tmp_str, this->str);
    strcpy(tmp_str+this->str_len, str);
 
    if(this->str == NULL)
        this->str = new char[tmp_strlen + 1];
    else
    {
        delete []this->str;
        this->str = new char[tmp_strlen + 1];
    }
    strcpy(this->str, tmp_str);
    this->str_len = tmp_strlen;
 
    delete []tmp_str;
    return *this;
}
string& mystd::string::operator+=(const string& r_string)
{
    int tmp_strlen = this->str_len + r_string.str_len;
    char * tmp_str = new char[tmp_strlen + 1];
 
    strcpy(tmp_str, this->str);
    strcpy(tmp_str+this->str_len, r_string.str);
 
    if(this->str == NULL)
        this->str = new char[tmp_strlen + 1];
    else
    {
        delete []this->str;
        this->str = new char[tmp_strlen + 1];
    }
    strcpy(this->str, tmp_str);
    this->str_len = tmp_strlen;
 
    delete []tmp_str;
    return *this;
}
bool mystd::string::operator== (const string& r_string)
{
    if(!strcmp(this->str, r_string.str))
        return true;
    else
        return false;
}
bool mystd::string::operator== (const char * str)
{
    if(!strcmp(this->str, str))
        return true;
    else
        return false;
}
 
istream& mystd::operator>> (istream& is, string& r_string)
{
    char str[100];
    is>>str;
    r_string = string(str);
    return is;
}
ostream& mystd::operator<< (ostream& os, const string& r_string)
{
    os<<r_string.str;
    return os;
}
const string mystd::operator+(const char * str, const string& r_string)
{
    char * tmp_str = new char[r_string.str_len + strlen(str) + 1];
    string tmp;
 
    strcpy(tmp_str, str);
    strcpy(tmp_str + strlen(str), r_string.str);
 
    tmp = tmp_str; // 대입 연산자 오버로딩
    delete []tmp_str;
    return tmp;
}
char * mystd::operator+=(char * str, const string& r_string)
{
    int i = 0;
    int tmp_strlen = strlen(str) + r_string.str_len;
    char * tmp_str = new char[tmp_strlen + 1];
 
    strcpy(tmp_str, str);
    strcpy(tmp_str + strlen(str), r_string.str);
 
    while(str[i++]!=0); // str의 사이즈를 구한다.(i-2)
    strncpy(str,tmp_str,i-2); // str의 기존 사이즈만큼 복사한다. 짤리겠지.
    delete []tmp_str;
    return str;
}
bool mystd::operator==(const char * str, const string& r_string)
{
    if(!strcmp(str, r_string.str))
        return true;
    else
        return false;
}
cs

-

 

직접 구현해보았으나 설명 및 풀이를 시간이 나면 진행을 해보겠다.

728x90
반응형