본문 바로가기

c++

c++ string을 나만의 것으로 구현

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
#include<iostream>
#include<string>
#include<cstring>
#pragma warning ( disable : 4996 )
using namespace std;
 
class Mystring {
 
    char *a; // 문자열을 만들기 위해서 포인터를 사용한다.
    int len// 문자열의 길이를 잡기 위해서 사용한다.
 
public :
 
    Mystring() {
 
        this ->= new char[50];
        len = 0;
 
    }
    Mystring(const char *ch) { // 
        
 
        len = strlen(ch);
        a = new char[len+1];
        strcpy_s(a,len+1,ch);
 
    }
 
    Mystring(int num, const char ch) { // 
 
        len = num;
        a = new char[len + 1];
 
        for (int i = 0; i < len; i++) {
 
            a[i] = ch;
        }
        a[len= NULL;
 
    }
 
 
    Mystring(char c[], int num) {
 
        len = num;
        a = new char[len+1];
        char x[20];
 
        for (int i = 0; i < len; i++) {
            x[i] = c[i];
            
        }
        x[len - 1= NULL;
        strcpy(a,x);
    
    }
    
    Mystring(char c[], char s[]) {
 
        len = 5;
        a = new char[len + 1];
        char x[20];
        for (int i = 0; i < len; i++) {
            x[i] = c[i];
 
        }
        x[len - 1= NULL;
        strcpy(this->a, x);
        
    }
 
    Mystring(Mystring &my) {
 
        len = my.len;
        this->= new char[len + 1];
 
        strcpy_s(this->a, len + 1, my.a);
 
    }
    ~Mystring(){
    
        delete []a;
    
    }
 
    Mystring & operator = (const Mystring &aa) {
 
        len = strlen(aa.a);
        delete []this->a; // 기존에 있던 two가 가리키는 값을 지워 주려고 delete를 사용. 
        this->= new char[len + 1];
        strcpy_s(this->a, len + 1, aa.a);
        return *this;
    }
 
 
    /*
    Mystring & operator += (const Mystring &my) {
 
        
        char *tmp;
        int len1 = strlen(my.a);
        tmp = new char[len1+1];
        strcpy(tmp, my.a);
    
        len = strlen(this->a)+ len1;
        this->= new char[len + 1];
 
        strcpy(this->a, strcat(this->a, my.a));
 
        a[len - 1= NULL;
        return *this;
    }
    */
    Mystring & operator+=(const Mystring &aa) {
 
        char *cpy;
        cpy = new char[strlen(this->a) + 1];
        strcpy(cpy, this->a);
 
        len = strlen(this->a) + strlen(aa.a) + 1;
        this->= new char[len];
 
        strcpy(this->a, strcat(cpy, aa.a));
        a[len - 1= NULL;
 
        return *this;
    }
    
    char & operator [] (const int num) {
 
        return this->a[num];
    }
 
    Mystring & operator +(const Mystring &aa) {
 
        char *tmp;
        len = strlen(this->a) + strlen(aa.a) + 1//30
        tmp = new char[len+1];
        strcpy(tmp,this->a);
        delete []this->a;
        this->= new char[len+1];
        strcpy(this->a, tmp);
        strcat(this->a, strcpy(tmp, aa.a));
 
        return *this;
 
    }
    friend ostream & operator <<(ostream & out, Mystring & a);
    friend istream & operator >> (istream& in, Mystring &aa);
    
};
ostream & operator <<(ostream & out, Mystring & aa) {
 
    out << aa.a;
    return out;
}
 
istream & operator >> (istream& in, Mystring &aa) {
 
    in >> aa.a;
    return in;
}
 
 
 
int main()
{
    
    Mystring one("lottery winner!");//생성자함수호출
    cout << one << endl;  //출력연산자함수호출
 
    Mystring two(20'$');//생성자함수호출
    cout << two << endl;//출력연산자함수호출
 
    Mystring three(one); // 복사생성자호출
    cout << three << endl; //출력연산자함수호출
 
 
    one += "oops"// +=연산자함수호출 ( strcat함수 )
    cout << one << endl;//문자열결합 
 
 
    two = "sorry!that was";//대입연산자함수 호출 
    cout << two << endl;
 
    three[0= 'p';//[]첨자연산자함수 호출 
    cout << three << endl;
    
 
    Mystring four;
    four = two + three;
    cout << four << endl;
 
 
    char alls[] = "all's well that ends well";
    Mystring five(alls, 20); //생성자호출
    cout << five << "!\n";
 
    
    Mystring six(alls + 6, alls + 10);  //생성자
    cout << six << ",";
 
 
    Mystring seven(&five[6], &five[10]);  //생성자
    cout << seven << "...\n";
 
 
    Mystring eight;
    cout << "문자열 입력하세요 :";
    cin >> eight;  // >>연산자호출
    cout << " 입력한 문자열은 \"" << eight << "\" 입니다 " << endl;
 
}
 
cs

'c++' 카테고리의 다른 글

c++ public protected private  (0) 2018.12.31
reference 레퍼런스 계산기 구현  (0) 2018.12.31
c++ operator 연산자 구현  (0) 2018.12.31
c++ 동적 계산기 구현 ver1  (0) 2018.12.31