在昨天的基础上增加:
#include <iostream>
#include <cstring>
using namespace std;
class Mystring{
private:
int len;
char* str;
public:
//无参构造函数
Mystring(){
len = 0;
str = nullptr;
}
//有参构造函数
Mystring(const char* s){
len = strlen(s);
str = new char[len+1];
strcpy(str,s);
}
//拷贝构造函数
Mystring(Mystring &s):len(s.len),str(new char[s.len+1])
{
// len = s.len;
// //str = s.str;
// new char[len+1];
strcpy(str,s.str);
}
//拷贝赋值函数
Mystring& operator=(const Mystring &s){
if(&s == this)
return *this;
len = s.len;
if(nullptr != str)
delete []str;
str = new char[s.len+1];
strcpy(str,s.str);
return *this;
}
//析构函数
~Mystring(){
cout<<&str<<"析构函数\n";
if(nullptr != str)
delete []str;
str = nullptr;
}
//c_str()
char* myc_str(){
return this->str;
}
//length()
int mylength(){
return this->len;
}
//empty()
bool myempty(){
cout<<"判空\n";
if(len == 0){
return 1;
}
return 0;
}
//at()
char& myat(int x){
if(x>=len || x<0)
throw "数组下标越界!\n";
return str[x];
}
Mystring& operator+(const Mystring &s){
len += s.len;
if(s.str!=nullptr){
char *p = new char[len+1];
bzero(p,len+1);
if(str != nullptr){
strcpy(p,str);
delete []str;
}
str = new char[len+1];
strcpy(str,p);
strcat(str,s.str);
delete []p;
}
return *this;
}
const bool operator!=(const Mystring &s){
if(str == nullptr&& s.str == nullptr)
return 0;
if(str!=nullptr&&s.str!=nullptr){
if(strcmp(str,s.str))
return 1;
return 0;
}
return 1;
}
const bool operator==(const Mystring &s){
if(str == nullptr&& s.str == nullptr)
return 1;
if(str!=nullptr&&s.str!=nullptr){
if(strcmp(str,s.str))
return 0;
return 1;
}
return 0;
}
const bool operator<(const Mystring &s){
if(s.str != nullptr){
if(str == nullptr)
return 1;
else if(strcmp(str,s.str)<0)
return 1;
}
return 0;
}
const bool operator<=(const Mystring &s){
if(str != nullptr){
if(s.str == nullptr)
return 0;
else if(strcmp(str,s.str)>0)
return 0;
}
return 1;
}
const bool operator>(const Mystring &s){
if(str != nullptr){
if(s.str==nullptr)
return 1;
else if(strcmp(str,s.str)>0)
return 1;
}
return 0;
}
const bool operator>=(const Mystring &s){
if(s.str != nullptr){
if(str == nullptr)
return 0;
else
if(strcmp(str,s.str)<0)
return 0;
}
return 1;
}
//friend << >>
friend ostream& operator<<(ostream &O,const Mystring &s);
friend istream& operator>>(istream &I,Mystring &s);
};
ostream& operator<<(ostream &O,const Mystring &s){
if(s.str != nullptr)
O<<"str = "<<s.str;
else
O<<"str = nullptr";
return O;
}
istream& operator>>(istream &I,Mystring &s){
cout<<"请输入一串字符!"<<endl;
I>>s.str;
s.len = strlen(s.str);
return I;
}
int main(int argc, const char *argv[])
{
Mystring str0;
// cout<<str0.myempty()<<endl;
Mystring str1("12345");
str0 = str1;
// cout<<"str1="<<str1.myc_str()<<"\nlen="<<str1.mylength()<<endl;
// cout<<"str0="<<str0.myc_str()<<"\nlen="<<str0.mylength()<<endl;
str0.myat(3) = 'h';
// cout<<"str0="<<str0.myc_str()<<"\nlen="<<str0.mylength()<<endl;
// try{
// str0.myat(10) = 'j';
// }catch(const char* err){
// cerr<<err;
// //cout<<str0.myc_str()<<endl;
// }
Mystring str2 = str0+str1;
cout<<str2<<endl;
bool a = str0>str1,b=str0>=str0;
cout<<"a="<<a<<"b="<<b<<endl;
cin>>str0;
a = str0>str1;
b=str0>=str0;
cout<<"a="<<a<<"b="<<b<<endl;
return 0;
}