模仿string类,编写自己的My_string类,并重载运算符
#include <iostream>
#include <cstring>
using namespace std;
int my_strcmp(const char*str1, const char *str2);
class My_string
{
private:
char *cstr;
int len;
public:
//无参构造
My_string()
{
this->cstr = NULL;
len = 0;
cout << "My_string::无参构造" << endl;
}
//有参构造
My_string(const char *str)
{
len = strlen(str);
this->cstr = new char[len+1];
strcpy(cstr,str);
*(cstr+len) = '\0';
cout << "My_string::有参构造" << endl;
}
//拷贝构造
My_string(const My_string &other)
{
len = other.len;
this->cstr = new char[other.len+1];
strcpy(cstr, other.cstr);
*(cstr+len) = '\0';
cout << "My_string::拷贝构造" << endl;
}
//拷贝赋值(用类)
My_string &operator=(const My_string &ms)
{
if(NULL == this->cstr)
{
len = ms.len;
cstr = new char[len+1];
strcpy(cstr, ms.cstr);
*(cstr+len) = '\0';
}else
{
delete []cstr;
cstr = NULL;
len = 0;
len = ms.len;
cstr = new char[len+1];
strcpy(cstr, ms.cstr);
*(cstr+len) = '\0';
}
return *this;
}
//拷贝赋值(用字符串)
My_string &operator=(const char* str)
{
if(NULL == this->cstr)
{
len = strlen(str);
cstr = new char[len+1];
strcpy(cstr, str);
*(cstr+len) = '\0';
}else
{
delete []cstr;
cstr = NULL;
len = 0;
len = strlen(str);
cstr = new char[len+1];
strcpy(cstr, str);
*(cstr+len) = '\0';
}
return *this;
}
//析构函数
~My_string()
{
if(NULL != cstr)
{
delete []cstr;
}
cout << "My_string::析构函数 : " << this << endl;
}
//判断是否为空
bool empty()
{
if(size())
{
return true;
}else
{
return false;
}
}
//返回字符串的长度
int size()
{
len = strlen(cstr);
return len;
}
//定位函数
char &at(int index)
{
if(len >= index)
{
return *(cstr+index);
}else
{
static char error = -1;
cout<< "超出范围" <<endl;
return error;
}
}
//转化为C风格字符串
const char *c_str()
{
const char *p = cstr;
return p;
}
//定义字符串加法重载函数
const My_string operator+( const My_string &R) const
{
My_string tms;
tms.len = strlen(this->cstr) + strlen(R.cstr);
tms.cstr = new char[tms.len+1];
strcpy(tms.cstr, this->cstr);
strcat(tms.cstr, R.cstr);
*(tms.cstr+tms.len) = '\0';
return tms;
}
//判断两个字符串是否相等
bool operator==(const My_string &R) const
{
if(my_strcmp(this->cstr, R.cstr) >0)
{
return true;
}else
{
return false;
}
}
bool operator==(const char* str)const
{
if(my_strcmp(this->cstr, str) >0)
{
return true;
}else
{
return false;
}
}
bool operator ==(const string &str)const
{
if(my_strcmp(this->cstr, str.data()) == 0)
{
return true;
}else
{
return false;
}
}
//判断两个字符串是否不相等
bool operator!=(const My_string &R)const
{
if(my_strcmp(this->cstr, R.cstr) != 0)
{
return true;
}else
{
return false;
}
}
//判断字符串1和字符串2的大小关系
bool operator>(const My_string &R)const
{
if(my_strcmp(this->cstr, R.cstr) >0)
{
return true;
}else
{
return false;
}
}
bool operator<(const My_string &R)const
{
if(my_strcmp(this->cstr, R.cstr) < 0)
{
return true;
}else
{
return false;
}
}
bool operator>=(const My_string &R)const
{
if(my_strcmp(this->cstr, R.cstr) >= 0)
{
return true;
}else
{
return false;
}
}
bool operator<=(const My_string &R)const
{
if(my_strcmp(this->cstr, R.cstr) <= 0)
{
return true;
}else
{
return false;
}
}
//返回对应下标的字符
char &operator[](int i)
{
if(i<0 || i>=len)
{
static char c = ' ';
cout<<"下标越界"<<endl;
return c;
}
return cstr[i];
}
//将重构的插入提取函数设置为友元函数
friend ostream &operator<<(ostream &L, const My_string & R);
friend istream &operator>>(istream &L, My_string &R);
friend istream &getline(istream &L, My_string &R, char endflag);
};
//定义插入运算符重载函数
ostream &operator<<(ostream &L, const My_string & R)
{
L<<R.cstr;
return L;
}
//定义提取运算符重载函数
istream &operator>>(istream &L, My_string &R)
{
if(NULL != R.cstr)
{
delete []R.cstr;
R.cstr = new char[128];
L>>R.cstr;
R.len = strlen(R.cstr);
*(R.cstr+R.len) = '\0';
}else
{
R.cstr = new char[128];
L>>R.cstr;
R.len = strlen(R.cstr);
*(R.cstr+R.len) = '\0';
}
return L;
}
istream &getline(istream &L, My_string &R, char endflag)
{
if(NULL != R.cstr)
{
delete []R.cstr;
R.cstr = new char[300];
char *temp = R.cstr;
int temp_len = 0;
int flag = 0;
while(1)
{
L>>temp;
temp_len = strlen(temp);
*(temp+temp_len) = ' ';
*(temp+temp_len+1) = '\0';
R.len = strlen(R.cstr);
temp += (temp_len+1);
char *temp1 = R.cstr;
while(*temp1)
{
if(endflag == *temp1)
{
*temp1 = '\0';
flag = 1;
break;
}
temp1++;
}
if(flag)
{
break;
}
}
}else
{
R.cstr = new char[300];
char *temp = R.cstr;
int temp_len = 0;
int flag = 0;
while(1)
{
L>>temp;
temp_len = strlen(temp);
*(temp+temp_len) = ' ';
*(temp+temp_len+1) = '\0';
R.len = strlen(R.cstr);
temp += (temp_len+1);
char *temp1 = R.cstr;
while(*temp1)
{
if(endflag == *temp1)
{
*temp1 = '\0';
flag = 1;
break;
}
temp1++;
}
if(flag)
{
break;
}
}
}
return L;
}
int main()
{
My_string ms1;
My_string ms2("ni hao ");
cout<< "ms2 = " << ms2 <<endl;
ms1 = ms2;
cout << "ms1 = " << ms1 << endl;
ms1 = "welcome";
cout<<"ms1 = \"welcome\" >>>" << ms1 <<endl;
My_string ms3;
cout << "请输入字符串,注意:不能带空格,空格后的内容丢失" <<endl;
cin>>ms3;
cout << "ms3 = " << ms3 << endl;
My_string ms4;
cout << "请输入字符串,可以带空格" <<endl;
getline(cin, ms4, '#');
cout << "ms4 = " << ms4 << " ms4.len = " << ms4.size() << endl;
My_string ms5 = ms1 + ms2;
cout << "ms5 = ms1 + ms2>>>" << ms5 << endl;
cout << "ms1 == ms2?\t" << (ms1==ms2) << endl;
cout << "ms1 > ms2?\t" << (ms1>ms2) << endl;
cout << "ms1 < ms2?\t" << (ms1<ms2) << endl;
cout << "ms1 >= ms2?\t" << (ms1>=ms2) << endl;
cout << "ms1 <= ms2?\t" << (ms1<=ms2) << endl;
cout << "ms1 != ms2?\t" << (ms1!=ms2) << endl;
cout << "ms1[2] = " << ms1[2] <<endl;
return 0;
}
int my_strcmp(const char*str1, const char *str2)
{
while(*str1 || *str2)
{
str1++;
str2++;
if(*str1!=*str2)
{
return (*str1-*str2);
}
}
return (*str1-*str2);
}