2022081班作业

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值