본문 바로가기
카테고리 없음

C++ Mystring quiz_0920

by 리승연 2018. 9. 21.
반응형

2018/09/21 - [Progamming/ C++] - [C++]copy constructor_0917

2018/09/21 - [Progamming/ C++] - [C++] operator function & assignment operator_9190


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
#include<iostream>
//#include<string>
#include<cstring>
#pragma warning(disable :4996)
using namespace std;
/*
    배열에 문자열을 넣을 땐 자동null설정/ 문자를 넣을땐 null 추가
*/
class Mystring {
    //field
    char *str;
    int len;
public:
    //constructor
    Mystring() {
        this->str = new char;
    }
    Mystring(const char *str) {
        //Mystring one("lottery winner!"); 1번
        len = strlen(str) + 1;
        this->str = new char[len];//문자열 상수 길이 null전까지 복사 , 문자열 값은 null까지 복사
        strcpy(this->str, str);
    }
    Mystring(int len, char str) {
        //Mystring two(20, '$'); 2번
        this->str = new char[len + 1];
        for (int i = 0; i < len; i++) {
            this->str[i] = len;
        }
        this->str[len] = NULL;
    }
    //copy constructor
    Mystring(const Mystring &str) {
        //Mystring three(one); 3번
        this->len = str.len;
        this->str = new char[len + 1];
        strcpy(this->str, str.str);
    }
    //operator function
    Mystring& operator+=(const char *str) {
        //one += "oops"; 4번
        int len2 = strlen(str);
        char *str1 = new char[len]; //one 문자열을 임시 저장 할 메모리 동적 할당'
        strcpy(str1, this->str);//one 문자열을 임시배열에 복사
        delete[]this->str; //원래 one삭제(좀비방지) 2번
 
        this->str = new char[len + len2 + 1]; //char 타입 메모리 동적 할당
        strcat(str1, str); //  one문자열+"oops"
        strcpy(this->str, str1); //연결된 문자열 this->str에 복사 
 
        return *this;
    }
    void operator=(const Mystring& str) {  //Mystring 객체로 리턴
        //two = "sorry!that was" 5번.
        len = strlen(str.str) + 1;
        this->str = new char[len];
        strcpy(this->str, str.str);
        //return *this;(하거나 반환 Mystring&, return *this)
    }
    char& operator[] (const int index) {
        // char& 반환타입결정 m 6번//
        return this->str[index];
    }
    Mystring& operator+(const Mystring& str) {
        // Mystring four; 7번 
 
        int len2 = strlen(str.str);
        char *str1 = new char[len + 1];
        strcpy(str1, this->str);
 
        this->str = new char[len + len2 + 1];
 
        strcpy(this->str, str1);// 
        strcat(this->str, str.str);//
        delete[] str1;
 
        return *this;
    }
    //constructor
    Mystring(char *str, int len) {
        //Mystring five(alls, 20); "all's well that ends well" 8번
        this->str = new char[len + 1]; // 메모리 동적 할당 20만큼
        strncpy(this->str, str, len);
        this->str[len] = NULL;
    }
 
    Mystring(char *str, char *str1) {
        //Mystring six(alls + 6, alls + 10); 9번, 주소값
        //Mystring seven(&five[6], &five[10]); 10번, five[6](값)의 주소를 char *str로 보냄
        this->len = strlen(str) - strlen(str1);
        this->str = new char[len + 1];
        strncpy(this->str, str, len);
        this->str[len] = NULL;
    }
    
    //destructor 
    ~Mystring() {
        delete[]str;
    }
    //method
    friend ostream& operator<<(ostream& out, Mystring &name);
    friend istream& operator>>(istream &in, Mystring &name);
};
 
ostream& operator<<(ostream& out, Mystring &name) {
    cout << name.str;
    return out;
}
istream& operator>>(istream &in, Mystring &name) {
    char str[100]; // char 정적 메모리 할당 
    cin >> str;
    Mystring a(str);//객체생성
    name = a; //대입
    return in;
}
 
void main() {
    Mystring one("lottery winner!");//생성자함수호출 1
    cout << one << endl;  //출력연산자함수호출
 
    Mystring two(20'$');//생성자함수호출 2
    cout << two << endl;//출력연산자함수호출
 
    Mystring three(one); // 복사생성자호출 3
    cout << three << endl//출력연산자함수호출
 
    one += "oops"// +=연산자함수호출 ( strcat함수 ) 4
    cout << one << endl;//문자열결합 
 
    two = "sorry!that was";//대입연산자함수 호출 5
    cout << two << endl;
 
    three[0= 'p';//[]첨자연산자함수 호출. 주소값으로 받는다 6
    cout << three << endl;
 
    Mystring four; //7
    four = two + three;
    cout << four << endl;
 
    char alls[] = "all's well that ends well"//8
    Mystring five(alls, 20); //생성자호출
    cout << five << "!\n";
 
    Mystring six(alls + 6, alls + 10);  //생성자 9
    cout << six << ",";
 
    Mystring seven(&five[6], &five[10]);  //생성자 10
    cout << seven << "...\n";
 
    Mystring eight; //11
    cout << "문자열 입력하세요 :";
    cin >> eight;  // >>연산자호출
    cout << " 입력한 문자열은 \"" << eight << "\" 입니다 " << endl;
}
cs

 

반응형