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
| #include<iostream> #include <string.h>
using std::cout; using std::endl;
class myString{ public: myString(); myString(const char* r); myString(const myString& r); ~myString(); void show(){cout<<ptr<<endl;} int size(){return len;} void clear(); bool empty(); myString operator= (const myString& r); myString operator+ (const char* r); myString operator+(const myString& r); myString operator+= (const char* r); myString operator+= (const myString& r); char operator[](int n);
void append(const char* r); void append(const myString& r);
friend std::ostream& operator<< (std::ostream& out, const myString& r); friend std::istream& operator>> (std::ostream& in, myString& r);
const char* data(){return this->ptr;}; int toInt(); private: char* ptr; int len; };
myString::myString(){ ptr = (char*)malloc(1); memset(ptr,0,1); len = 0; }
myString::myString(const char* r){
len = strlen(r); ptr = (char*)malloc(len+1); memset(ptr,0,len+1);
memmove(ptr,r,len+1); }
myString::myString(const myString& r){ len = r.len; ptr = (char*)malloc(len+1); memset(ptr,0,len+1);
memmove(ptr,r.ptr,len+1); }
myString::~myString(){ free(ptr); }
void myString::clear(){ free(ptr); ptr=(char*) malloc(1); memset(ptr,0,1); len = 0; }
bool myString::empty(){ return !len; }
myString myString::operator= (const myString& r){ if(this != &r) { if(ptr != nullptr) free(ptr); len = r.len; ptr = (char*)malloc(len+1); memset(ptr, 0, len+1); memmove(ptr, r.ptr, len+1); } return *this; }
myString myString::operator+(const char* r){ myString newStr; free(newStr.ptr); newStr.len = this->len+strlen(r); newStr.ptr = (char*)malloc(newStr.len+1); memset(newStr.ptr,0,newStr.len+1); memmove(newStr.ptr,ptr,len); strcat(newStr.ptr,r); return newStr; }
myString myString::operator+(const myString& r){ myString newStr; free(newStr.ptr); newStr.len = this->len+r.len; newStr.ptr = (char*)malloc(newStr.len+1); memset(newStr.ptr,0,newStr.len+1); memmove(newStr.ptr,ptr,len); strcat(newStr.ptr,r.ptr); return newStr; }
myString myString::operator+=(const char* r){ char* temp = ptr; int l = len; this->len = this->len+strlen(r); this->ptr = (char*)malloc(this->len+1); memset(ptr,0,len+1); memmove(ptr,temp,l); strcat(ptr,r); free(temp); return *this; }
myString myString::operator+=(const myString& r){ char* temp = ptr; int l = len; len = len+r.len; ptr = (char*)malloc(len+1); memset(ptr,0,len+1); memmove(ptr,temp,l); strcat(ptr,r.ptr); free(temp); return *this; }
char myString::operator[](int n){ return ptr[n]; }
void myString::append(const char* r){ char* temp = ptr; int l = len; this->len = this->len+strlen(r); this->ptr = (char*)malloc(this->len+1); memset(ptr,0,len+1); memmove(ptr,temp,l); strcat(ptr,r); free(temp); }
void myString::append(const myString& r){ char* temp = this->ptr; int l = len; len = len+r.len; ptr = (char*)malloc(len+1); memset(ptr,0,len+1); memmove(ptr,temp,l); strcat(ptr,r.ptr); free(temp); }
std::ostream& operator<< (std::ostream& out, const myString& r){ out<<r.ptr; return out; }
std::istream& operator>> (std::ostream& in, myString& r){ char buf[1024]; std::cin>>buf; free(r.ptr); r.len = strlen(buf); r.ptr = (char*)malloc(r.len+1); memset(r.ptr,0,r.len+1); memmove(r.ptr,buf,r.len+1);
}
int myString::toInt(){ return atoi(this->ptr); }
int main(){ myString str = "hello"; myString ptr(str); myString atp = str; ptr = str; ptr = ptr + " world"; ptr.show(); myString str2 = " world"; str2 = str+str2; str += " world"; str.show(); ptr+=str2; ptr.show(); atp.append(" world"); atp.show(); return 0; }
|