반응형
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 | #define _CRT_SECURE_NO_WARNINGS //#pragma warning(disable:4996) #include <iostream> #include <cstring> using namespace std; class MyString { char *_str; //문자 메모리 공간 할당 변수 int _len; // public: MyString() { _str = NULL; _len = -1; } MyString(const char *str) { _len = strlen(str) + 1; _str = new char[_len]; strcpy(_str, str); } MyString(int len) { _str = new char[len + 1]; _len = len; } MyString(int len, const char str) { _str = new char[len + 1]; for (int i = 0; i < len; i++) _str[i] = str; _str[len] = '\0'; } MyString(const char *str, int len) { _len = len + 1; _str = new char[_len]; strncpy(_str, str, len); _str[len] = '\0'; } MyString(const MyString &ms) { _len = strlen(ms._str) + 1; _str = new char[_len]; strcpy(_str, ms._str); } MyString(char *p1, char *p2) { _len = p2 - p1; _str = new char[_len + 1]; for (int i = 0; i < _len; i++) _str[i] = p1[i]; _str[_len] = '\0'; } ~MyString() { delete[] _str; } friend ostream& operator << (ostream &out, const MyString &obj); friend istream& operator >> (istream &cin, MyString &ms); void operator += (const char *str) // one+="oops" { _len += strlen(str); char *temp = new char[_len]; strcpy(temp, _str); strcat(temp, str); delete[] _str; _str = temp; } MyString operator+(MyString& ms) // four = two + three; four.operator=(two.operator+(three)) { MyString *tempMs; tempMs = new MyString(_len + ms._len+1); strcpy(tempMs->_str, _str); strcat(tempMs->_str, ms._str); //tempMs->_len = _len + ms._len; // 아래 코드가 문제임 //tempMs->_str = strcat(_str, ms._str); return *tempMs; } MyString& operator=(const MyString& ms) { /* cout << ms._len << endl; exit(-1);*/ _len = ms._len; delete[] _str; _str = new char[_len]; strcpy(_str, ms._str); return *this; } MyString& operator=(const char *ms) { _len = strlen(ms) + 1; delete[] _str; _str = new char[_len]; strcpy(_str, ms); return *this; } char& operator [] (int index) { return _str[index]; } }; ostream& operator << (ostream &out, const MyString &obj) { out << obj._str; return out; } istream& operator >> (istream &in, MyString &ms) { /*char *buf = new char[BUFSIZ]; in >> buf; ms.len = strlen(buf) + 1; obj.str = new char[obj.len]; strcpy(obj.str, buf); delete[]buf; return in;*/ char *tmpStr = new char[BUFSIZ]; in >> tmpStr; ms._len = strlen(tmpStr) + 1; ms._str = new char[ms._len]; strcpy(ms._str, tmpStr); delete[]tmpStr; return in; //cout << tmpStr; //exit(-1); //ms._len = strlen(tmpStr) + 1; //tmpStr = new char[ms._len]; /*char tmpStr[BUFSIZ]; cin >> tmpStr; ms._len = strlen(tmpStr) + 1; ms._str = new char[ms._len]; strcpy(ms._str, tmpStr); return cin;*/ } int main() { /* 1 */ 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; return 0; } | cs |
반응형