1、C+程序代码类串示范程序#include#include#include#include#includestringclass.hString:String(const char *c)/缺省构造函数 size=strlen(c); str=new charsize+1; if(str=NULL) Error(String:overflow!); strcpy(str,c);String:String(const String& s)/复制构造函数 size=s.size; str=new charsize+1; if(str=NULL) Error(String:overflow!); st
2、rcpy(str,s.str);String:String() deletestr;void String:Error(const char* c)const coutcendl; exit(1);String String:operator+(const String& s)const/类串+类串 String w; int len=size+s.size; deletew.str; w.str=new charlen+1; if(w.str=NULL) Error(operator+:overflow!); strcpy(w.str,str); strcat(w.str,s.str); w
3、.size=len; return(w);String String:operator+(const char *c)const /类串+C串 String w; int len=size+strlen(c); deletew.str; w.str=new charlen+1; if(w.str=NULL) Error(operator+:overflow!); strcpy(w.str,str); strcat(w.str,c); w.size=len; return(w); /相当于String _temp=w; 调用复制构造函数String operator+(const char *c
4、,const String& s1)/C串+类串 String w;/不能简化为return(s1+c),因为连接后的串顺序不对 deletew.str; w.size=strlen(c)+s1.size; w.str=new charw.size+1; if(w.str=NULL) /友元不能直接调用私有函数Error s1.Error(operator+:overflow!); strcpy(w.str,c); strcat(w.str,s1.str); return(w);String& String:operator=(const String& s)/复制赋值:类串=类串 if(si
5、ze!= s.size) deletestr; str=new chars.size+1; if(str=NULL) Error(operator=:overflow!); size=s.size; strcpy(str, s.str); return(*this);String& String:operator=(const char *c) /转换赋值:类串=C串 int len=strlen(c); if(size!=len) deletestr; str=new charlen+1; if(str=NULL) Error(operator=:overflow!); size=len;
6、strcpy(str,c); return(*this);String String:SubStr(int id,int num)const/取子串 String s; int len=size; int left=len-id,i; if(idlen-1) Error(id illegal!); if(numleft) Error(num illegal!); deletes.str; s.str=new charnum+1; if(s.str=NULL) Error(overflow!); s.size=num; char* p=str+id; char* p1=s.str; for(i=
7、1;i=num;i+) *p1+=*p+; *p1=0; return(s);String& String:Insert(int id,const String& s)/子串插入 char *p, *p1,*buf; int len=size; /记录本串长度 int len1=s.size; /记录子串长度 int left=len-id; /计算后段长度 int i; if(idlen) /检验插入位置的合法性 Error(id illegal!); buf=str; /保留本串的字符串 str=new charlen+len1+1; /重新分配本串的字符串空间 if(str=NULL)
8、Error(overflow!); strcpy(str, buf); /取回保留的字符串 deletebuf; /释放保留字符串的空间 p=str+len; /指向本串结束符 p1=p+len1; /指向本串结束符移动的终点 for(i=1;i=left+1;i+) /移动字符是后段字符和串结束符 *p1-=*p-; p=str+id; /指向插入的起始位置 p1=s.str; /指向待插子串的起始位置 while(*p1!=0) /逐个字符插入 *p+=*p1+; size=len+len1; /修改本串的长度 return(*this); /返回插入后的本串String& String:
9、Erase(int id,int num)/子串删除 char *p,*q,*buf; int len=size; int left=len-id; if(idlen-1) Error(id illegal!); if(numleft) Error(num illegal!); p=str+id; q=str+id+num; while(*q!=0) *p+=*q+; *p=0; buf=str; len=strlen(buf); str=new charlen +1; if(str=NULL) Error(overflow!); strcpy(str,buf); size=len; dele
10、tebuf; return(*this);bool String:operator=(const String& s)const /类串=类串 return(strcmp(str,s.str)=0);bool String:operator=(const char* c)const /类串=C串 return(strcmp(str,c)=0);bool operator=(const char* c,const String& s) /C串=类串 return(strcmp(c,s.str)=0);String:operator char*(void)const char *c=new cha
11、rsize+1; if(c=NULL) Error(overflow); strcpy(c,str); return(c);char& String:operator(int id) if(idsize-1) Error(operator:Id illegal!); return(*(str+id); /return(strid);const char& String:operator(int id)const if(idsize-1) Error(operator:Id illegal!); return(*(str+id); /return(strid);int String:Find_F
12、irst_Of(char ch,int id)const/从id开始查找ch首次出现的位置 int i=id; char *p; if(idsize-1) /检验查找起始位置合法性 Error(Start illegal!); p=str+id; while(*p!=0) if(*p=ch) break; else i+; p+; return(*p=0?-1:i); int String:Find_First_Of(const String& s,int id)const int len=s.Size(),end=Size()-1; char first=s0, last=slen-1; i
13、nt firstid,lastid; String mid,cs; if(len2) mid=s.SubStr(1,len-2); firstid=Find_First_Of(first,id); lastid=firstid+len-1; while(firstid!=-1&lastid=end) if(strlastid=last) if(len=2) return(firstid); cs=SubStr(firstid+1,len-2); if(cs=mid) return(firstid); id=firstid+1; firstid=Find_First_Of(first,id);
14、lastid=firstid+len-1; return(-1);ostream& operator(ostream& ostr,const String& s) ostr(istream& istr,String& s) char buf256; /256是输入缓冲区长度 istr buf; s=buf; /调用转换赋值函数 return(istr);int String:ReadString(istream& istr,char delimiter) char buf 256; int token=-1; if(istr.getline(buf,256,delimiter) *this= buf; /调用转换赋值函数 token=size; return(token);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1