ImageVerifierCode 换一换
格式:DOCX , 页数:9 ,大小:16.09KB ,
资源ID:3818813      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3818813.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++程序代码类串示范程序.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++程序代码类串示范程序.docx

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