要求:
#include <iostream>
#include <cstring>
using namespace std;
class My_string{
private:
char *cstr;
int size;
public:
My_string():size(15){
cstr=new char[size];
cstr[0]='\0';
}
//无参构造
~My_string()
{
if(cstr != nullptr)
{
delete cstr;
}
}
//有参构造
My_string(const char *str)
{
size=strlen(str);
cstr=new char[size+1];
strcpy(cstr,str);
}
//有参构造的函数重载
My_string(int n,char ch)
{
size=n;
cstr=new char[n+1];
for(int i=0;i<n;i++)
{
cstr[i]=ch;
}
cstr[n]='\0';
}
//拷贝构造
My_string(const My_string &other):size(other.size)
{
cstr= new char[size+1];
strcpy(cstr,other.cstr);
cstr[size]='\0';
}
//拷贝赋值
My_string &operator=(const My_string &other)
{
this->size=other.size;
this->cstr= new char[size+1];
strcpy(this->cstr,other.cstr);
this->cstr[this->size]='\0';
}
//"+"运算符重载
const My_string operator+(const My_string &other)const
{
My_string temp;
temp.size=this->size+other.size;
temp.cstr=new char[temp.size+1];
memcpy(temp.cstr,this->cstr,this->size);
memcpy(temp.cstr+this->size,other.cstr,other.size);
temp.cstr[this->size+other.size]='\0';
return temp;
}
//"+="运算符重载
My_string &operator+=(const My_string &other)
{
this->size+=other.size;
memcpy(this->cstr+(this->size-other.size),other.cstr,other.size);
this->cstr[this->size]='\0';
return *this;
}
//"[]"运算符重载
char operator[](const int &index)const
{
char ch=this->cstr[index];
return ch;
}
//">"运算符重载
bool operator>(const My_string &other)const
{
return strcmp(this->cstr,other.cstr)>0;
}
//"<"运算符重载
bool operator<(const My_string &other)const
{
return strcmp(this->cstr,other.cstr)<0;
}
//">="运算符重载
bool operator>=(const My_string &other)const
{
return (strcmp(this->cstr,other.cstr)>=0 );
}
//"<="运算符重载
bool operator<=(const My_string &other)const
{
return (strcmp(this->cstr,other.cstr)<=0);
}
//"!="运算符重载
bool operator!=(const My_string &other)const
{
return (strcmp(this->cstr,other.cstr)!=0);
}
//字符串展示
const char *my_c_str()
{
return cstr;
}
//字符串大小
int my_size()
{
return size;
}
//判空
bool my_empty()
{
return sizeof(cstr)==0;
}
//按位查找
char my_at(int i)
{
cout<<size<<endl;
if(i>=size || i<0)
{
cout<<"位置不合法"<<endl;
}
else
{
char ch=cstr[i];
return ch;
}
}
//将输出流运算符的重载函数设置为友元函数
friend ostream &operator<<(ostream &L,const My_string &R);
//将插入流运算符的重载函数设置为友元函数
friend istream &operator>>(istream &L,const My_string &R);
};
//"<<"运算符重载
ostream &operator<<(ostream &L,const My_string &R)
{
L<<R.cstr;
return L;
}
//">>"运算符重载
istream &operator>>(istream &L,const My_string &R)
{
L>>R.cstr;
return L;
}
int main()
{
My_string s1(3,'z');
cout<<s1.my_c_str()<<endl;
cout<<"************"<<endl<<endl;
My_string s2("abcdefg");
cout<<s2.my_c_str()<<endl;
cout<<"************"<<endl<<endl;
My_string s3(s1);
cout<<s3.my_c_str()<<endl;
cout<<"************"<<endl<<endl;;
My_string s4;
s4=s2;
cout<<s4.my_c_str()<<endl;
cout<<"字符串的长度为: "<<s4.my_size()<<endl;
cout<<"字符串第5个元素是"<<s4.my_at(4)<<endl;
cout<<"************"<<endl<<endl;
My_string s5;
s5=s1+s2;
cout<<"s1+s2="<<s5.my_c_str()<<endl;
s5+=s1;
cout<<"s5+s1="<<s5.my_c_str()<<endl;
cout<<"s5的第5位是:"<<s5[4]<<endl;
cout<<"************"<<endl<<endl;
My_string sa="a";
My_string sb="b";
cout<<"a>b?"<<boolalpha<<(sa>sb)<<" "<<"a<b?"<<(sa<sb)<<endl;
cout<<"a>=a?"<<boolalpha<<(sa>=sa)<<" "<<"a<=a?"<<(sa<=sa)<<endl;
cout<<s5<<endl;
cout<<"************"<<endl<<endl;
My_string s6;
cin>>s6;
cout<<s6<<endl;
return 0;
}
2022.12.6