#include <iostream>
#include <string>
using namespace std;
class my_string
{
private:
char *str; //字符串指针
int len; //长度
public:
my_string(){} //无参构造函数
~my_string(){} //析构函数
my_string(char *p) //有参构造函数
{
len=my_size(p);
str=new char[len+1]; //地址
for(int i=0;i<len+1;i++)
{
str[i]=*(p+i); //赋值
}
}
my_string(my_string& s) //拷贝构造函数 同类的类对象
{
this->len=s.len;
str=new char[len+1];
for(int i=0;i<len+1;i++)
{
str[i]=s.str[i];
}
}
bool operator == (my_string& S)
{
int i;
for( i=0;i<len+1;i++)
{
if(str[i]!=S.str[i])
{
break;
}
}
if((this->len==S.len)&&(i==this->len))
{
return 1;
}
else
{
return 0;
}
}
bool operator >(my_string&S)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=S.str[i])
{
break;
}
}
if(str[i]>S.str[i])
{
return 1;
}
else
{
return 0;
}
}
bool operator <(my_string&S)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=S.str[i])
{
break;
}
}
if(str[i]<S.str[i])
{
return true;
}
else
{
return false;
}
}
bool operator >=(my_string&S)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=S.str[i])
{
break;
}
}
if(str[i]>=S.str[i])
{
return true;
}
else
{
return false;
}
}
bool operator <=(my_string&S)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=S.str[i])
{
break;
}
}
if(str[i]<=S.str[i])
{
return true;
}
else
{
return false;
}
}
bool operator !=(my_string&S)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=S.str[i])
{
break;
}
}
if((this->len==S.len)&&(i==this->len))
{
return false;
}
else
{
return true;
}
}
void show()
{
cout<<"str="<<this->str<<" yu"<<endl;
}
int my_size(char *S)
{
int len=0;
while(*S!='\0')
{
len++;
S++;
}
return len;
}
bool my_empty()
{
return len==0?true:false;
}
char my_at(int i)
{
return str[i];
}
};
int main()
{
char c[]="yuyu so pretty wubalubadubdub";
char *q;
q=c;
my_string s1(q);
s1.show();
my_string s2(s1);
s2.show();
my_string s3;
s3=s2;
s3.show();
if(s1.my_empty()==true)
{
cout<<"null"<<endl;
}
else
{
cout<<"full"<<endl;
}
char a[]="yuyu so pretty wubalubadubdub";
char *p;
p=a;
my_string s4(p);
s4.show();
cout<<(s1==s4?"1":"0")<<endl;
cout<<(s1>s4?"1":"0")<<endl;
cout<<(s1<s4?"1":"0")<<endl;
cout<<(s1>=s4?"1":"0")<<endl;
cout<<(s1<=s4?"1":"0")<<endl;
cout<<(s1!=s4?"1":"0")<<endl;
cout<<"动感光波~~~哔~哔~哔~哔~~~"<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class my_string
{
private:
char *str; //字符串指针
int len; //长度
public:
my_string(){} //无参构造函数
~my_string(){} //析构函数
my_string(char *p) //有参构造函数
{
len=my_size(p);
str=new char[len+1]; //地址
for(int i=0;i<len+1;i++)
{
str[i]=*(p+i); //赋值
}
}
my_string(my_string& s) //拷贝构造函数 同类的类对象
{
this->len=s.len;
str=new char[len+1];
for(int i=0;i<len+1;i++)
{
str[i]=s.str[i];
}
}
bool operator == (my_string& s)
{
int i;
for( i=0;i<len+1;i++)
{
if(str[i]!=s.str[i])
{
break;
}
}
if((this->len==s.len)&&(i==this->len))
{
return 1;
}
else
{
return 0;
}
}
bool operator >(my_string& s)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=s.str[i])
{
break;
}
}
if(str[i]>s.str[i])
{
return 1;
}
else
{
return 0;
}
}
bool operator <(my_string& s)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=s.str[i])
{
break;
}
}
if(str[i]<s.str[i])
{
return true;
}
else
{
return false;
}
}
bool operator >=(my_string& s)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=s.str[i])
{
break;
}
}
if(str[i]>=s.str[i])
{
return 1;
}
else
{
return 0;
}
}
bool operator <=(my_string& s)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=s.str[i])
{
break;
}
}
if(str[i]<=s.str[i])
{
return true;
}
else
{
return false;
}
}
bool operator !=(my_string& s)
{
int i;
for(i=0;i<len+1;i++)
{
if(str[i]!=s.str[i])
{
break;
}
}
if((this->len==s.len)&&(i==this->len))
{
return false;
}
else
{
return true;
}
}
void show()
{
cout<<"str->"<<this->str<<" yu"<<endl;
}
int my_size(char *s)
{
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
bool my_empty()
{
return len==0?0:1;
}
char my_at(int i)
{
return str[i];
}
};
int main()
{
char c[]="yuyu so pretty wubalubadubdub";
char *q;
q=c;
my_string s1(q);
cout<<"s1.str如下:"<<endl;
s1.show();
my_string s2(s1);
cout<<"s2.str如下:"<<endl;
s2.show();
my_string s3;
cout<<"s3.str如下:"<<endl;
s3=s2;
s3.show();
char a[]="yuyu so pretty wubalubadubdub";
char *p;
p=a;
my_string s4(p);
cout<<"s4.str如下:"<<endl;
s4.show();
if(s1.my_empty()==0)
{
cout<<"s1为空 输出:null"<<endl;
}
else
{
cout<<"s1不为空 输出:full"<<endl;
}
cout<<"s4.str如下:"<<endl;
s4.show();
cout<<"s1=s4 是不是: "<<(s1==s4?"Y":"N")<<endl;
cout<<" "<<endl;
cout<<"s1>s4 是不是: "<<(s1>s4?"Y":"N")<<endl;
cout<<" "<<endl;
cout<<"s1<s4 是不是: "<<(s1<s4?"Y":"N")<<endl;
cout<<" "<<endl;
cout<<"s1>=s4 是不是: "<<(s1>=s4?"Y":"N")<<endl;
cout<<" "<<endl;
cout<<"s1<=s4 是不是: "<<(s1<=s4?"Y":"N")<<endl;
cout<<" "<<endl;
cout<<"s1!=s4 是不是: "<<(s1!=s4?"Y":"N")<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"动感光波~~~哔~哔~哔~哔~~~"<<endl;
return 0;
}