簡易文本編輯器_第1頁
簡易文本編輯器_第2頁
簡易文本編輯器_第3頁
簡易文本編輯器_第4頁
簡易文本編輯器_第5頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、 /簡易文本編輯器,包含KMP算法,大量指針的使用#include<iostream>#include<cstring>#include<fstream>#include<iomanip>#include<string>using namespace std;struct zifu/ 錄入或者讀入的每一個字符都用鏈表進行存儲char data;zifu* pnext;struct biao_zhi /存取要被替換或刪除的字符所在位置zifu* tihuan1;zifu* tihuan2;int wei_zhi;biao_zhi* pn

2、ext;/回車和空格可以檢測到,回車和空格可以存入數組/回車的存入格式為'n',空格為' ',每一行有80個字符空間/再怎么長,沒有輸入回車也按照一行處理/i加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=get

3、char()!=EOF)/判斷輸入是否完成輸入ptemp = new zifu1;/開辟一個空間ptemp->data=ch; /暫時存著字符ptemp->pnext=NULL;/i+;/字符個數加1if(phead=NULL /*&& phead1=NULL*/ )phead=ptemp;ptail=ptemp;elseptail->pnext=ptemp;ptail=ptail->pnext;phead1=phead;while(phead1!=NULL)cout<<phead1->data;phead1=phead1->pn

4、ext;return phead;int bao_cun(zifu* ptemp3) char ch;char name30; int i=0; cout<<"輸入文件名(格式H:xxx.txt):" getchar();/丟棄第一個回車符 /獲取文件名ch=getchar(); while(ch!='n') namei=ch;ch=getchar();i+; namei='0'ofstream outFile(name);if(!outFile) cerr<<"ereor:unable to open fi

5、le!" return 1; while(ptemp3!=NULL)outFile<<ptemp3->data; /將字符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; char name30;/文件名 int i=0; cout<<"輸入文件名(格

6、式H:xxx.txt):" getchar();/丟棄第一個回車符 /獲取文件名 ch=getchar(); while(ch!='n') namei=ch;ch=getchar();i+; namei='0' if(fp=fopen(name,"rb+")=NULL) cout<<"文件不存在!"<<endl;/測試用,一定會自動建立一個getchar();return 0; /讀文件 ch=fgetc(fp); while(ch!=EOF) putchar(ch); temp= new

7、 zifu1; temp->data = ch; temp->pnext =NULL; if(read_head=NULL) read_head=temp;tail=temp; else tail->pnext=temp; tail=tail->pnext; ch=fgetc(fp); cout<<"讀入成功!"<<endl; return read_head;void insert(zifu* phead)/插入回車有點問題zifu* temp=NULL;zifu* temp1=phead;zifu* temp2=NULL;

8、int x=0,y=0;int i=1,j=0;char ch;cout<<"請輸入要插入文本的位置(x行,y列): "cout<<"請輸入行數: "cin>>x;cout<<"請輸入列數: "cin>>y;while(temp1->data!=NULL)if(i=x)/是否是要找的行數,沒有找到則不執行for(j=0;j<y-1;j+)/查找列數temp1=temp1->pnext;temp2=temp1->pnext;cout<<&qu

9、ot;輸入要插入的字符: "ch=getchar();/丟棄第一個回車符ch=getchar();while(ch!=EOF)if(ch='n');elsetemp = new zifu1;temp->data=ch;temp->pnext=NULL;temp1->pnext=temp;temp1=temp1->pnext;ch=getchar();temp1->pnext=temp2;cout<<"插入字符成功!"<<endl;break;if(temp1->data='n

10、9;)/到沒到要到達的行數,到了將進入下一行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(); / 丟棄第一個回車符cout<<"輸入要查找/替換的字符或字符串: "ch=getchar();while(ch!=EOF)if(ch='n'

11、);/如果得到回車鍵 不做任何處理elsetemp = new zifu1;temp->data=ch;temp->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'r

12、eturn mu_biao;/計算特征向量,并且返回到主函數int *Next(char *mu_biao)int m=0;/獲取目標串的長度while(mu_biaom!='0')m+;if(m<=0)return 0;/失敗int *N = new intm;N0=0;for(int i=1;i<m;i+)int k=Ni-1;while(k>0 && mu_biaoi!=mu_biaok)k=Nk-1;if(mu_biaoi=mu_biaok)Ni=k+1;elseNi=0;return N;/返回各個字符的特征向量值biao_zhi*

13、KMP(zifu* chang_chuan, char* mu_biao,int * N, int startIndex)zifu* phead=NULL;/長串的頭zifu* ptemp=NULL;/用來申請臨時空間zifu* ptemp1=NULL;zifu* phead2=NULL;/要求它比phead的移動慢一步biao_zhi* biaozhi_temp=NULL;/申請臨時空間biao_zhi* biaozhi_head=NULL;/頭biao_zhi* biaozhi_tail=NULL;/尾int L=0;/長串的長度int BIAOZHI=0;/記錄是第幾次配對情況int w

14、enben_long=0;/ 文本的總長度int lastIndex=0;phead=chang_chuan;phead2=chang_chuan;ptemp=chang_chuan;ptemp1=chang_chuan;/開辟一個臨時空間biaozhi_temp = new biao_zhi1;biaozhi_temp->pnext=NULL;biaozhi_temp->tihuan1=NULL;biaozhi_temp->tihuan2=NULL;/計算長串的長度while(ptemp1!=NULL)wenben_long+;ptemp1=ptemp1->pnext

15、;/獲取目標串的長度int l=0;while(mu_biaol!='0')l+;lastIndex=wenben_long-l;if(lastIndex-startIndex<0)/若startIndex過大,則無法匹配成功cout<<"查找失敗!"<<endl;return biaozhi_head;int i;int j=0;for(i=startIndex;i<wenben_long;i+)if(phead->data='n' && j!=l)/當長串遇到回車并且沒有匹配成功,

16、因為遇到回車標志著進入了另一行j=0;/j歸0elsewhile(mu_biaoj != phead->data && j>0)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)<<endl

17、;biaozhi_temp->tihuan2=phead->pnext;/用指針標志匹配成功的字符串的最后一個字符的后一個字符的位置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_te

18、mp = new biao_zhi1;biaozhi_temp->pnext=NULL;biaozhi_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 biaoz

19、hi_head;void zifu_delete(char* mu_biao1,zifu* head1,biao_zhi* biao_zhi1) int K;/刪除第幾處char flag='Y'/簡單標志位char flag1='Y'biao_zhi* biaozhi_temp=NULL;biaozhi_temp=biao_zhi1;zifu* head2=NULL;head2=head1;cout<<"是否全部刪除?(Y/N)"cin>>flag1;if(flag1='Y'| flag1='

20、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')cout<<head2->data;head2=head2->pnext;head2=head1;/重新歸位

21、elsewhile(1)cout<<"您要刪除第幾處?請輸入:"cin>>K;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')co

22、ut<<head2->data;head2=head2->pnext;head2=head1;/重新歸位cout<<"是否繼續?(Y/N)"cin>>flag;if(flag!='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'b

23、iao_zhi* biaozhi_temp=NULL;biaozhi_temp=biao_zhi1;zifu* head2=NULL;head2=head1;char* ti_huan; /要被換上的字符串cout<<"是否全部替換?(Y/N)"cin>>flag1;if(flag1='Y'| flag1='y') zifu* tihuantemp=NULL;ti_huan=get_zifuchuan();while(biaozhi_temp!=NULL)i=0;while(ti_huani!='0')

24、/ 連接上要拷上去的字符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=biaozhi_temp->pnex

25、t;cout<<"替換后的新串: "<<endl;biaozhi_temp=biao_zhi1;/重新歸位while(head2!='0')cout<<head2->data;head2=head2->pnext;head2=head1;/重新歸位elsewhile(1)cout<<"您要替換第幾處?請輸入:"cin>>K;while(biaozhi_temp->wei_zhi!=K)biaozhi_temp=biaozhi_temp->pnext;zif

26、u* tihuantemp=NULL;ti_huan=get_zifuchuan();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 = bia

27、ozhi_temp->tihuan2;/ 接上后串i=0;cout<<"替換后的新串: "<<endl;biaozhi_temp=biao_zhi1;/重新歸位while(head2!='0')cout<<head2->data;head2=head2->pnext;head2=head1;/重新歸位cout<<"是否繼續?(Y/N)"cin>>flag;if(flag!='Y' && flag!='y')brea

28、k;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.刪除操作 */"&

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論