Bootstrap

嵌入式入门Day42

@toc

作业

//main.cpp
#include <iostream>
#include "mystring.h"

using namespace std;

int main()
{
    mystring stra("Hello");
    mystring strb;

    cin >> strb;
    cout << strb << endl;
	
	strb += stra;
	cout << strb << endl;
	
	strb = strb + stra;
	cout << strb << endl;

	cin >> strb;
    cout << strb << endl;
	
    if(stra == strb)
    {
        cout << "=" << endl;
    }else if(stra > strb)
    {
        cout << ">" << endl;
    }else if(stra < strb)
    {
        cout << "<" << endl;
    }

    return 0;
}

#ifndef MYSTRING_H
#define MYSTRING_H

#include <cstring>
#include <iostream>

using namespace std;

class mystring
{
private:
    char *str;
    int size;
    int len;
public:
    //无参构造函数
    mystring();

    //有参构造函数1
    mystring(const char * s);

    //有参构造函数2
    mystring(int n, char ch);

    //拷贝构造函数
    mystring(mystring &other);



    //访问元素函数
    char &at(int i);

    //判空函数
    bool is_empty();

    //自动扩容函数
    void full();

    //获取C风格字符串
    char *get_c();

    //获取空间实际长度
    int get_size();

    //获取字符串的实际长度
    int get_len();

    //展示函数
    void show();

    //+=运算符重载
    mystring &operator+=(const mystring &other);

    //=运算符重载
    mystring &operator=(const mystring &other);

    //[]运算符重载
    char &operator[](int i);

    //+运算符重载
    mystring operator+(const mystring &other) const;

    //==运算符重载
    bool operator==(const mystring &other) const;

    // !=运算符重载
    bool operator!=(const mystring &other) const;

    //<运算符重载
    bool operator<(const mystring &other) const;

    //>运算符重载
    bool operator>(const mystring &other) const;

    //>运算符重载
    bool operator>=(const mystring &other) const;

    //>运算符重载
    bool operator<=(const mystring &other) const;

    // cout << 重载
    friend ostream &operator<<(ostream &L,const mystring &R);

    // cin >> 重载
    friend istream &operator>>(istream &L,const mystring &R);

};

#endif // MYSTRING_H

#include "mystring.h"

//无参构造函数
mystring::mystring() : str(new char[10]), size(10) , len(0)
{

}

//有参构造函数1
mystring::mystring(const char *s) : str(new char[strlen(s)]), size(strlen(s)), len(size)
{
    //参数列表:申请strlen大小的空间,size和len均设置为这个大小
    //将s中的内容拷贝到str中
    strcpy(str,s);
}

mystring::mystring(int n, char ch) : str(new char[n+1]), size(n+1) ,len(size)
{
    //参数列表:申请n+1的空间,size和len均设置为这个大小
    //拼接字符串
    for(int i= 0; i < n; i++)
    {
        str[i] = ch;
    }
    str[n+1] = 0;
}

mystring::mystring(mystring &other): str(new char[other.get_len()]), size(other.get_len()) ,len(size)
{

    //参数列表:申请大小为other对象中str大小的空间,size和len均设置为这个值
    //拷贝other中字符串的值到str中
    strcpy(str, other.str);
}



char &mystring::at(int i)
{
    //判断访问位置是否合理
    if(i>=1 && i < len)
        return str[i-1];
    else
        return str[-1];
}



bool mystring::is_empty()
{
    //判断是否为空
    return len;
}

void mystring::full()
{
    //判断修改后字符串的预计长度是否大于堆区空间的长度
    if(len > size)
    {
        //创建一个临时指针,指向新申请大小为2*size的堆区空间
        char *temp = new char[2*size];
        //将老空间中的数据移动到新空间中
        memmove(temp, str, len);
        //释放老的堆区空间
        delete []str;
        //将str指针指向新的堆区空间
        str = temp;
        //扩容完毕,size变为两倍
        size = size*2;
        //temp指针闲置,置空
        temp = NULL;
    }

}



char *mystring::get_c()
{
    //返回字符指针,用于C风格操作
    return this->str;
}

int mystring::get_size()
{
    //返回堆区空间大小
    return size;
}

int mystring::get_len()
{
    //返回字符串的实际长度
    return len;
}

void mystring::show()
{
    //输出字符串内容
    cout << str << endl;
}

mystring &mystring::operator+=(const mystring &other)
{
    //求出拼接后的长度
    len += other.len;
    //判断一下是否需要扩容
    full();
    //拼接两个字符串
    strcat(str, other.str);
    return *this;
}

mystring &mystring::operator=(const mystring &other)
{
    size = other.size;
    len = other.len;
    for(int i = 0; i < len; i++)
    {
        str[i] = other.str[i];
    }
    return *this;
}

char &mystring::operator[](int i)
{
    return str[i-1];
}

mystring mystring::operator+(const mystring &other) const
{
    mystring temp;
    temp.str = new char[this->len + other.len];
    temp.size = this->len + other.len;
    temp.len = this->len + other.len;
    strcat(temp.str, str);
    strcat(temp.str,other.str);
    return temp;
}

bool mystring::operator==(const mystring &other) const
{
    bool flag = true;
    if(len != other.len || strcmp(str,other.str))
    {
        flag = false;
    }
    return flag;
}

bool mystring::operator!=(const mystring &other) const
{
    bool flag = true;
    if(len == other.len && strcmp(str,other.str))
    {
        flag = false;
    }
    return flag;
}

bool mystring::operator<(const mystring &other) const
{
    bool flag = true;
    if(len >= other.len)
    {
        flag = false;
    }
    return flag;
}

bool mystring::operator>(const mystring &other) const
{
    bool flag = true;
    if(len <= other.len)
    {
        flag = false;
    }
    return flag;
}

bool mystring::operator>=(const mystring &other) const
{
    bool flag = true;
    if(len < other.len)
    {
        flag = false;
    }
    return flag;

}

bool mystring::operator<=(const mystring &other) const
{
    bool flag = true;
    if(len > other.len)
    {
        flag = false;
    }
    return flag;
}


istream &operator>>(istream &L, const mystring &R)
{
    L >> R.str;
    return L;
}

ostream &operator<<(ostream &L, const mystring &R)
{
    L << R.str;
    return L;
}



;