#include<stdio.h>#include<iostream>#include<malloc.h>#include<string.h>#define Base 10000#define M 1000/*初始长度为Base,以后依次增加M*/using namespace std;class String{private:char *str; ///str为指针,len为长度,size为能容纳的最大字符数int len,size;public:///构造函数,能直接确定长度,或者用一个字符串初始化String (int maxsize=Base);String (const char *s);char *c_str() { return str; } ///返回一个指向字符串头部的C语言的指针String insert(int pos,const char c); ///在pos位置插入字符cString insert(int pos,String s); ///在pos位置插入String sString insert(int pos,const char *s); ///插入字符串String Delete(int pos); ///删除pos位置的字符String Delete(int start,int end); ///删除区间内的字符String Delete(char c); ///删除所有的c字符int copy(char *s,int num,int start); ///从start开始复制num个字符到str中int search(char c); ///返回第一个出现字符c的位置char operator [] (int pos);String operator = (String other) ; ///重载= 运算符String operator = (const char * other) ; ///还是重载,使其支持字符串直接赋值String operator + (String &other) const; ///重载,返回两个字符串连接String operator += (String &other) ; ///还是重载,在原String后添加Stringbool operator < ( String &other) ; ///重载< ,比较大小///用重载好了的< ,直接定义其他运算符bool operator > ( String &other) { return other < *this;}bool operator >= ( String &other) { return !(*this < other);}bool operator <= ( String &other) { return !(other < *this);}bool operator == ( String &other) { return (other <= *this) && (*this <= other);}bool operator != ( String &other) { return other < *this || *this < other;}void clear(); ///清空一个字符串///判断一个String是否为空bool empty() { if(len==0) return true; return false; }int length() { return len; } ///返回字符串长度int max_size() { return size; } ///返回能容纳的字符的最大数量};///重新定义输入流istream & operator >> (istream &in, String &other){char s[Base];in >> s;other=s;return in;}///重新定义输出流ostream & operator << (ostream &out,String other){out << other.c_str();return out;}///初始化,若没有参数,则以Base的大小确定String :: String (int maxsize){str=(char *)malloc(sizeof(char)*maxsize);memset(str,0,sizeof(str));len=0;size=maxsize+M;}///用字符串初始化,可以是char *s,也可以使"123"这样的字符串String :: String (const char *s){len=strlen(s);str=(char *)malloc(sizeof(char)*(len + M));memset(str,0,sizeof(str));for(int i=0;i<len;i++) str[i]=s[i];str[len]=0;size=len+M;}///在pos的位置插入字符cString String :: insert(int pos,const char c){if(pos<=0 || pos >len) return *this;char *t=(char *)malloc(sizeof(char)*size);memset(t,0,sizeof(t));bool find=false;for(int i=0,j=0;i<len;i++){if(!find && i==pos-1){i--;t[j++]=c;find=true;}elset[j++]=str[i];}t[++len]=0;free(str);str=t;return *this;}///在pos的位置插入字符串String String :: insert(int pos,const char *s){if(pos <= 0 || pos >len) return *this;int slen=strlen(s);bool find=false;char *t=(char *)malloc(sizeof(slen+size));memset(t,0,sizeof(t));for(int i=0,j=0;i<len;i++){if(!find && i == pos-1){i--;for(int k=0;k < slen;k++) t[j++]=s[k];find = true;}else t[j++]=str[i];}t[len+=slen]=0;free(str);str=t;return *this;}///在pos的位置插入StringString String :: insert(int pos,String ss){if(pos <= 0 || pos >len) return *this;char *s=ss.c_str();int slen=strlen(s);bool find=false;char *t=(char *)malloc(sizeof(slen+size));memset(t,0,sizeof(t));for(int i=0,j=0;i<len;i++){if(!find && i == pos-1){i--;for(int k=0;k < slen;k++) t[j++]=s[k];find = true;}else t[j++]=str[i];}t[len+=slen]=0;free(str);str=t;return *this;}///在pos位置删除一个字符String String :: Delete (int pos){if(pos<=0 || pos >len) return *this;char *t=(char *)malloc(sizeof(size));memset(t,0,sizeof(t));for(int i=0,j=0;i<len;i++){if(i!=pos-1)t[j++]=str[i];}t[--len]=0;free(str);str=t;return *this;}///删除区间内的所有字符String String :: Delete(int start,int end){if(start > end ||start <= 0 || start > len ||end <= 0 || end > len ) return *this;char *t=(char *)malloc(sizeof(size));for(int i=0,j=0; i < len; i++){if(i >= start-1 && i <= end-1) continue;t[j++]=str[i];}t[len-=end-start+1]=0;free(str);str=t;return *this;}///删除一个String内的所有c字符String String :: Delete(char c){int j=0;char *t=(char *)malloc(sizeof(size));for(int i=0;i < len;i++){if(str[i]==c) continue;t[j++]=str[i];}t[j]=0;len=j;free(str);str=t;return *this;}///把String内从start开始的,长度为num的字符全部赋值到以s为首地址的内存里int String :: copy(char *s,int num,int start=0){if(num<=0) return 0;if(start <= 0 || start > len) return 0;int i,slen=strlen(s);for(i=start;i<start+num && i < slen;i++){s[i]=str[i];}return i-1;}///搜索String中的c,返回第一个出现的位置int String :: search(char c){for(int i=0;i<len;i++)if(str[i]==c) return i+1;return -1; ///未找到,返回-1}///重载运算符,使其支持直接读取字符char String :: operator [] (int pos){if(pos<0 || pos>len) return 0;return str[pos];}///重载=,方便赋值String String :: operator = (String other){free(str);len=other.length();size=other.max_size();str=(char *)malloc(sizeof(char) * (size));memset(str,0,sizeof(str));char *t=other.c_str();for(int i=0;i<len;i++) str[i]=t[i];return *this;}///还是重载,支持char *s,"123456"等方式的赋值String String :: operator = (const char * other){int i=0;len=strlen(other);if(len > size){size = len + M;free(str);str=(char *)malloc(sizeof(char) * size);memset(str,0,sizeof(str));}for(i=0;i < len ;i++) str[i]=other[i];return *this;}///重载+String String :: operator + (String &other) const{char *y=other.c_str();char *t=(char *)malloc(sizeof(char)*(other.length()+size));memset(t,0,sizeof(t));int i=0,j=0,k=0;for(i = 0;i < len;i++) t[i]=str[i];for(j = 0;j < other.length();j++) t[i++]=y[j];t[i]=0;String res(t);free(t);return res;}///重载+=String String :: operator += (String &other){int res_len=len+other.length();if( size < res_len ){free(str);size = res_len ;str = (char *)malloc(sizeof(char) * (size));}char *tep=other.c_str();for(int i=0;i < other.length();i++){str[i+len]=tep[i];}len=res_len;return *this;}///重载<,使其支持两个String的直接比较///这里是严格的小于,并用< 直接重载其他比较运算符bool String :: operator < (String &other){char *tstr=other.c_str();for(int i=0;i < len && i < other.length();i++)if(str[i] != tstr[i])return str[i] < tstr[i];return false;}///清空一个Stringvoid String :: clear(){len=0;size=Base;return ;}/*主函数中是一些简单的应用,但不是全部一些类似的运算符没必要体现出来一些函数也没有必要体现出来,例如c_str(),copy()*/int main(){String a,b,x,y,z;String c(100),d("123456");String f(d);a="Hello ";b="World!";a+=b;cout<<a<<endl;cout<<"Please input two strings:";cin >> a >> b;if(a < b) cout<<"a < b"<<endl;else if(a==b) cout<<"a = b"<<endl;else cout<<"a > b"<<endl;cout<<"a + b = "<<a + b<<endl;z=a+b;cout<<"Input a char that you want to find:";cin>> c;cout<<"It's pos is "<<z.search(c[0])<<endl;cout<<"Input a char that you want to delete :";cin >> c;z.Delete(c[0]);cout<<"After deleting,it's "<<z<<endl;int pos;cout<<"Please input the string that you want to insert,and it's pos :";cin >> c >>pos;z.insert(pos,c);cout<<"After inserting,it's "<<z<<endl;z.clear();if(z.empty()) cout<<"After clearing,z is a empty string now"<<endl;//z=a+b;return 0;}。