#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
public:
MyString();
MyString(const char *str);
MyString(const MyString &obj);
~MyString()
{
delete this->pString;
}
//[]重载
char & operator[](int index);
//=重载
MyString& operator=(const char * str);
MyString& operator=(const MyString& str);
//字符串拼接 重载+号
MyString operator+(const char * str );
MyString operator+(const MyString& str);
//字符串比较
bool operator== (const char * str);
bool operator== (const MyString& str);
bool operator!= (const char * str);
bool operator!= (const MyString& str);
bool operator>= (const MyString& str);
bool operator<= (const MyString& str);
bool operator<(const MyString& str);
bool operator>(const MyString& str);
// void StringShow();
friend ostream & operator<<(ostream & output,MyString & obj);
private:
char *pString;
int size;
};
MyString::MyString()
{
this->pString = NULL;
this->size = 0;
}
MyString::MyString(const char *str)
{
this->pString = new char[strlen(str)+1];
this->size = strlen(str);
for(unsigned int i = 0;i < strlen(str)+1;i++)
{
this->pString[i] = str[i];
}
}
MyString::MyString(const MyString &obj)
{
this->pString = new char[strlen(obj.pString)+1];
strcpy(this->pString,obj.pString);
this->size = obj.size;
}
char & MyString::operator[](int index)
{
if(index > this->size || index < 0)
{
cout<<"查询错误"<<endl;
}
return this->pString[index];
}
MyString& MyString::operator=(const char * str)
{
this->pString = new char[strlen(str)+1];
strcpy(this->pString,str);
this->size = strlen(str);
return *this;
}
MyString& MyString::operator=(const MyString& str)
{
this->pString = new char[strlen(str.pString)+1];
strcpy(this->pString,str.pString);
this->size = strlen(str.pString);
return *this;
}
MyString MyString::operator+(const MyString& str)
{
strcat(this->pString,str.pString);
this->size += strlen(str.pString);
return *this;
}
MyString MyString::operator+(const char * str )
{
strcat(this->pString,str);
this->size += strlen(str);
return *this;
}
ostream & operator<<(ostream & output,MyString & obj)
{
for(int i = 0;i < obj.size+1;i++)
{
output<<obj.pString[i];
}
return output;
}
bool MyString::operator== (const char * str)
{
if(strlen(str) == (unsigned int)this->size)
{
for(int i = 0;i < size+1;i++)
{
if(str[i] != this->pString[i])
return false;
}
return true;
}
else
return false;
}
bool MyString::operator== (const MyString& str)
{
if(str.size == this->size)
{
for(int i = 0;i < size+1;i++)
{
if(str.pString[i] != this->pString[i])
return false;
}
return true;
}
else
return false;
}
bool MyString::operator!= (const char * str)
{
if(strlen(str) == (unsigned int)this->size)
{
for(int i = 0;i < size+1;i++)
{
if(str[i] != this->pString[i])
return true;
}
return false;
}
else
return true;
}
bool MyString::operator!= (const MyString& str)
{
if(str.size == this->size)
{
for(int i = 0;i < size+1;i++)
{
if(str.pString[i] != this->pString[i])
return true;
}
return false;
}
else
return true;
}
bool MyString::operator>= (const MyString& str)
{
if(strcmp(this->pString,str.pString) >= 0)
return true;
else
return false;
}
bool MyString::operator<= (const MyString& str)
{
if(strcmp(this->pString,str.pString) <= 0)
return true;
else
return false;
}
bool MyString::operator> (const MyString& str)
{
if(strcmp(this->pString,str.pString) > 0)
return true;
else
return false;
}
bool MyString::operator< (const MyString& str)
{
if(this->size < str.size)
return true;
else
return false;
}
int main()
{
MyString str1("123");
MyString str2(str1);
MyString str3 = "321";
cout<<str2<<endl<<str3;
cout<<(str2>str1)<<endl;
cout<<(str3>=str1)<<endl;
return 0;
}
2022081班作业
最新推荐文章于 2024-10-05 19:23:25 发布