C++作业(0824-林雪阵)

定义my_string 类重载的运算符全部重载掉

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

加号运算符:+

取成员运算符:[]

赋值运算符: =

分文件编程:头文件

#ifndef MY_STRING
#define MY_STRING

#include 
#include
#include
using namespace std;

class my_string
 {
 private:
    char *str;
    int len;
 public:
/**********构造函数,类内定义************/
    //无参构造
    my_string()
    {
        this->len=0;
        this->str=NULL;
    }
    //析构函数
    ~my_string()
    {
        delete str;
        str=NULL;
    }
    //有参构造
    my_string(char c,int l)
    {
        this->str = new char[l];
        for(int i=0;ilen=l;
    }

    my_string(char *c)
    {
        this->len=strlen(c);
        this->str=new char[len+1];
        strcpy(str,c);
        cout<<"asd"<len=other.len;
        str = new char[other.len+1];
        for(int i=0;i(const my_string& ,const my_string & );
    friend bool operator<(const my_string& ,const my_string & );
    friend bool operator==(const my_string& ,const my_string & );
    friend bool operator!=(const my_string& ,const my_string & );
    friend bool operator>=(const my_string& ,const my_string & );
    friend bool operator<=(const my_string& ,const my_string & );
    friend my_string operator+(my_string& L,const my_string & R);

};

#endif // MY_STRING

功能文件

#include

//拷贝赋值
my_string & my_string::operator=(const my_string & other)
{
    delete this->str;

    this->len=other.len;
    str = new char[other.len+1];
    for(int i=0;istr;

    this->len=strlen(c);
    this->str=new char[len+1];
    strcpy(str,c);
    return *this;
}

//bool my_empty() 判空
bool my_string::my_empty()
{
    if(len==0)
        return true;
    else
        return false;
}
//int my_size() 求长度
int my_string::my_size()
{
    return len;
}
//char *my_str() 转化为c风格字符串
char *my_string::my_str()
{
    return str;
}
//关系运算符的重载:>  < == >= <= !=
bool operator>(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i*(R.str+i))
            return true;
        else if(*(L.str+i)<*(R.str+i))
            return false;
    }
    return false;
}

bool operator<(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i*(R.str+i))
            return false;
    }
    return false;
}

bool operator==(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    if(len1 != len2)
        return false;
    int size= len1;
    for(int i=0;i=(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i*(R.str+i))
            return true;
        else if(*(L.str+i)<*(R.str+i))
            return false;
    }
    return true;
}

bool operator<=(const my_string& L,const my_string & R)
{
    int len1=L.len;
    int len2=R.len;
    int size= len1>len2?len1:len2;
    for(int i=0;i*(R.str+i))
            return false;
    }
    return true;
}
//strcat   +
my_string operator+(my_string& L,const my_string & R)
{
    int size=L.len+R.len;
    char *p = new char[size+1];
    strcpy(p,L.str);
    strcat(p,R.str);
    delete L.str;
    L.str=p;
    L.len=size;
    return L;
}
//取字符
char my_string::operator[](int i)
{
    if(i>this->len || i<0)
        return 0;
    return*(this->str+i);
}

 主函数

#include 
#include
#include
using namespace std;




int main()
{
    //定义一个string类
    cout<<"=========string========="<bbb?->"<<(str1>str2?"yes":"no")<aaa?->"<<(str2>str1?"yes":"no")<"<<(str1"<<(str2aaa?->"<<(str1>str2?"yes":"no")<"<<(str1"<<(str1==str2?"yes":"no")<"<<(str1!=str2?"yes":"no")<=aaa?->"<<(str1>=str2?"yes":"no")<"<<(str1<=str2?"yes":"no")<=aab?->"<<(str1>=str2?"yes":"no")<"<<(str1<=str2?"yes":"no")<

 运行结果

C++作业(0824-林雪阵)_第1张图片

 

你可能感兴趣的:(c++,蓝桥杯,算法)