MyString(重写String)

时间:2023-03-08 16:24:35

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

已知类String的原型为:

class String{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operator =(const String &other);// 赋值函数
private:
char *m_data; // 用于保存字符串
};

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

 /*
ccnu_hupo_cpp_class_tst6exercise2_by:lele_2013_10_30
new不忘delete
Design the string class in the C++ library by providing your own implementation for the following functions (name your class MyString): MyString();//构造
MyString(const char* cString);//地址不能变
char at(int index) const;//输入数组下标,返回字符
int length() const;//长度
void clear();//清空 len=0
bool empty() const;//是否清空?len不变
int compare(const MyString& s) const;//
int compare(int index, int n, const MyString& s) const;
void copy(char s[], int index, int n);
char* data() const;
int find(char ch) const;
int find(char ch, int index) const;
int find(const MyString& s, int index) const; */ #include<iostream>
#include<string.h>
using namespace std;
class MyString
{
public:
MyString(void):a(NULL){cout << "MyString()" << endl;}
MyString(const char* cString=NULL);//ordinary func
MyString(const MyString &other);//copy func
MyString & operator =(const MyString &other);//assign func
//~MyString(void);
char at(int index) const;//
int length() const;
void clear();
bool empty() const;
int compare(const MyString& s) const;
int compare(int index, int n, const MyString& s) const;
void copy(char s[], int index, int n);
char* data() const;
int find(char ch) const;
int find(char ch, int index) const;
int find(const MyString& s, int index) const; // MyString &operator+=(const MyString &other);
//MyString operator +(const MyString &mystr);
//MyString &operator +(const MyString &mystr);
//重载为友元函数,重载"+"运算符(不改变被加对象) 注意返回引用不行
friend MyString operator +(const MyString& mystr1,const MyString &mystr2);
MyString &operator+=(const MyString &other); bool operator > (const MyString & another);
bool operator < (const MyString & another);
bool operator == (const MyString & another); char& operator[](int idx); void dis();
friend ostream& operator<<(ostream& os,const MyString& other); ~MyString(); private:
char *a;
int len;
}; //MyString::MyString(){
// cout << "MyString()" << endl;
//}
MyString::~MyString(){
cout<<"~MyString():delete..."<<endl;
delete []a;
} MyString::MyString(const char* cString)
{
if (cString==NULL)
{
a=new char[];
a[]=;
//int len=0;
}
else
{
len = strlen(cString);//new
a=new char [len+];
strcpy(a,cString);
//len=strlen(cString);
}
} MyString::MyString(const MyString &other){
int len = strlen(other.a);
a = new char[len+];
strcpy(a, other.a);
} MyString &MyString::operator =(const MyString &other){
//self check assign
if(this == &other){
return *this;
}
//释放原有的内存资源
delete []a;
//分配新的内存资源,并复制内容
int len = strlen(other.a);
a = new char[len+];
strcpy(a, other.a);
//返回本对象的引用
return *this;
} char MyString::at(int index) const
{
if(len==)
{cout<<"no char in string"<<endl;
return ;
}
if(index>len)
{
cout<<"the maximun array exceed"<<endl;
return ;
}
else
{ return a[index-];
}
} int MyString::length() const
{
return len;
} void MyString::clear()
{
if(!a)
{
delete []a;
a=NULL;
} len=;
//a=" ";
}
bool MyString::empty() const
{
if(len==)
return true;
else
return false;
} int MyString::compare(const MyString& s) const
{
int m=this->len;int n=s.len; for(int i=;i<m&&i<n;i++)
{
if(s.a[i]==a[i])
continue;
else if(s.a[i]<a[i])
return ;
else
return -;
}
return ; }
int MyString::compare(int index, int n, const MyString& s) const
{
int m=len,k=s.len;
if(index+n>m||index+n>k)
{
cout<<"cannot compare!"<<endl;
///I dont know how to solve it
}
for(int i=index-,j=;i<n+index;i++,j++)
{
if(s.a[j]==a[i])
continue;
else if(s.a[j]<a[i])
return ;
else
return -;
}
return ;
} void MyString::copy(char s[], int index, int n)
{
if(n>=len)
{cout<<"the maximun array exceed"<<endl;}
else if(n+index->len)
{
cout<<"the maximun array exceed"<<endl;
return;
}
else
{
for(int i=n-,j=;i<n+index-;i++,j++)
s[j]=a[i];
} }
char* MyString::data() const
{
return a;
} int MyString::find(char ch) const
{ for(int i=;i<len;i++)
{
if(ch==a[i])
return i+;
} return ;
}
int MyString::find(char ch, int index) const
{
if(index>len||index<)
{
cout<<"the index num is should be 1~ "<<len<<endl;
return ;
}
for(int i=index-;i<len;i++)
{
if(ch==a[i])
return i+;
}
return -; } int MyString::find(const MyString& s, int index) const
{
if(index>s.len||index<)
{
cout<<"the index num is should be 1~ "<<s.len<<endl;
return ;
}
char ch=s.a[index-]; for(int i=;i<len;i++)
{
if(ch==a[i])
return i+;
} return ;
} // 加法运算符重载
//MyString MyString::operator +(const MyString & another){
// int len = strlen(this->a) + strlen(another.a);
// MyString str(""); // delete []str.a;
// str.a = new char[len + 1];
// memset(str.a,0,len + 1);
// //int len1 = strlen(this->a) + 1;
// //strcat_s(str.a, len1, this->a);
// strcat(str.a, this->a);
// // 源串长度 + 目标串长度 + 结束符
// //int len2 = strlen(this->a) + strlen(another.a) + 1;
// //strcat_s(str.a,len2, another.a);
// strcat(str.a, another.a);
// return str;
//} //MyString MyString::operator +(const MyString &mystr){//mem leak
// MyString newString("");
// if (!mystr.a)
// newString = *this;
// else if (!a)
// newString = mystr;
// else {
// newString.a = new char[strlen(a) + strlen(mystr.a) + 1];
// strcpy(newString.a, a);
// strcat(newString.a, mystr.a);
// }
// return newString;
//} //重载"+"运算符(不改变被加对象)
//MyString &MyString::operator +(const MyString &mystr){
// MyString *temp=new MyString("");
// temp->a=new char[strlen(a)+strlen(mystr.a)+1];
// strcpy(temp->a,a);
// strcat(temp->a,mystr.a);
// return *temp;
//} /*
//重载"+"运算符(改变被加对象)
Mystring &operator +(Mystring &mystr){
char *temp=str;
str=new char[strlen(temp)+strlen(mystr.str)+1];
strcpy(str,temp);
strcat(str,mystr.str);
delete [] temp;
return *this;
}
*/
//friend
MyString operator +(const MyString& mystr1,const MyString &mystr2){
MyString str("");
delete[] str.a;
str.len = strlen(mystr1.a)+strlen(mystr2.a);
str.a = new char[str.len + ];
strcpy(str.a, mystr1.a);
strcat(str.a, mystr2.a);
return str; // //MyString *temp=new MyString("");//mem leak
// MyString temp("");
// temp.a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
// //temp->a=new char[strlen(mystr1.a)+strlen(mystr2.a)+1];
// strcpy(temp.a,mystr1.a);
// strcat(temp.a,mystr2.a);
// return temp;
} // ==关系运算符重载
bool MyString::operator==(const MyString &other)
{
if(strcmp(this->a,other.a) == )
return true;
else
return false;
}
// >关系运算符重载
bool MyString::operator>(const MyString &other)
{
if(strcmp(this->a,other.a) > )
return true;
else
return false;
}
// <运算符重载
bool MyString::operator<(const MyString &other)
{
if(strcmp(this->a,other.a) < )
return true;
else
return false;
}
// []运算符重载
char& MyString::operator[](int idx)
{
return a[idx];
} ostream& operator<<(ostream& os,const MyString& other){
return os << other.a;
} MyString &MyString::operator+=(const MyString &other)
{
int len = strlen(a) + strlen(other.a) + ;
char *newstr = new char[len];
memset(newstr, , len);
strcpy(newstr, a);
strcat(newstr, other.a); delete[] a; a = newstr;
return *this;
} int main(){
MyString ms="hello"; MyString mt("world");
mt += ms;
cout << mt << endl;
return ;
}