[C++] String 코딩하기
C & C++ 2018. 9. 26. 15:27
String을 사용하지 않고 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 | #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<cstring> //#include<string> using namespace std; class Mystring { char *a; int len; public: Mystring() { this->a = new char; } //Mystring one("lottery winner!"); Mystring(const char *a) { len = strlen(a); this->a = new char[len + 1]; strcpy(this->a, a); } // Mystring two(20, '$'); Mystring(int len, const char a) { this->len = len; this->a = new char[this->len + 1]; for (int i = 0; i < len; i++) { this->a[i] = a; } this->a[len] = NULL; } ~Mystring() { delete[]a; } //소멸자 // Mystring three(one); Mystring(Mystring &a) { this->len = a.len; this->a = new char[this->len + 1]; strcpy(this->a, a.a); } Mystring(char *a, int num) {//8번 this->a = new char[num+1]; strncpy(this->a, a, num); this->a[num] = NULL; } Mystring(char *a, char *b) {//9번 len = strlen(a) - strlen(b); this->a = new char[len + 1]; strncpy(this->a, a, len); this->a[len] = NULL; } // one += "oops"; Mystring& operator +=(const char *a) { int len2 = strlen(a);// oops 길이 char * str = new char[len + 1]; //char 타입 메모리 동적할당 strcpy(str, this->a); //str에 lottery winner! 복사 delete[]this->a; //원래 있던 this->a 해제 this->a = new char[len + len2 + 1]; // this->a 새로 할당 strcat(str, a); // str(lottery winner!) + oops strcpy(this->a, str); // this->a에 str(lottery winner!oops) 복사 return *this; //this->a 리턴 } //two = "sorry!that was"; Mystring& operator =(const Mystring &a) { len = strlen(a.a); this->a = new char[len + 1]; strcpy(this->a, a.a); return *this; } // three[0]='p' pottery winner! char& operator [](int a) { return this->a[a]; } // four = two + three; Mystring& operator +(const Mystring &a) { int len2 = strlen(a.a); char *str = new char[len + 1]; //a.a = pottery winner! strcpy(str, this->a); //delete[]this->a; this->a = new char[len + len2 + 1]; strcpy(this->a, str); strcat(this->a, a.a); return *this; } friend ostream& operator << (ostream &a, Mystring &aa); friend istream& operator >>(istream &in, Mystring &aa); }; // 출력 ostream& operator << (ostream &out, Mystring &aa) { out << aa.a; return out; } // 입력 istream& operator >>(istream &in, Mystring &aa) { char str[100]; in >> str; Mystring a(str); 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++' 카테고리의 다른 글
[Visual Studio]비쥬얼스튜디오 단축키 (0) | 2018.08.22 |
---|