#include"head.h"
#include <iostream>
#include <string.h>
#include<string>
using namespace std;
class myString
{
private:
char *str;
int size;
public:
myString():size(30)
{
str = new char [size];
strcpy(str,"");
cout << this->str<<endl;
}
myString(const char *str)
{
size = strlen(str);
this->str = new char [size+1];
strcpy(this->str , str);
cout << this->str<<endl;
}
myString(const myString &other):str(new char[other.size+1]),size(other.size)
{
int i = 0 ;
while (*(other.str+i) != '\0' )
{
this->str[i] = *(other.str+i);
i++;
}
cout << this->str<<endl;
}
myString & operator = (const myString & other)
{
if (this != &other)
{
strcpy(this->str,other.str);
this->size = other.size;
}
else
{
}
cout<<"拷贝赋值"<<endl;
cout<<this->str<<endl;
return *this;
}
myString &operator +=(const myString &other)
{
strcat(this->str,other.str);
this->size += other.size;
cout<<"+=重载"<<endl;
cout<<"size:"<<this->size<<endl;
cout<<"str:"<<this->str<<endl;
return *this;
}
bool operator == (const myString &other)const
{
return (!strcmp(this->str,other.str));
}
bool operator != (const myString &other)const
{
return strcmp(this->str,other.str);
}
bool operator < (const myString &other)const
{
return strcmp(this->str,other.str)<0? 1:0;
}
bool operator > (const myString &other)const
{
return strcmp(this->str,other.str)<0? 0:1;
}
bool operator <= (const myString &other)const
{
return strcmp(this->str,other.str)<=0? 1:0;
}
bool operator >= (const myString &other)const
{
return strcmp(this->str,other.str)<=0? 0:1;
}
~myString()
{
delete []this->str;
cout << "析构成功" << endl;
}
void judge_empty()
{
if(!*(this->str))
{
cout << "空" <<endl;
}
else
{
cout << "非空" <<endl;
}
}
int len_size ()
{
return this->size;
}
const char *c_str()
{
return this->str;
}
char at (int i)
{
return *(str+i);
}
void show()
{
cout<<this->str<<endl;
}
friend ostream &operator << ( ostream &L,const myString &R);
friend istream &operator >> ( istream &L,const myString &R);
};
ostream &operator << ( ostream &L,const myString &R)
{
L << R.str ;
return L;
}
istream &operator >> (istream &L , const myString &R)
{
L >> R.str ;
return L;
}
int main()
{
myString s1("hello");
myString s2(s1);
cout <<"s1 size:"<<s1.len_size()<<endl;
cout<<"第1个字符为"<<s1.at(0)<<endl;
cout<<"第2个字符为"<<s1.at(1)<<endl;
cout<<"第3个字符为"<<s1.at(2)<<endl;
cout << "s1";
s1.judge_empty();
cout<<"********************************************"<<endl;
myString s3 ;
s3 = s1 ;
s3 += s1;
s3.show();
s2.show();
s1.show();
if(s3 == s1)
{
cout<<"相等"<<endl;
}
else
{
cout<<"不相等"<<endl;
}
cout<<"********************************************"<<endl;
if((s3 != s1) == 0)
{
cout<<"相等"<<endl;
}
else
{
cout<<"不相等"<<endl;
}
if(s3 < s1)
{
cout<<"s3小"<<endl;
}
else
{
cout<<"s3大"<<endl;
}
if(s3 > s1)
{
cout<<"s3大"<<endl;
}
else
{
cout<<"s3小"<<endl;
}
if(s3>=s1)
{
cout<<"s3>=s1"<<endl;
}
else
{
cout<<"s3<s1"<<endl;
}
if(s3<=s1)
{
cout<<"s3<=s1"<<endl;
}
else
{
cout<<"s3>s1"<<endl;
}
cout<<s3<<endl;
cin>> s2;
s2.show();
return 0;
}