#include <iostream>
#include <cstring>
using namespace std;
class my_string
{
private:
char *str;
int len;
public:
//无参构造
my_string(){
str=new char[128];
memset(str,0,128);
}
//有参构造
my_string(char *s){
//cout<<"调用了有参构造1"<<endl;
str=new char[128];
memset(str,0,sizeof(str));
strcpy(str,s);
}
my_string(int n,char ch){
//cout<<"调用了有参构造2"<<endl;
int i;
str=new char[128];
memset(str,0,sizeof(str));
for(i=0;i<n;i++){
str[i]=ch;
}
str[i]='\0';
//str=arr;
}
//拷贝构造
my_string(const my_string& O){
//cout<<"调用了拷贝构造"<<endl;
this->str=new char[128];
memset(str,0,sizeof(str));
strcpy(this->str,O.str);
this->len=O.len;
}
//拷贝赋值
my_string& operator=(const my_string& R){
//cout<<"调用了赋值构造"<<endl;
this->str=new char[128];
memset(str,0,sizeof(str));
strcpy(this->str,R.str);
this->len=R.len;
}
//析构函数
~my_string(){
delete str;
}
int getLen(){
return len;
}
//判空
bool my_empty(){
if(strlen(str)==0)
return true;
else
return false;
}
//求长度
int my_size(){
return strlen(str);
}
//转换为c分格字符串
char *my_str(){
return str;
}
//运算符重载
//关系运算符>
bool operator >(const my_string &R){
char *p=this->str;
char *q=R.str;
int n;
while(*p && *q && *p==*q){
p++;
q++;
}
n=*p-*q;
if(n>0)return true;
else return false;
}
//关系运算符<
bool operator <(const my_string &R){
char *p=this->str;
char *q=R.str;
int n;
while(*p && *q && *p==*q){
p++;
q++;
}
n=*p-*q;
if(n<0)return true;
else return false;
}
//关系运算符>=
bool operator >=(const my_string &R){
char *p=this->str;
char *q=R.str;
int n;
while(*p && *q && *p==*q){
p++;
q++;
}
n=*p-*q;
if(n>=0)return true;
else return false;
}
//关系运算符<=
bool operator <=(const my_string &R){
char *p=this->str;
char *q=R.str;
int n;
while(*p && *q && *p==*q){
p++;
q++;
}
n=*p-*q;
if(n<=0)return true;
else return false;
}
//关系运算符==
bool operator ==(const my_string &R){
char *p=this->str;
char *q=R.str;
int n;
while(*p && *q && *p==*q){
p++;
q++;
}
n=*p-*q;
if(n==0)return true;
else return false;
}
//关系运算符!=
bool operator !=(const my_string &R){
char *p=this->str;
char *q=R.str;
int n;
while(*p && *q && *p==*q){
p++;
q++;
}
n=*p-*q;
if(n!=0)return true;
else return false;
}
//+运算符
my_string operator +(const my_string &R)const{
my_string temp;
strcat(temp.str,this->str);
strcat(temp.str,R.str);
temp.len=this->len+R.len;
return temp;
}
//取成员运算符
char operator [](const int n){
return this->str[n-1];
}
};
int main()
{
cout<<"----------8.23---------"<<endl;
//验证有参构造
my_string str1("hello world!");
cout<<"str1:"<<str1.my_str()<<endl;
my_string str2(10,'h');
cout<<"str2:"<<str2.my_str()<<endl;
//验证拷贝构造
my_string str3=str1;
cout<<"str3"<<str3.my_str()<<endl;
//验证赋值构造
my_string str4;
str4=str3;
//验证判空
my_string str5("");
if(str5.my_empty())
cout<<"str5空"<<endl;
else
cout<<"str5非空"<<endl;
if(str4.my_empty())
cout<<"str4空"<<endl;
else
cout<<"str4非空"<<endl;
//获取长度
int len=str1.my_size();
cout<<"str1长度为:"<<len<<endl;
cout<<"----------8.24---------"<<endl;
//验证大于
my_string s1("hellq");
my_string s2("hellp");
cout<<"hellq>hellp?<<"<<(s1>s2?"yes":"no")<<endl;
s1="hello";
cout<<"hello>hellp?<<"<<(s1>s2?"yes":"no")<<endl;
//验证小于
cout<<"hello<hellp?<<"<<(s1<s2?"yes":"no")<<endl;
s1="hellq";
cout<<"hellq<hellp?<<"<<(s1<s2?"yes":"no")<<endl;
//验证大于等于
cout<<"hellq>=hellp?<<"<<(s1>=s2?"yes":"no")<<endl;
s1="hellp";
cout<<"hellp>=hellp?<<"<<(s1>=s2?"yes":"no")<<endl;
//验证小于等于
cout<<"hellp<=hellp?<<"<<(s1<=s2?"yes":"no")<<endl;
s1="hellq";
cout<<"hellq<=hellp?<<"<<(s1<=s2?"yes":"no")<<endl;
//验证等于
cout<<"hellq==hellp?<<"<<(s1==s2?"yes":"no")<<endl;
s1="hellp";
cout<<"hellp==hellp?<<"<<(s1==s2?"yes":"no")<<endl;
//验证不等于
cout<<"hellp!=hellp?<<"<<(s1!=s2?"yes":"no")<<endl;
s1="hellq";
cout<<"hellq!=hellp?<<"<<(s1!=s2?"yes":"no")<<endl;
//验证加号
s1="hello";
s2="world";
my_string s3;
s3=s1+s2;
cout<<"hello+world="<<s3.my_str()<<endl;
//验证取成员运算符
cout<<"s3[4]="<<s3[5]<<endl;
return 0;
}