728x90
반응형
String 함수를 만들어보았습니다.
(Mystring 코드)
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 | #pragma warning(disable : 4996) #include <iostream> #include <cstring> using namespace std; class Mystring { char *str; //문자메모리공간 할당 int len; //문자열 길이저장변수 public: Mystring(const char *p) //생성자함수호출 { // 문자열 길이 구하기 len = strlen(p)+1; //문자열 마지막에 NULL! // 메모리 할당 str = new char[len]; //문자열 복사 strcpy(str, p); } Mystring(const int len) { this->len = len; str = new char[len]; } Mystring(const int data, const char dol) //생성자함수 호출 { str = new char[data+1]; for (int i = 0; i < data; i++) { str[i] = dol; } str[data] = NULL; } Mystring(const Mystring & aa) { //복사 생성자함수 호출 len = strlen(aa.str) + 1; str = new char[len]; strcpy(str, aa.str); } Mystring& operator += (const char *p) //+= 연산자함수 호출 { char *temp; len = strlen(this->str)+strlen(p)+1; temp = new char[len]; strcpy(temp, this->str); delete[] str; this->str = new char[len]; strcpy(this->str, temp); delete[] (temp); strcat(this->str, p); return *this; } Mystring & operator = (const char *p) //대입연산자함수 호출 { len = strlen(p)+1; str = new char[len]; //this->str = p; strcpy(str, p); return *this; } char& operator [] (int dol) //첨자 { return this->str[dol]; } Mystring() { //str = new char[1000]; //len = 1000; len = 0; str = NULL; } Mystring operator + (Mystring& aa) // four = two + three; four.operator=(two.operator+(three)); { /*Mystring res; char *temp; int len2; len2 = strlen(str) + strlen(aa.str) + 1; temp = new char[len2]; strcpy(temp, str); strcat(temp, aa.str); res.str = temp; return res;*/ Mystring *res; res = new Mystring(strlen(str) + strlen(aa.str) + 1); strcpy(res->str, str); strcat(res->str, aa.str); return *res; } Mystring& operator = (const Mystring &cc) { len = strlen(cc.str)+1; str = new char[len]; //*str = *cc.str; strcpy(this->str, cc.str); return *this; } Mystring(const char *ab, int data) { str = new char[data+1]; for (int i = 0; i < data; i++) { str[i] = ab[i]; } str[data] = NULL; } Mystring(const char *a , const char *b) { len = b-a+1; str = new char[len]; for (int i = 0; i < len; i++) { str[i] = a[i]; } str[len-1] = NULL; } ~Mystring() { delete[] str; } friend ostream& operator<<(ostream &out, const Mystring &obj); friend istream& operator >>(istream &in, Mystring &obj); }; ostream& operator<<(ostream &out, const Mystring &obj) { out << obj.str; return out; } istream& operator >>(istream &in, Mystring &obj) { /*obj.len = strlen(obj.str); char *p = new char[obj.len + 1]; in >> p; if(obj.str != NULL) delete obj.str; obj.str = p; */ char *buf = new char[1000]; in >> buf; obj.len = strlen(buf) + 1; obj.str = new char[obj.len]; strcpy(obj.str, buf); delete []buf; 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';//[]첨자연산자함수 호출 cout << three << endl; Mystring four; four = two + three;// four.operation=(two.operation +( 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; } |
(실행결과)
반응형
'Programming > C++' 카테고리의 다른 글
(C++)템플릿을 사용한 스택/큐 구조 프로그램 (2) | 2018.03.16 |
---|---|
(C++) 큐, 스택이 뭔지 아시나요?? FIFO? FILO? (0) | 2018.03.14 |
(C++) 급여관리프로그램을 짜보았습니다. (0) | 2018.03.12 |
(C++)난 private고 넌 외부사람이야! 우린 안돼! 접근지정자에 대해 알아보자! (0) | 2018.03.09 |
(C++) 클래스를 이용하여 성적처리 (4) | 2018.03.08 |