程序设计报告(2014/2015学年第一学期)题目:文档编辑器的设计与实现专业组长学号姓名组员学号姓名指导教师指导单位日期文档编辑器的设计与实现一课题内容与要求文档编辑器系统是对一个文本内容进行各种常规操作,例如:插入、删除、查找、替换等功能。
通过此课题,熟练掌握文本文件的操作及用字符数组来实现字符串操作的功能。
基本要求:(1)首先文件标准化处理:如果句子有前导空格和后导空格,则删除这些空格,单词与单词之间只保留一个空格。
(2)统计功能:可方便地统计出文档中所有出现次数最多和最少的字符串。
(3)查找与替换功能:能够查找任意一个字符串在文档中出现的次数,并可以选择全部或有选择地将其替换为另一个字符串。
(4)显示功能:编辑完成后可以显示编辑后的文档。
(5)抽取功能:根据要求将文件中的某一类字符抽取,并存入另外的文件,如抽取所有的数字、抽取所有的字母等。
(1)提供可操作的主菜单:输出个菜单,用于显示若干个可选的功能选项。
根据输入的选项来运行不同的功能,运行不同的函数。
(2)进行文本信息的载入:选择输入方式,输入文本内容,提供可操作文本。
(3)统计数据功能:输出第二子菜单,用于显示若干个可选的功能选项。
根据输入的选项来运行不同的功能,运行不同的函数。
(4)编辑数据功能:输出个菜单,用于显示若干个可选的功能选项。
根据输入的选项来运行查找、删除、插入、显示当前文本等不同的功能,运行不同的函数。
(5)退出程序:退出当前程序。
三概要设计本程序共有11个函数1、HeadWord() 标题函数,即一个输出标题,永远出现在程序的最顶端。
2、CreatWord() 文本输入函数,实现对文本的内容进行输入;3、PrintWord() 当前文本内容输出函数,实现文本内容输出;4、CountWord() 文章内容统计函数,文本内容进行统计,包括对文本内容中的大写字母、小写字母、数字、标点符号、空格以及文章所有字数的个数的统计;5、SearchWord() 文章内容查找函数,实现查找部分;6、DeleteWord() 文章内容删除函数,实现删除部分;7、InsertWord() 文章内容插入函数,实现插入部分;8、Bmenu() 第二子菜单函数,实现子菜单功能;9、AboutWord() 显示作者信息的函数;10、menu() 主菜单函数,实现可操作菜单;11、main()主函数。
(1)查找功能:图二:查找功能图图三:插入功能图图四:删除功能图图五:统计功能图四源程序代码#include<stdio.h>#include<malloc.h>#include<string>#include<iostream>#include<stdlib.h>#include<conio.h>#include<fstream>using namespace std;#define Link_Size 100int NUM,C,N;typedef struct list{char data[80];int length;struct list *next;struct list *pre;int row;} LinkList;LinkList *head;void HeadWord(){// system("cls");cout<<"\t\t****************************************************\n"; cout<<"\t\t**** 欢迎使用简单的文本编辑器****\n"; cout<<"\t\t****************************************************\n"; }LinkList *LoadWord(){LinkList *temp;char ch;int i,j;head->next=(LinkList *)malloc(sizeof(LinkList));head->pre=NULL;temp=head->next;temp->pre=NULL;temp->length=0;for(i=0;i<80;i++)temp->data[i]='\0';cout<<"继续上次输入(输入#号结束):\n";for(j=0;j<Link_Size;j++){for(i=0;i<80;i++){ch=getchar();temp->data[i]=ch;temp->length++;if(ch=='#'){NUM=j;break;}}if(ch=='#'){temp->length=i;temp->next=NULL;break;}temp->next=(LinkList *)malloc(sizeof(LinkList)) ;temp->next->pre=temp;temp=temp->next;for(i=0;i<80;i++)temp->data[i]='\0';}ofstream ocout;ocout.open("f://text.txt",ios::app);ocout<<temp->data;ocout.close();LinkList *temp;char ch; temp->row=NUM+1;system("cls");return temp;}LinkList *CreatWord(){int i,j;head->next=(LinkList *)malloc(sizeof(LinkList));head->pre=NULL;temp=head->next;temp->pre=NULL;temp->length=0;for(i=0;i<80;i++)temp->data[i]='\0';cout<<"开始创建文本,请输入文章(输入#号结束):\n"; for(j=0;j<Link_Size;j++){for(i=0;i<80;i++){ch=getchar();temp->data[i]=ch;temp->length++;if(ch=='#'){NUM=j;break;}}if(ch=='#'){temp->length=i;temp->next=NULL;break;}temp->next=(LinkList *)malloc(sizeof(LinkList)) ;temp->next->pre=temp;temp=temp->next;for(i=0;i<80;i++)temp->data[i]='\0';}ofstream ocout;ocout.open("f://text.txt");ocout<<temp->data;ocout.close();temp->row=NUM+1;system("cls");return temp;}void PrintWord(){ifstream icin;icin.open("f://text.txt");char tt[100]={0};icin.getline(tt,100,0);cout<<tt<<endl;}void CountWord(){LinkList *temp;char ch;int i,j,t;int WORD=0,word=0,space=0,num=0,punct=0,sum=0;temp=head->next;for(j=0;j<=NUM;j++){for(i=0;(i<80)&&(temp->data[i])!='#';i++){ch=temp->data[i];if((ch>='A')&&(ch<='Z'))WORD++;else if((ch>='a')&&(ch<='z'))word++;else if((ch>='0')&&(ch<='9'))num++;else if(ch==' ')space++;else if(ch==33||ch==34||ch==39||ch==44||ch==46||ch==58||ch==59||ch==63){punct++;}}sum=WORD+word+num;}while(1){cout<<"\n";HeadWord();cout<<"\t\t****************************************************\n"; cout<<"\t\t**** 文章内容统计菜单****\n"; cout<<"\t\t****************************************************\n"; cout<<"\t\t**** 1、文章中大写字母的个数****\n"; cout<<"\t\t**** 2、文章中小写字母的个数****\n"; cout<<"\t\t**** 3、文章中数字的个数****\n"; cout<<"\t\t**** 4、文章中标点符号的个数****\n"; cout<<"\t\t**** 5、文章中空格的个数****\n"; cout<<"\t\t**** 6、文章中所有字数****\n"; cout<<"\t\t**** 7、退出返回主菜单****\n"; cout<<"\t\t**** 8、直接退出本系统****\n"; cout<<"\t\t****************************************************\n"; cout<<"\t\t请选择需统计项目:";cin>>t;switch(t){case 1:system("cls");HeadWord();cout<<"文章中大写字母的个数:"<<WORD<<endl;cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 2:system("cls");HeadWord();cout<<"文章中小写字母的个数:"<<word<<endl;cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 3:system("cls");HeadWord();cout<<"文章中数字的个数:"<<num<<endl;cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 4:system("cls");HeadWord();cout<<"文章中标点符号的个数:"<<punct<<endl;cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 5:system("cls");HeadWord();cout<<"文章中空格的个数:"<<space<<endl;cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 6:system("cls");HeadWord();cout<<"文章中所有字数:"<<sum<<endl;cout<<"按回车键继续·····";getchar();getchar();system("cls");break;}if(t==7){system("cls"); break;}if(t==8) exit(0);}}void SearchWord(char *str1,LinkList* temp){char Data[20] ;int i,j,k=0,sum=0;int l=1;temp=head->next;strcpy(Data,str1);for(i=0;i<=NUM;i++){for(j=0;j<80;j++){if((temp->data[j])==Data[k]) k++;else if(Data[k]!='\0'){j=j-k;k=0;}if(Data[k]=='\0'){sum++;j=j-k+1;cout<<"\t\t\t第"<<l<<"次出现在第"<<i+1<<"行第"<<j+1<<"列\n";k=0;continue;}}temp=temp->next;}cout<<"\t\t\t字符串总共出现次数为:"<<sum<<endl;C=sum;N=i*80+j;}void DeleteWord(char *str2){ char Data[20];LinkList *temp,*term;int i,j,k,m,y,num;strcpy(Data,str2);for(y=0;y<C;y++){num=80;k=0,m=0;temp=head;for(i=0;i<=NUM;i++){term=temp;temp=temp->next;for(j=0;j<80;j++){if((temp->data[j])==Data[k]) k++;else if(Data[k]!='\0') {j=j-k;k=0;}if(Data[k]=='\0'){num=j;break;}}if(num<80) break;}for(;i<=NUM;i++){for(;j<80;j++)if(j+1<k){term->data[80-k+num]=temp->data[j+1];}elsetemp->data[j-k+1]=temp->data[j+1];}term=temp;temp=temp->next;j=0;}}}LinkList * InsertWord(LinkList *temp){char Data[20];int h,l;cout<<"\t\t请输入要插入的字符或字符串:"<<endl;getchar();gets(Data);printf("\n\t\t当前文章内容为:");PrintWord();printf("\n\t\t请输入要插入的行:");scanf("%d",&h);printf("\n\t\t请输入要插入的列:");scanf("%d",&l);int i=(h-1)*80+l;LinkList *a;int n=strlen(Data);int m ;int insertRow=i/80+1;int row=temp->row;int j;if(insertRow==row){for(m=temp->length-1;m>=(i%80)&&n>0;m--)temp->data[m+n]=temp->data[m];for(m=(i%80),j=0;m<n+(i%80);m++,j++){temp->data[m]=Data[j];}}else{int r=0;for(int p=insertRow; p<row;p++){if(p == insertRow)r=0;elser=n;for(m=temp->length-1-r;m>=0&&n>0;m--)temp->data[m+n]=temp->data[m];a=temp;temp = temp->pre;temp->length = 80;for(m = temp->length-n,j=0;m<temp->length;m++,j++)a->data[j]=temp->data[m];}for(m=temp->length-n-1;m>=(i%80);m--)temp->data[m+n]=temp->data[m];for(m=(i%80),j=0;m<(i%80)+n;m++,j++)temp->data[m] =Data[j];}return temp;}void Bmenu(LinkList *temp){char str1[20];char str2[20];int a;do{HeadWord();cout<<"\n\t\t****************************************************\n"; cout<<"\t\t**** 文章内容处理菜单****\n";cout<<"\t\t****************************************************\n"; cout<<"\t\t**** 1、查找文章中的字符或者字符串****\n"; cout<<"\t\t**** 2、删除文章中的字符或者字符串****\n"; cout<<"\t\t**** 3、向文章中插入字符或者字符串****\n"; cout<<"\t\t**** 4、显示当前文章内容****\n"; cout<<"\t\t**** 5、返回主菜单****\n"; cout<<"\t\t**** 6、直接退出系统****\n"; cout<<"\t\t****************************************************\n"; cout<<"\t\t 请选择:";cin>>a;switch(a){case 1:system("cls");HeadWord();cout<<"\t\t\t请输入您需要查找的字符或字符串:";getchar();gets(str1);SearchWord(str1,temp);cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 2:system("cls");HeadWord();cout<<"\t\t\t请输入您需要删除的字符或字符串:";getchar();gets(str2);SearchWord(str2,temp);DeleteWord(str2);cout<<"\t\t\t删除%s 后的文章为:"<<str2<<endl;PrintWord();cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 3:system("cls");HeadWord();InsertWord(temp);cout<<"\t\t\t插入字符或字符串后文章为:";PrintWord();cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 4:system("cls");HeadWord();PrintWord();cout<<"按回车键继续·····";getchar();getchar();system("cls");break;}if(a==5){system("cls");break;}if(a==6) exit(0);}while(1);}void AboutWord(){cout<<"\n\n\t\t 关于\n";cout<<"\t\t****************************************************\n";cout<<"\t\t** 谢谢使用**\n";cout<<"\t\t****************************************************\n";cout<<"\n";}void menu(LinkList *temp){int t;do{HeadWord();printf("\n");cout<<"\t\t****************************************************\n";cout<<"\t\t**** 主菜单****\n";cout<<"\t\t****************************************************\n";cout<<"\t\t**** 1、文章内容输入****\n";cout<<"\t\t**** 2、显示当前文章内容****\n";cout<<"\t\t**** 3、进入文章内容统计菜单****\n";cout<<"\t\t**** 4、进入文章内容处理菜单****\n";cout<<"\t\t**** 5、关于****\n";cout<<"\t\t**** 6、退出文本编辑器****\n";cout<<"\t\t****************************************************\n";cout<<"\t\t**** 注:第一次运行本程序时请选择1号功能****\n";cout<<"\t\t****************************************************\n";cout<<" \t\t 请选择:";cin>>t;if((t>6)&&(t<1)){cout<<"对不起,无此功能,请输入正确的功能序号!\n";}elseswitch(t){case 1:system("cls");cout<<"\t\t************继续上次输入还是重新输入?*************"<<endl;cout<<"\t\t*******1:继续输入***********2:重新输入*********\n";int s;cin>>s;switch(s){case 1:HeadWord();temp=LoadWord();break;case 2:HeadWord();temp=CreatWord();break;}case 2:system("cls");PrintWord();printf("\n");cout<<"按回车键继续·····";getchar();getchar();system("cls");break;case 3:system("cls");CountWord();break;case 4:system("cls");Bmenu(temp);break;case 5:system("cls");HeadWord();AboutWord();cout<<"按回车键继续·····";getchar();getchar();system("cls");break;}if(t==6) break;}while(1);}int main(){head=(LinkList *)malloc(sizeof(LinkList));LinkList *temp;menu(temp);return 0;}五测试结果及数据分析运行程序后,主菜单如图所示:图2 主菜单欲输入文本,输入1,按Enter键,出现下图:图3 选择输入方式:1:继续输入;2:重新输入输入2,按Enter键,出现下图:图4 创建文本输入文章内容,以#号键结束,按Enter键,出现下图:图5 主菜单输入3,按Enter键,进入文章内容统计菜单,如下图:图6 文章内容统计菜单输入1,按Enter键,出现下图:图7 输出文章中大写字母个数按Enter键继续,输入2出现下图:图8输出文章中小写字母个数按Enter键继续,出现下图:图9 输入3,按Enter键继续,出现下图:图10 输出文章中数字个数按Enter键继续,出现下图:图11 输入4,按Enter键继续,出现下图:图12 输出文章中标点符号的个数按Enter键继续,出现下图:图13 输入5,按Enter键继续,出现下图:图14 输出文章中空格个数按Enter键继续,输入6,出现下图:图15 输出文章中所有字数按Enter键继续,出现下图:图16主菜单,输入4按Enter键继续,进入文章内容处理菜单,出现下图:图17 输入1,按Enter键继续,出现下图:图18 输入o,按Enter键继续,进行查找,出现下图:图19 输出查找结果按Enter键继续,出现下图:图20 输入2按Enter键继续,出现下图:图21 输入要删除的字符或字符串按Enter键继续,出现下图:图22 输出删除结果按Enter键继续,出现下图:图23 输入要插入的字符或字符串按Enter键继续,出现下图:图24 输入所要插入的位置按Enter键继续,出现下图:图25 输出插入结果六调试过程中问题(1)输入输出问题:由于要将数据存到文件中,输入输出就不同于直接输入输出,c++的输出读到空格便会停止,用getline函数解决;(2)追加输入问题:用ocout.open("f://text.txt")输入会覆盖掉以前的内容,如果不想覆盖以前内容,通过加参数实现,即ocout.open("f://text.txt",ios::app),在两个功能设子菜单供选择;(3)循环结束问题:break与exit(0)功能在某些情况下相似,某些情况下却只能选其一,否则运行就有问题;(4)选择问题:当选择要操作的功能时,若选择数字超过列表范围,就会出现未知错误,需增设超出范围就返回的功能,例如用switch语句可以较容易解决;(5)数组空间问题:若预设空间过小,则输入长文本和进行插入等复杂操作时就会很容易出问题,预设空间过大又会造成空间浪费,如何取舍成为关键;七课程的设计与总结在本次实验中,我们选取的课题是文档编辑器的设计与实现,设计该程序的目的主要是对一个文本内容进行一些常规操作,例如:插入,删除,查找,替换等功能,以此来熟练掌握对文本文件及字符数组的操作来实现字符串操作的功能。