@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();//有参构造函数1mystring(const char * s);//有参构造函数2mystring(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];elsereturn 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;
}