본문으로 바로가기

String 클래스 구조

category C++ 2018. 3. 15. 23:14
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
#pragma warning(disable:4996//경고를 무시해라 임
#include<iostream>
//#include<string>
#include<cstring>
using namespace std;
 
class Mystring
{
    char *str;  //문자메모리공간할당
    int len;    //문자열길이 저장변수
    char hi;
 
public:
    //1번
    Mystring(const char *p)
    {
        //문자열 길이구하기
        len = strlen(p)+1;  //strlen은 실제 길이만 알려줌 그래서+\n해야하니까 +1해야함
        //메모리 할당
        str = new char[len];
        //문자열 복사
        strcpy(str, p); // (복사할곳, 복사할 값)
    }
    //2번
    Mystring(int len, char dollar)
    {
        this->len = len+1;    //밑에서 20을 받아와서 this로 넣고
        this->str = new char[this->len];    //str에 동적메모리 할당을 해 $를 20개 넣고 마지막에 '\0'을 넣어야해서
        
        for (int i=0; i < len; i++) {
            str[i] = dollar;
        }
        str[len] = '\0';    //문자열마지막에는 NULL값이 저장되어야해서
    }
    //3번
    Mystring(const Mystring& one) {
        len = strlen(one.str) + 1;
        str = new char[len];
        strcpy(str, one.str);
 
        /*str = one.str;    밑에서 one을 줬으니까 받는이름은 내맘인데 one으로 받는게 보기 좋겠지
                        근데 이거는 one의 str의 주소값을 str에 대입하는거라서 str을 one과 Three가 둘다
                        가리키고 있다 그래서 소멸자를 생성하면 소멸은 스택순서로 없어지는데 Three
                        가 먼저 없어졌는데 one 없어져야하는데 one과 three가 같은 곳을 가리키고 있어서
                        one이 지울 str이 없어서 안됨 */
    }
    //4번
    Mystring& operator +=(const char *p)
    {
        //int len2;
        //char *temp; //새로운 공간에 담아야지
        //
        //len = strlen(str);    //lottery winner의 길이
        //len2 = strlen(p);    //oops의 길이
        //
        //temp = new char[len + len2];    //두 개 다 담을 공간 할당
        //
        //strcpy(temp, str);    //temp에 lottery winner을 복사하고
        //strcat(temp, p);    //temp에 있는 lottery winner와 oops를 결함
        //delete[]str;        //str을 지우고 다시할당할거임
 
        //str = new char[len + len2];    //str을 다시 할당
        //strcpy(str, temp);        //str에 temp문자열을 복사
 
        //delete[]temp;        //temp 다 썻으니까 삭제
 
        //return *this;
 
        len = len + strlen(p);
        char *str2 = new char[len];
        strcpy(str2, str);
        delete str;
        str = new char[len];
        strcpy(str,str2);
        strcat(str, p);
        delete str2;
        return *this;
    }
    //5번
    Mystring& operator=(const char *p)
    {
        int len2;
        len2 = strlen(p)+1;
        delete [] str;
        str = new char[len2];
        strcpy(str, p);
        str[len2 - 1= '\0';
 
        return *this;
        
        /*
        strcpy(str,p);
        return *this;
        */
    }
 
    //6번
    char& operator[](const int g)
    {
        //this->str[g];
        return this->str[g];
    }
    Mystring() {
        len = 0;
        str = NULL;
    }
    //7번
    Mystring operator+(Mystring& three) { //물어보기
        Mystring temp;    //인자값은 안줬으니까 기본생성자가 필요
        temp.len = strlen(str) + strlen(three.str) + 1;
        temp.str = new char[temp.len];
        strcpy(temp.str, str);    //여기 2군
        strcat(temp.str, three.str);
        
        return temp;    //
    }
    Mystring& operator=(const Mystring& temp) {
        str = new char[temp.len];
        strcpy(str, temp.str);
        str[temp.len-1= '\0';
        
        return *this;
    }
    Mystring(char *p, int num) {
        str = new char[num + 1];
        for (int i = 0; i < num; i++)
        {
            str[i] = p[i];
        }
        str[num] = '\0';
    }
    Mystring(char *p1, char *p2) {
        len = p2 - p1 + 1;
        str = new char[len];
        for (int i = 0; i < len; i++) {
            str[i] = p1[i];
        }
        str[len-1= NULL;
    }
    
    ~Mystring()    //소멸자
    {
        delete []str;
    }
    friend ostream& operator<<(ostream &out, const Mystring &obj);    //1
    friend istream& operator>>(istream &in, Mystring &obj);
};
 
ostream& operator<<(ostream &out, const Mystring &obj)
{
    out << obj.str;
 
    return out;
}
istream& operator>>(istream &in, Mystring &YJ) {
    char *HJ;
    HJ = new char[1000];    
    YJ.len = strlen(HJ)+1;
    YJ.str = new char[YJ.len];
    in >> HJ;
    strcpy(YJ.str, HJ);
    return in;
}
 
void 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';//[]첨자연산자함수 호출 //three.operatorp[](0)   
    cout << three << endl;
 
    
    Mystring four;
    four = two + three;     //four.operator=(two.operator+(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;  // >>연산자호출//cin.operator>>(eight)
 
    cout << " 입력한 문자열은 \"" << eight << "\" 입니다 " << endl;
 
 
 
 
}
cs

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

동적바인딩을 이용한 Stack, Queue 구현  (0) 2018.03.14
C++ 클래스를 이용한 성적처리(동적메모리)  (0) 2018.03.08
DAY1  (0) 2018.03.06