1/13C++
X_Mind
作业:
#include <iostream>
#include <cstring>
using namespace std;
class myString
{
private:char *str; //记录C风格的字符串int size; //记录字符串的实际长度
public://无参构造myString():size(10){str = new char[size];str[0] = '\0';}//有参构造myString(const char *s){size = std::strlen(s)+1;str = new char[size];strcpy(str,s);}//有参构造myString(int n,char ch){size = n+1;str = new char[size];for(int i=0;i<size;i++){str[i] = ch;}str[n] = '\0';/*std::memset(str,ch,n);str[n] = '\0';*/}//析构函数~myString(){delete str;str=NULL;}//拷贝构造函数myString(const myString &other):size(other.size){str = new char[size];std::strcpy(str,other.str);}//拷贝赋值函数myString &operator = (const myString &other){if(&other != this){this->size = other.size;delete []str;this->str = new char[size];std::strcpy(str,other.str);}return *this;}//判空函数bool empty() const{return str[0]=='\0';}//size函数int Size() const{return std::strlen(str);}//c_str函数const char *C_str() const{return str;}//at函数char &at(int index){if(index<0 || index>=Size()){cout<<"地址访问出界"<<endl;}return str[index];}//二倍扩容void expend(){size = 2*size;char *newstr = new char[size];std::strcpy(newstr,str);delete[] str;str = newstr;}//实现+=运算符重载myString & operator+= (const myString &other){if(Size()+other.Size()>=size){expend();}std::strcat(str,other.str);return *this;}//取地址运算符重载const myString * operator &(){return this;}//访问指定字符char & operator[](const int index){if(index<0 && index>Size()-1){cout<<"越界访问"<<endl;}return this->at(index);}//连接两个字符串const myString operator+(const myString &R)const{int newSize = std::strlen(str) + std::strlen(R.str)+1;myString temp;delete[] temp.str;temp.size = newSize;temp.str = new char[temp.size];std::strcpy(temp.str,str);std::strcat(temp.str,R.str);return temp;}//连接一个字符串和一个字符const myString operator+(const char c)const{int newSize = std::strlen(str) + 2;myString temp;delete []temp.str;temp.size = newSize;temp.str = new char[temp.size];std::strcpy(temp.str,str);std::strcat(temp.str,&c);return temp;}//比较两个字符串是否相等bool operator== (const myString &other)const{if(std::strcmp(this->str,other.str)==0){return true;}else{return false;}}//比较两个字符串是否不相等bool operator!= (const myString &other)const{if(std::strcmp(this->str,other.str)!=0){return true;}else{return false;}}//比较两个字符串的大小bool operator> (const myString &other)const{if(std::strcmp(this->str,other.str)>0){return true;}else{return false;}}//比较两个字符串的大小bool operator< (const myString &other)const{if(std::strcmp(this->str,other.str)<0){return true;}else{return false;}}//比较两个字符串的大小bool operator<= (const myString &other)const{if(std::strcmp(this->str,other.str)<=0){return true;}else{return false;}}//比较两个字符串的大小bool operator>= (const myString &other)const{if(std::strcmp(this->str,other.str)>=0){return true;}else{return false;}}//执行字符串的流输出friend std::ostream &operator<< (std::ostream &out,const myString &s){out<<s.str;return out;}//执行字符串的流输入friend std::istream &operator>> (std::istream &in,myString &s){char temp[1024];in >> temp;delete[] s.str;s.size = std::strlen(temp) +1;s.str = new char[s.size];std::strcpy(s.str,temp);return in;}//从I/O流读取数据到字符串void getline(std::istream &in,myString &s){char temp[1024];in >> temp;delete[] s.str;s.size = std::strlen(temp) +1;s.str = new char[s.size];std::strcpy(s.str,temp);}
};
int main()
{myString m1("hdllo");cout<<m1.Size()<<endl;cout<<m1.C_str()<<endl;cout<<m1.at(1)<<endl;myString m2(5,'c');cout<<m2.Size()<<endl;cout<<m2.C_str()<<endl;cout<<m2.at(1)<<endl;cout<<"*********************"<<endl;myString m3;cout<<m3.Size()<<endl;cout<<m3.C_str()<<endl;cout<<m3.at(1)<<endl;cout<<m3.empty()<<endl;myString m4("heLlo world");cout<<m4[11]<<endl;myString m5 = m4+m1;cout<<m5.at(12)<<endl;cout<<"**********************"<<endl;myString m6 = m4+'A';cout<<m6.at(11)<<endl;cout<<"**********************"<<endl;myString m7("a");myString m8("A");if(m7>m8){cout<<"m7大"<<endl;}else{cout<<"m8大"<<endl;}cout<<"**********************"<<endl;cout<<m4<<endl;cout<<"**********************"<<endl;myString m9;cin >> m9;cout<<m9<<endl;cout<<"**********************"<<endl;myString m10;m10.getline(cin,m10);cout<<m10<<endl;return 0;
}