简易文本编辑器

上传人:fgh****35 文档编号:107036690 上传时间:2022-06-14 格式:DOCX 页数:14 大小:29.54KB
收藏 版权申诉 举报 下载
简易文本编辑器_第1页
第1页 / 共14页
简易文本编辑器_第2页
第2页 / 共14页
简易文本编辑器_第3页
第3页 / 共14页
资源描述:

《简易文本编辑器》由会员分享,可在线阅读,更多相关《简易文本编辑器(14页珍藏版)》请在装配图网上搜索。

1、 /简易文本编辑器,包含KMP算法,大量指针的使用#include#include#include#include#includeusing namespace std;struct zifu/ 录入或者读入的每一个字符都用链表进行存储char data;zifu* pnext;struct biao_zhi /存取要被替换或删除的字符所在位置zifu* tihuan1;zifu* tihuan2;int wei_zhi;biao_zhi* pnext;/回车和空格可以检测到,回车和空格可以存入数组/回车的存入格式为n,空格为 ,每一行有80个字符空间/再怎么长,没有输入回车也按照一行处理/i

2、加1记录出入的字符的个数,回车空格也会统计在内/录入功能zifu* lu_ru() getchar();/丢弃第一个回车符zifu* phead=NULL;zifu* ptail=NULL;zifu* ptemp=NULL; /用来开辟临时空间zifu* phead1=NULL;/用来把所有的字符从头到尾再输出一遍char ch; /获取输入的字符/int i=0;/int j=0;while(ch=getchar()!=EOF)/判断输入是否完成输入ptemp = new zifu1;/开辟一个空间ptemp-data=ch; /暂时存着字符ptemp-pnext=NULL;/i+;/字符个

3、数加1if(phead=NULL /*& phead1=NULL*/ )phead=ptemp;ptail=ptemp;elseptail-pnext=ptemp;ptail=ptail-pnext;phead1=phead;while(phead1!=NULL)coutdata;phead1=phead1-pnext;return phead;int bao_cun(zifu* ptemp3) char ch;char name30; int i=0; cout输入文件名(格式H:xxx.txt):; getchar();/丢弃第一个回车符 /获取文件名ch=getchar(); while

4、(ch!=n) namei=ch;ch=getchar();i+; namei=0;ofstream outFile(name);if(!outFile) cerrereor:unable to open file!; return 1; while(ptemp3!=NULL)outFiledata; /将字符ch写入文件ptemp3=ptemp3-pnext;cout保存成功,请到H盘查找endl;return 0;zifu* read() FILE *fp; zifu* read_head=NULL; zifu* temp=NULL; zifu* tail=NULL; int ch; ch

5、ar name30;/文件名 int i=0; cout输入文件名(格式H:xxx.txt):; getchar();/丢弃第一个回车符 /获取文件名 ch=getchar(); while(ch!=n) namei=ch;ch=getchar();i+; namei=0; if(fp=fopen(name,rb+)=NULL) cout文件不存在!data = ch; temp-pnext =NULL; if(read_head=NULL) read_head=temp;tail=temp; else tail-pnext=temp; tail=tail-pnext; ch=fgetc(fp

6、); cout读入成功!endl; return read_head;void insert(zifu* phead)/插入回车有点问题zifu* temp=NULL;zifu* temp1=phead;zifu* temp2=NULL;int x=0,y=0;int i=1,j=0;char ch;cout请输入要插入文本的位置(x行,y列): ;coutx;couty;while(temp1-data!=NULL)if(i=x)/是否是要找的行数,没有找到则不执行for(j=0;jpnext;temp2=temp1-pnext;coutdata=ch;temp-pnext=NULL;tem

7、p1-pnext=temp;temp1=temp1-pnext;ch=getchar();temp1-pnext=temp2;cout插入字符成功!data=n)/到没到要到达的行数,到了将进入下一行temp1=temp1-pnext;/进入下一行i+;elsetemp1=temp1-pnext;char* get_zifuchuan() /获取要比较的字符串char* mu_biao;char ch;int i=0;zifu* phead=NULL;zifu* temp=NULL;zifu* ptail=NULL;ch=getchar(); / 丢弃第一个回车符coutdata=ch;tem

8、p-pnext=NULL;i+;/记录字符的个数if(phead=NULL)phead=temp;ptail=temp;elseptail-pnext=temp;ptail=ptail-pnext;ch=getchar();mu_biao= new chari+1;i=0;/字符归0while(phead!=NULL)mu_biaoi=phead-data;i+;phead=phead-pnext;mu_biaoi=0;return mu_biao;/计算特征向量,并且返回到主函数int *Next(char *mu_biao)int m=0;/获取目标串的长度while(mu_biaom!=

9、0)m+;if(m=0)return 0;/失败int *N = new intm;N0=0;for(int i=1;i0 & mu_biaoi!=mu_biaok)k=Nk-1;if(mu_biaoi=mu_biaok)Ni=k+1;elseNi=0;return N;/返回各个字符的特征向量值biao_zhi* KMP(zifu* chang_chuan, char* mu_biao,int * N, int startIndex)zifu* phead=NULL;/长串的头zifu* ptemp=NULL;/用来申请临时空间zifu* ptemp1=NULL;zifu* phead2=N

10、ULL;/要求它比phead的移动慢一步biao_zhi* biaozhi_temp=NULL;/申请临时空间biao_zhi* biaozhi_head=NULL;/头biao_zhi* biaozhi_tail=NULL;/尾int L=0;/长串的长度int BIAOZHI=0;/记录是第几次配对情况int wenben_long=0;/ 文本的总长度int lastIndex=0;phead=chang_chuan;phead2=chang_chuan;ptemp=chang_chuan;ptemp1=chang_chuan;/开辟一个临时空间biaozhi_temp = new bi

11、ao_zhi1;biaozhi_temp-pnext=NULL;biaozhi_temp-tihuan1=NULL;biaozhi_temp-tihuan2=NULL;/计算长串的长度while(ptemp1!=NULL)wenben_long+;ptemp1=ptemp1-pnext;/获取目标串的长度int l=0;while(mu_biaol!=0)l+;lastIndex=wenben_long-l;if(lastIndex-startIndex0)/若startIndex过大,则无法匹配成功cout查找失败!endl;return biaozhi_head;int i;int j=0

12、;for(i=startIndex;idata=n & j!=l)/当长串遇到回车并且没有匹配成功,因为遇到回车标志着进入了另一行j=0;/j归0elsewhile(mu_biaoj != phead-data & j0)j = Nj-1;if(mu_biaoj=phead-data)/出现单个字符匹配的情况j+;if(j=1)/如果是第一个字符匹配biaozhi_temp-tihuan1=phead2;/用指针标志第一个字符的前一个字符的位置else;if(j=l)/匹配成功了BIAOZHI+;/匹配成功的次数cout您要查找的目标串的位置为:(i-j+1+1)tihuan2=phead-p

13、next;/用指针标志匹配成功的字符串的最后一个字符的后一个字符的位置biaozhi_temp-wei_zhi=BIAOZHI;/记录这是第几次匹配成功if(biaozhi_head=NULL)/创建biao_zhi类的链表biaozhi_head=biaozhi_temp;biaozhi_tail=biaozhi_temp;elsebiaozhi_tail-pnext=biaozhi_temp;biaozhi_tail=biaozhi_tail-pnext;/开辟一个新的空间biaozhi_temp = new biao_zhi1;biaozhi_temp-pnext=NULL;biaozh

14、i_temp-tihuan1=NULL;biaozhi_temp-tihuan2=NULL;j=0;/j归0,接着重新往下搜else;phead=phead-pnext;if(i=1)phead2=phead2-pnext; /比phead的移动要慢一步if(biaozhi_head=NULL) cout匹配失败!endl;return biaozhi_head;elsereturn biaozhi_head;void zifu_delete(char* mu_biao1,zifu* head1,biao_zhi* biao_zhi1) int K;/删除第几处char flag=Y;/简单标

15、志位char flag1=Y;biao_zhi* biaozhi_temp=NULL;biaozhi_temp=biao_zhi1;zifu* head2=NULL;head2=head1;coutflag1;if(flag1=Y| flag1=y)while(biaozhi_temp!=NULL)biaozhi_temp-tihuan1-pnext = biaozhi_temp-tihuan2;/ 接上后串biaozhi_temp=biaozhi_temp-pnext;cout删除后的新串: endl;biaozhi_temp=biao_zhi1;/重新归位while(head2!=0)co

16、utdata;head2=head2-pnext;head2=head1;/重新归位elsewhile(1)coutK;while(biaozhi_temp-wei_zhi!=K)biaozhi_temp=biaozhi_temp-pnext; biaozhi_temp-tihuan1-pnext = biaozhi_temp-tihuan2;/ 接上后串cout删除后的新串: endl;biaozhi_temp=biao_zhi1;/重新归位while(head2!=0)coutdata;head2=head2-pnext;head2=head1;/重新归位coutflag;if(flag!

17、=Y & flag!=y)break;void ti_huan(char* mu_biao1,zifu* head1,biao_zhi* biao_zhi1)int K;/替换第几处int i=0;char flag=Y;/简单标志位char flag1=Y;biao_zhi* biaozhi_temp=NULL;biaozhi_temp=biao_zhi1;zifu* head2=NULL;head2=head1;char* ti_huan; /要被换上的字符串coutflag1;if(flag1=Y| flag1=y) zifu* tihuantemp=NULL;ti_huan=get_z

18、ifuchuan();while(biaozhi_temp!=NULL)i=0;while(ti_huani!=0)/ 连接上要拷上去的字符tihuantemp=new zifu1;tihuantemp-data=ti_huani;tihuantemp-pnext=NULL;biaozhi_temp-tihuan1-pnext=tihuantemp;biaozhi_temp-tihuan1=biaozhi_temp-tihuan1-pnext;i+; biaozhi_temp-tihuan1-pnext = biaozhi_temp-tihuan2;/ 接上后串biaozhi_temp=bia

19、ozhi_temp-pnext;cout替换后的新串: endl;biaozhi_temp=biao_zhi1;/重新归位while(head2!=0)coutdata;head2=head2-pnext;head2=head1;/重新归位elsewhile(1)coutK;while(biaozhi_temp-wei_zhi!=K)biaozhi_temp=biaozhi_temp-pnext;zifu* tihuantemp=NULL;ti_huan=get_zifuchuan();i=0;while(ti_huani!=0)tihuantemp=new zifu1;tihuantemp-

20、data=ti_huani;tihuantemp-pnext=NULL;biaozhi_temp-tihuan1-pnext=tihuantemp;biaozhi_temp-tihuan1=biaozhi_temp-tihuan1-pnext;i+;biaozhi_temp-tihuan1-pnext = biaozhi_temp-tihuan2;/ 接上后串i=0;cout替换后的新串: endl;biaozhi_temp=biao_zhi1;/重新归位while(head2!=0)coutdata;head2=head2-pnext;head2=head1;/重新归位coutflag;if

21、(flag!=Y & flag!=y)break;void main()int flag=1;int* next;/特征向量char* mu_biao;/要查找的目标char key;int wei_zhi;zifu* head=NULL;biao_zhi* BIAO_ZHI=NULL;while(flag=1)cout/*/endl;cout/* 欢迎使用简易文本编辑器 */endl;cout/* 功能2,3,4,5先行录入或读取文件 */endl;cout/* */endl;cout/* 1.录入操作 */endl;cout/* 2.删除操作 */endl;cout/* 3.替换操作 */

22、endl;cout/* 4.插入操作 */endl;cout/* 5.保存文件 */endl;cout/* 6.读取文件 */endl;cout/* 0.退出 */endl;cout/* */endl;cout/*/endl;couti;switch(i)case 0 : flag=0; break;case 1 : head=lu_ru(); break;case 2 : mu_biao=get_zifuchuan(); /要删除的目标 next=Next(mu_biao); /计算特征向量 BIAO_ZHI = KMP(head,mu_biao,next,0); /匹配,找到位置 zifu

23、_delete(mu_biao,head,BIAO_ZHI); /删除 break;case 3: mu_biao=get_zifuchuan();/要替换的字符串 next=Next(mu_biao);/计算特征向量 BIAO_ZHI = KMP(head,mu_biao,next,0);/匹配,找到位置 ti_huan(mu_biao,head,BIAO_ZHI);/替换 break;case 4: insert(head);break;break;case 5 : bao_cun(head);break;case 6 : head=read( );break;case 7 : default : break;while(head!=NULL)coutdata;head=head-pnext;

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!