首页 > 代码库 > MyString(重写String)

MyString(重写String)

http://wenku.baidu.com/view/d7ac113243323968011c925b.html

http://blog.csdn.net/wxm349810930/article/details/52553578

已知类String的原型为:

 class String  {

    public:    String(const char *str = NULL); // 普通构造函数

  String(const String &other);     // 拷贝构造函数

  ~ String(void);         // 析构函数

   String & operate =(const String &other); // 赋值函数

    private:    char   *m_data;    // 用于保存字符串

  };

  请编写String的上述4个函数。

 

技术分享
  1 /*
  2 ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30
  3    new不忘delete
  4 Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString):
  5 
  6 MyString();//构造
  7 MyString(const char* cString);//地址不能变
  8 char at(int index) const;//输入数组下标,返回字符
  9 int length() const;//长度
 10 void clear();//清空 len=0
 11 bool empty() const;//是否清空?len不变
 12 int compare(const MyString& s) const;//
 13 int compare(int index, int n, const MyString& s) const;
 14 void copy(char s[], int index, int n);
 15 char* data() const;
 16 int find(char ch) const;
 17 int find(char ch, int index) const;
 18 int find(const MyString& s, int index) const;
 19 
 20 */
 21 
 22 #include<iostream>
 23 #include<string.h>
 24 using namespace std;
 25 class MyString
 26 {
 27 public:
 28     MyString();
 29     MyString(const char* cString);//ordinary func
 30     MyString(const MyString &other);//copy func
 31     MyString &operator =(const MyString &other);//assign func
 32     //~MyString(void);
 33     char at(int index) const;//
 34     int length() const;
 35     void clear();
 36     bool empty() const;
 37     int compare(const MyString& s) const;
 38     int compare(int index, int n, const MyString& s) const;
 39     void copy(char s[], int index, int n);
 40     char* data() const;
 41     int find(char ch) const;
 42     int find(char ch, int index) const;
 43     int find(const MyString& s, int index) const;
 44 
 45     MyString operator + (const MyString & another);
 46 
 47     bool operator > (const MyString & another);
 48     bool operator < (const MyString & another);
 49     bool operator == (const MyString & another);
 50 
 51     char& operator[](int idx);
 52 
 53     void dis();
 54 
 55     ~MyString()
 56     {
 57         cout<<"delete..."<<endl;
 58         delete []a;
 59     }
 60 
 61 private:
 62     char *a;
 63     int len;
 64 };
 65 
 66 MyString::MyString(){
 67     cout << "MyString()" << endl;
 68 }
 69 
 70 MyString::MyString(const char* cString)
 71 {
 72     if (cString==NULL)
 73     {
 74         a=new char[1];
 75         a[0]=0;
 76         //int len=0;
 77     }
 78     else
 79     {
 80         len = strlen(cString);//new
 81         a=new char [len+1];
 82         strcpy(a,cString);
 83         //len=strlen(cString);
 84     }
 85 }
 86 
 87 MyString::MyString(const MyString &other){
 88     int len = strlen(other.a);
 89     a = new char[len+1];
 90     strcpy(a, other.a);
 91 }
 92 
 93 MyString &MyString::operator =(const MyString &other){
 94     //self check assign
 95     if(this == &other){
 96         return *this;
 97     }
 98     //释放原有的内存资源
 99     delete []a;
100     //分配新的内存资源,并复制内容
101     int len = strlen(other.a);
102     a = new char[len+1];
103     strcpy(a, other.a);
104     //返回本对象的引用
105     return *this;
106 }
107 
108 char MyString::at(int index) const
109 {
110     if(len==0)
111     {cout<<"no char in string"<<endl;
112         return 0;
113     }
114     if(index>len)
115     {
116         cout<<"the maximun array exceed"<<endl;
117         return 0 ;
118     }
119     else
120     {
121 
122         return a[index-1];
123     }
124 }
125 
126 int MyString::length() const
127 {
128     return len;
129 }
130 
131 void MyString::clear()
132 {
133     if(!a)
134     {
135         delete []a;
136         a=NULL;
137     }
138 
139     len=0;
140     //a=" ";
141 }
142 bool MyString::empty() const
143 {
144     if(len==0)
145         return true;
146     else
147         return false;
148 }
149 
150 
151 int MyString::compare(const MyString& s) const
152 {
153     int m=this->len;int n=s.len;
154 
155     for(int i=0;i<m&&i<n;i++)
156     {
157         if(s.a[i]==a[i])
158             continue;
159         else if(s.a[i]<a[i])
160             return 1;
161         else
162             return -1;
163     }
164     return 0;
165 
166 
167 }
168 int MyString::compare(int index, int n, const MyString& s) const
169 {
170     int m=len,k=s.len;
171     if(index+n>m||index+n>k)
172     {
173         cout<<"cannot compare!"<<endl;
174         ///I dont know how to solve it
175     }
176     for(int i=index-1,j=0;i<n+index;i++,j++)
177     {
178         if(s.a[j]==a[i])
179             continue;
180         else if(s.a[j]<a[i])
181             return 1;
182         else
183             return -1;
184     }
185     return 0;
186 }
187 
188 
189 void MyString::copy(char s[], int index, int n)
190 {
191     if(n>=len)
192     {cout<<"the maximun array exceed"<<endl;}
193     else  if(n+index-1>len)
194     {
195         cout<<"the maximun array exceed"<<endl;
196         return;
197     }
198     else
199     {
200         for(int i=n-1,j=0;i<n+index-1;i++,j++)
201             s[j]=a[i];
202     }
203 
204 }
205 char* MyString::data() const
206 {
207     return a;
208 }
209 
210 
211 
212 
213 int MyString::find(char ch) const
214 {
215 
216     for(int i=0;i<len;i++)
217     {
218         if(ch==a[i])
219             return i+1;
220     }
221 
222     return 0;
223 }
224 int MyString::find(char ch, int index) const
225 {
226     if(index>len||index<1)
227     {
228         cout<<"the index num is should be  1~ "<<len<<endl;
229         return 0;
230     }
231     for(int i=index-1;i<len;i++)
232     {
233         if(ch==a[i])
234             return i+1;
235     }
236     return -1;
237 
238 }
239 
240 int MyString::find(const MyString& s, int index) const
241 {
242     if(index>s.len||index<1)
243     {
244         cout<<"the index num is should be  1~ "<<s.len<<endl;
245         return 0;
246     }
247     char ch=s.a[index-1];
248 
249     for(int i=0;i<len;i++)
250     {
251         if(ch==a[i])
252             return i+1;
253     }
254 
255     return 0;
256 }
257 
258 // 加法运算符重载
259 MyString MyString::operator +(const MyString & another){
260     int len = strlen(this->a) + strlen(another.a);
261     MyString str;
262     delete []str.a;
263     str.a = new char[len + 1];
264     memset(str.a,0,len + 1);
265     //int len1 = strlen(this->a) + 1;
266     //strcat_s(str.a, len1, this->a);
267     strcat(str.a, this->a);
268     // 源串长度 + 目标串长度 + 结束符
269     //int len2 = strlen(this->a) + strlen(another.a) + 1;
270     //strcat_s(str.a,len2, another.a);
271     strcat(str.a, another.a);
272     return str;
273 }
274 
275 // ==关系运算符重载
276 bool MyString::operator==(const MyString &other)
277 {
278     if(strcmp(this->a,other.a) == 0)
279         return true;
280     else
281         return false;
282 }
283 // >关系运算符重载
284 bool MyString::operator>(const MyString &other)
285 {
286     if(strcmp(this->a,other.a) > 0)
287         return true;
288     else
289         return false;
290 }
291 // <运算符重载
292 bool MyString::operator<(const MyString &other)
293 {
294     if(strcmp(this->a,other.a) < 0)
295         return true;
296     else
297         return false;
298 }
299 // []运算符重载
300 char& MyString::operator[](int idx)
301 {
302     return a[idx];
303 }
304 
305 int main(){
306     MyString ms;
307     ms = "string";
308     cout << ms[0] << ms[1] << ms[2] << endl;
309     return 0;
310 }
View Code

 

MyString(重写String)