简易文本编辑器.docx
- 文档编号:9809191
- 上传时间:2023-05-21
- 格式:DOCX
- 页数:22
- 大小:85.04KB
简易文本编辑器.docx
《简易文本编辑器.docx》由会员分享,可在线阅读,更多相关《简易文本编辑器.docx(22页珍藏版)》请在冰点文库上搜索。
简易文本编辑器
简易文本编辑器
摘要:
简易文本编辑是最常用的文档提取和编辑工具,通过对简易文本的研究,利用C语言实现对文本的查找,替换(等长,不等长),插入(插串,文本块的插入)、块移动(行块,列块移动),删除;可正确存盘、取盘;正确显示总行数等功能。
关键字:
文件提取,替换,查找,插入,删除,块移动.
前言
数据结构这门课程是打开程序设计知识宝库的金钥匙,其主要目的是培养将现实世界偶像为数据和数据模型的能力以及利用计算机进行数据存储和数据加工的能力。
学好数据结构,可以使我们掌握更多的程序设计技巧,为以后学习计算机专业课程打下良好的基础。
然而,要学习好数据结构这门课,就必须加强自己动手写程序的能力,在这方面除了上课期间的课后实验,一个完整的课程设计也是一项提高自己抽象思维能力和编程能力的重要方法。
这次学习中,我选择了简易文本编辑器这个课题,根据对这个题目的研究,做了以下分析:
在操作系统中,外存中数据,文档或程序,都是以文件的形式保存。
因此对文件的操作就要首先从文件中读取数据,然后对读取的文件进行其他操作。
最后,当操作完成后,再将文件从新写入文件中,在此过程中需要掌握以下信息。
1.文件当前的读写位置。
2.与该文件对应的内存缓冲区的位置。
3.缓冲区中未被处理的字符串。
4.文件操作方式。
最后完成对文件数据的操作,重新调用文件指针将文件写入文件操作。
《数据结构》
课程设计任务书
一、设计目的
通过《数据结构》课程设计,使学生能够将课堂上学到数据结构理论知识与实际问题相联系,在提高学生学习兴趣的同时逐渐培养实际操作技能,强化对课程内容的了解。
本课程设计不仅有助于学生提高学生的编程能力,而且也有助于培养学生抽象思维能力和动手能力。
二、设计教学内容
完成简单的文本编辑功能,最好具有图形菜单界面。
包括:
查找,替换(等长,不等长),插入(插串,文本块的插入)、块移动(行块,列块移动),删除;可正确存盘、取盘;正确显示总行数。
三、设计时间
2011—2012学年第1学期:
第16周 共计1周
教师签名:
2010年12月19日
第一章.需求分析
一个简易文本编辑器应该具有图形菜单界面,包括查找,替换(等长,不等长),插入(插串,文本块的插入)、块移动(行块,列块移动),删除文本信息等功能并可正确存盘、取盘,正确显示总行数。
第二章.概要设计
2.1结构与算法分析
为实现数据的有序存储,该编辑器应该用顺序表来存储输入的信息。
顺序表是数据结构中线性表的一种,它是用一块地址连续的存储空间依次存储线性表的元素。
其特点为:
在顺序表上逻辑关系相邻的俩个元素在物理位置上也相邻;在顺序表上可以随即存取表中的元素。
在编辑器的主界面中应有如下提示信息:
2.11清空以前的文本信将用数组存的数据内容全部置为0,显示当前文本信息:
遍历顺序表中存入的信息,并输出到显示器上。
2.12编辑信息:
定义一个结构体,并在结构体中定义一个字符型的一维数组和两个整型变量,整型变量用于记录一维数组中存入数据的的行数与列数。
typedefstruct
{
charsr[MAXSIZE];
inthang;
intlie;
}shuru;
2.13替换文本信息:
首先在数组中查找要被替换的信息,如果找到该信息,提示输入要替换的信息内容,否则提示未找到要被替换的信息。
2.14插入文本信息:
首先在数组中查找要插入点,如果找到该插入点,提示输入插入信息,确认插入信息后,提示选择向前插入信息还是向后插入信息,如果未找到插入点,显示未找到要插入的位置。
2.15移动文本信息:
首先在数组中查找要移动的信息,如果找到该信息,提示是进行列移动还是进行行移动,否则提示未找到要移动的信息。
2.16删除文本信息:
首先在数组中查找要删除的信息,如果找到该信息,提示是否确认删除该信息,通过确认来删除信息,如果未找到要删除的信息,提示未找到该信息。
2.2流程图
图2—1文本编辑器流程
第三章.详细设计
#include"stdafx.h"
#include"stdio.h"
#include"malloc.h"
#include"string.h"
#include"iostream.h"
#include"stdlib.h"
#include"conio.h"
#include
#defineLink_Size100
intNUM,C,N;
typedefstructlist
{//1、定义链表结点
chardata[50];//记录一行字符
intlength;//记录一行字符长度
structlist*next;
structlist*prior;
introw;
}LinkList;
LinkList*head;
///////////////////////////////////////////////////////////////////////////////
LinkList*CreatWord()
{//编辑文本内容
LinkList*temp;
charch;
inti,j;
head->next=(LinkList*)malloc(sizeof(LinkList));
head->prior=NULL;
temp=head->next;
temp->prior=NULL;
temp->length=0;
for(i=0;i<50;i++)
temp->data[i]='\0';
printf("开始创建文本,请输入文章(输入#号结束):
\n");
for(j=0;j { for(i=0;i<50;i++)//控制一行 { ch=getchar();//接收输入字符 temp->data[i]=ch; temp->length++; if(ch=='#') { NUM=j; break;//文章结束时,Num来记录整个文章的行数 } } if(ch=='#') { temp->length=i; temp->next=NULL; break; } temp->next=(LinkList*)malloc(sizeof(LinkList)); temp->next->prior=temp; temp=temp->next; for(i=0;i<50;i++) temp->data[i]='\0'; } temp->row=NUM+1; system("cls"); returntemp; } /////////////////////////////////////////////////////////////////////////////// voidPrintWord() {//显示当前文章内容 inti,j;//定义文本行数变量j,每行字符数i LinkList*p; p=head->next; system("cls"); printf("\n当前文章的内容是: "); for(j=0;j<=NUM&&p! =NULL;j++) { for(i=0;(i<50)&&(p->data[i])! ='#';i++) { printf("%c",p->data[i]); } p=p->next; } } /////////////////////////////////////////////////////////////////////////////////// voidSearchWord(char*str1,LinkList*temp) {//查找文章中的字符或者字符串 charData[20];//记录需要查找的字符内容 inti,j,k=0,sum=0; intl=1; temp=head->next; strcpy(Data,str1); for(i=0;i<=NUM;i++) { for(j=0;j<50;j++) { if((temp->data[j])==Data[k])k++;//将输入的查找字符与链表中信息比较,找到第一个相同的字符 elseif(Data[k]! ='\0') { j=j-k; k=0; } if(Data[k]=='\0') { sum++; j=j-k+1;//从主串第j-k个位置重新查找 printf("\t\t第%d次出现在第%d行第%d列\n",l,i+1,j+1); l++; k=0; continue; } } temp=temp->next; } printf("\t\t\t字符串总共出现次数为: %d\n\n",sum); C=sum; N=i*50+j; } ////////////////////////////////////////////////////////////////////////////// voidDeleteWord(char*str2) {//从主串第j-k个位置重新查找 charData[20];//存储需要删除的字符或者字符串 LinkList*temp,*term; inti,j,k,m,y,num; strcpy(Data,str2); for(y=0;y { num=50; k=0,m=0; temp=head; for(i=0;i<=NUM;i++) { term=temp; temp=temp->next; for(j=0;j<50;j++) { if((temp->data[j])==Data[k])k++; elseif(Data[k]! ='\0'){j=j-k;k=0;} if(Data[k]=='\0') { num=j; break; } } if(num<50)break; } for(;i<=NUM;i++) { for(;j<50;j++) { if(j+1 { term->data[50-k+num]=temp->data[j+1];//删除的字符串不在最后一行,将下一行的字符(由temp指向)前移到前行 } else temp->data[j-k+1]=temp->data[j+1]; } term=temp; temp=temp->next; j=0; } } } ///////////////////////////////////////////////////////////////////////////////////////////// LinkList*InsertWord(LinkList*temp) {//向文章中插入字符或者字符串 charData[20]; inth,l; printf("\n\t\t请输入要插入的字符或字符串: "); getchar(); gets(Data); printf("\n\t\t当前文章内容为: "); PrintWord(); printf("\n\t\t请输入要插入的行: "); scanf("%d",&h); printf("\n\t\t请输入要插入的列: "); scanf("%d",&l); inti=(h-1)*50+l; LinkList*a; intn=strlen(Data); intm; intinsertRow=i/50+1;//确定插入位置的行数 introw=temp->row;//将全局变量记录的行数赋值给row intj; if(insertRow==row) { for(m=temp->length-1;m>=(i%50)&&n>0;m--) temp->data[m+n]=temp->data[m]; for(m=(i%50),j=0;m { temp->data[m]=Data[j]; } } else { intr=0; for(intp=insertRow;p { if(p==insertRow)//判断插入位置是否在最后一行 r=0; else r=n; for(m=temp->length-1-r;m>=0&&n>0;m--) temp->data[m+n]=temp->data[m]; a=temp; temp=temp->prior; temp->length=50; for(m=temp->length-n,j=0;m a->data[j]=temp->data[m]; } for(m=temp->length-n-1;m>=(i%50);m--) temp->data[m+n]=temp->data[m]; for(m=(i%50),j=0;m<(i%50)+n;m++,j++) temp->data[m]=Data[j]; } returntemp; } ///////////////////////////////////////////////////////////////////////////////////// intCountWord(LinkList*temp) {//文章中所有字数 charch; inti,j; intsum=0; temp=head->next; for(j=0;j<=NUM;j++) { for(i=0;(temp->data[i])! ='#';i++) { ch=temp->data[i]; sum++; } } return(sum-1); } ///////////////////////////////////////////////////////////////////////////// voidStorageWord() {//存盘 LinkList*t; charch; t=head->next; inti,j; ofstreamoutFile("b.txt",ios: : out); if(! outFile) cerr<<"Openfileorcreatefileerror."< else for(j=0;j<=NUM;j++) { for(i=0;(i<60)&&(t->data[i])! ='#';i++) { ch=t->data[i]; outFile< } } cout< } /////////////////////////////////////////////////////////////////////////////////////////////// voidReadWord() {//取盘 ifstreaminFile; inFile.open("b.txt"); if(! inFile) { cout<<"error! "< abort(); } charch; while(inFile.get(ch)) { cout< } inFile.close(); cout< } ///////////////////////////////////////////////////////////////////////// voidmenu(LinkList*temp) { charstr1[20]; charstr2[20]; intt; do{ printf("\n"); printf("\t\t*****************************************************\n"); printf("\t\t****欢迎使用简易文本编辑器****\n"); printf("\t\t*****************************************************\n"); printf("\t\t****1、编辑文本内容****\n"); printf("\t\t****2、显示当前文章内容****\n"); printf("\t\t****3、查找文章中的字符或者字符串****\n"); printf("\t\t****4、删除文章中的字符或字符串****\n"); printf("\t\t****5、向文章中插入字符或者字符串****\n"); printf("\t\t****6、文章中所有字数****\n"); printf("\t\t****7、文章存盘****\n"); printf("\t\t****8、文章取盘****\n"); printf("\t\t****9、退出****\n"); printf("\t\t*****************************************************\n"); printf("\t\t请选择: "); scanf("%d",&t); if((t>9)&&(t<1)) { printf("对不起,无此功能,请输入正确的功能序号! \n"); } else switch(t) { case1: system("cls"); temp=CreatWord(); printf("\n"); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; case2: system("cls"); PrintWord(); printf("\n"); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; case3: system("cls"); printf("\t\t\t请输入您需要查找的字符或字符串: "); getchar(); gets(str1); SearchWord(str1,temp); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; case4: system("cls"); printf("\t\t\t请输入您需要删除的字符或字符串: "); getchar(); gets(str2); SearchWord(str2,temp); DeleteWord(str2); printf("\t\t\t删除%s后的文章为: ",str2); PrintWord(); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; case5: system("cls"); InsertWord(temp); printf("\t\t\t插入字符或字符串后文章为: "); PrintWord(); printf("\n"); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; case6: system("cls"); printf("\n\t\t文章中所有字数: %d\n",CountWord(temp)); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; break; case7: system("cls"); printf("\t\t\t文档存盘成功: "); StorageWord(); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; case8: system("cls"); printf("\t\t\t文档取盘成功: "); ReadWord(); printf("按回车键继续·····"); getchar(); getchar(); system("cls"); break; } if(t==9)break; }while (1); } ////////////////////////////////////////////////////////////////////////////////////////// voidmain() { head=(LinkList*)malloc(sizeof(LinkList)); LinkList*temp; menu(temp); } 测试结果 测试文章: Fortoday'sChristian,theoriginofChristmasis,andshouldbe,thebirthofJesusChristasrecordedintheBible.Nothingmoreandnothingless.However,mostofwhatwewitnessonDecember25theachyearhasabsolutelynothi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 简易 文本 编辑器