数据结构,字符串类的操作C++

上传人:公**** 文档编号:506323146 上传时间:2023-10-25 格式:DOCX 页数:4 大小:37.81KB
返回 下载 相关 举报
数据结构,字符串类的操作C++_第1页
第1页 / 共4页
数据结构,字符串类的操作C++_第2页
第2页 / 共4页
数据结构,字符串类的操作C++_第3页
第3页 / 共4页
数据结构,字符串类的操作C++_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《数据结构,字符串类的操作C++》由会员分享,可在线阅读,更多相关《数据结构,字符串类的操作C++(4页珍藏版)》请在金锄头文库上搜索。

1、数据结构,字符串类的操作 C+9 字符串 字符串是由零个或多个字符的顺序排列所组成的数据结构,字符串在计算机处理中使用非常广泛。通过本次实验理解字符串运算的原理,掌握主要算法的实现。 建立字符串类,并实现求子串、字符串赋值、字符串连接等运算符重载函数,实现字符串的模式匹配功能。编写一个能够统计字符串中各个字符出现频度的函数。 #include #include #include const defaultsize=128; class astring/定义字符串类 public: astring(int sz=defaultsize); /字符串构造函数 astring(char *init)

2、;/字符串构造函数 astring(const astring &ob);/字符串构造函数 astringdelete ch;/析构函数 int lengthconst return curlength;/求字符串的长度 astring operator(int pos,int len);/求子串 int operator=(astring &ob)const return strcmp(ch,ob.ch)=0;/=号符重载 int operator!=(astring &ob)const return strcmp(ch,ob.ch)!=0;/!=号符重载 int operator!cons

3、treturn curlength=0; astring operator=(astring &ob); astring operator+=(astring &ob); char operator(int i); int find(astring&pat)const; void outputcoutch; int asize(char &x);/统计字符x在字符串中出现的次数 private: char *ch; int curlength; int maxsize; ; astring:astring(int sz) /构造函数,把sz的值给字符串的总长度 maxsize=sz; ch=n

4、ew charmaxsize+1; if(ch=NULL)cerrdefaultsize)?len:defaultsize; ch=new charmaxsize+1; curlength=len; strcpy(ch,init); astring:astring(const astring &ob)/复制构造函数 maxsize=ob.maxsize; 1 ch=new charmaxsize+1; curlength=ob.curlength; strcpy(ch,ob.ch); astring astring:operator (int pos,int len) /求子串 astring

5、 temp; if(pos=maxsize|len= curlength) len=curlength-pos; temp.curlength=len; for(int i=0,j=pos;ilen;i+,j+) temp.chi=chj; temp.chlen=0; return temp; astring astring:operator = (astring &ob)/赋值符号重载 if(&ob!=this) deletech; ch=new charob.maxsize; if(ch=NULL) cerr存储分配失败!n;exit(1); curlength=ob.curlength;

6、 strcpy(ch,ob.ch); else cout=n)?maxsize:n; ch=new charm; if(ch=NULL)cerr存储分配失败!n;exit(1); maxsize=m;curlength=n; strcpy(ch,temp); strcat(ch,ob.ch); delete temp; return *this; char astring:operator(int i) if(i=curlength) cout字符串下标越界n;exit(1); return chi; int astring:asize(char &x)/统计字符x在字符串中出现的次数,且把出

7、现的次数返回。 int count=0; int i=0; while(icurlength) if(chi=x) count+; i+; 2 return count; void main astring a1(wo shi ); cout字符串a1为:; a1.output; coutendl; astring a2(a1); cout字符串a2为:; a2.output; coutendl; astring a3=a2;/等号的重载,字符串赋值 cout字符串a3为:; a3.output; coutendl; astring a4(Kuang JiaLiang.); cout字符串a4

8、为:; a4.output; coutendl; if(a4=a3) cout字符串a4等于字符串a3endl; else cout字符串a4不等于字符串a3endl; a3+=a4;/字符串的连接 couta3+a4=; a3.output; coutendl; couta3的第六个字符为; couta35; coutendl; char x; x=a; cout字符串a3中出现字符a的次数是: a3.asize(x)endl; 字符串a1为:wo shi 字符串a2为:wo shi 字符串a3为:wo shi 字符串a4为:Kuang JiaLiang 字符串a4不等于字符串a3 a3+a4=wo shi Kuang JiaLiang 字符串a3中出现字符a的次数是:3 字符串的实验也是书上有的实验,大部分函数都是书上有的函数。只有一个统计字符在字符串中出现的个数这个函数书上没有给出。而这个函数也是非常简单的,通过一个count计数变量就可以实现。写这个程序也增强了我对程序调试的能力。 3

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号