day4 C++qt作业

作业

在昨天my_string的基础上,将能重载的运算符全部重载掉

关系运算符:>、<、==、>=、<=、!=

//关系运算函数

//大于>
    bool operator >(const my_string &R)const
    {
        int i;
        for(i=0;i<=(len>R.len?R.len:len);i++)
        {
            if(str[i]<R.str[i])
                return false;
            if(str[i]>R.str[i])
                return true;
        }
        return false;
    }

    bool operator >(const char* R)const
    {
        int i;
        int Rlen=strlen(R);
        for(i=0;i<=(len>Rlen?Rlen:len);i++)
        {
            if(str[i]<R[i])
                return false;
            if(str[i]>R[i])
                return true;
        }
        return false;
    }
//小于<
    bool operator <(const my_string &R)const
    {
        int i;
        for(i=0;i<=(len>R.len?R.len:len);i++)
        {
            if(str[i]>R.str[i])
                return false;
            if(str[i]<R.str[i])
                return true;
        }
        return false;
    }
    bool operator <(const char* R)const
    {
        int i;
        int Rlen=strlen(R);
        for(i=0;i<=(len>Rlen?Rlen:len);i++)
        {
            if(str[i]>R[i])
                return false;
            if(str[i]<R[i])
                return true;
        }
        return false;
    }
//等于==
    bool operator ==(const my_string &R)const
    {
        int i;
        if(len!=R.len)
            return false;
        for(i=0;i<=len;i++)
        {
            if(str[i]!=R.str[i])
                return false;
        }
        return true;
    }
    bool operator ==(const char* R)const
    {
        int i;
        int Rlen=strlen(R);
        if(len!=Rlen)
            return false;
        for(i=0;i<=len;i++)
        {
            if(str[i]!=R[i])
                return false;
        }
        return true;
    }
//大于等于>=
    bool operator >=( my_string &R)
    {
        if(*this==R || *this>R)
            return true;
        else
            return false;
    }
    bool operator >=( char *R)
    {
        if(*this==R || *this>R)
            return true;
        else
            return false;
    }
//小于等于<=
 bool operator <=( my_string &R)
 {
     if(*this==R || *this<R)
         return true;
     else
         return false;
 }
 bool operator <=( char *R)
 {
     if(*this==R || *this<R)
         return true;
     else
         return false;
 }
//不等于
    bool operator !=(my_string &R)
    {
        if(*this==R)
            return false;
        else
            return true;
    }

    bool operator !=(char *R)
    {
        if(*this==R)
            return false;
        else
            return true;
    }

加号运算符:+

    my_string & operator +(const my_string &R)
    {
        char *tem=new char[len+R.len+1];
        tem[0]=0;
        strcat(tem,str);
        delete []str;
        str=tem;
        strcat(str,R.str);
        len=len+R.len;
        str[len]=0;
        return *this;
    }

    my_string & operator +(const char *R)
    {
        int Rlen=strlen(R);
        char *tem=new char[len+Rlen+1];
        tem[0]=0;
        strcat(tem,str);
        delete []str;
        str=tem;
        strcat(str,R);
        len=len+Rlen;
        str[len]=0;
        return *this;
    }

取成员运算符:[]

 char  operator [](const int i)const
    {
        if(i>=len ||i<0)
        {
            cout<<"error []"<<endl;
            return -1;
        }
        char x;
        x=*(str+i);
        return x;
    }

赋值运算符:

   my_string &operator =(const my_string &R)
    {
        char *tem=new char[R.len+1];
        tem[0]=0;
        strcat(tem,R.str);
        delete []str;
        str=tem;
        len=R.len;
        str[len]=0;
        return *this;
    }

    my_string &operator =(const char *R)
    {
        int Rlen=strlen(R);
        char *tem=new char[Rlen+1];
        tem[0]=0;
        strcat(tem,R);
        delete []str;
        str=tem;
        len=Rlen;
        str[len]=0;
        return *this;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值