当前位置:文档之家› C语言课程设计报告一种简单的英文词典排版系统的实现

C语言课程设计报告一种简单的英文词典排版系统的实现

..中国地质大学本科生课程论文封面课程名称 C语言课程设计教师姓名本科生姓名本科生学号本科生专业所在院系日期:课程设计评语注:1、无评阅人签名成绩无效;2、必须用钢笔或圆珠笔批阅,用铅笔阅卷无效;3、如有平时成绩,必须在上面评分表中标出,并计算入总成绩。

目录课程设计评语 (2)目录 (3)1.课程论文题目 (4)2.程序设计思路 (4)3.功能模块图 (5)4.数据结构设计 (5)5.算法设计 (5)6.程序代码 (16)7.程序运行结果 (21)8.编程中遇到的困难及解决方法 (24)9.总结心得及良好建议 (24)10.致谢 (24)1.课程论文题目一种简单的英文词典排版系统的实现:【要求】(1)能输入和现实打入的单词;(2)能分辨出单词;(3)对重复的单词和已经输入的单词能自动排除;(4)能按A~Z的顺序排版;(5)能将运行结果以文本形式储存;(6)具有添加新单词重新排版的能力。

[提示](1)需求分析:运行结果以文本形式储存,因而要提供文件的输入输出操作;通过查找操作检查重复单词;提供排序操作现实按A~Z的顺序排版;提供插入操作添加新单词并重新排版。

另外通过键盘式菜单实现功能选择。

(2)总体设计:整个系统被设计为单词录入模块,文件存储模块和单词浏览模块。

其中单词录入模块要完成输入单词,检查是否重复,排序操作。

文件存储模块把存放单词的数组中的数据写入文件。

单词浏览模块完成英文词典的输出,即文件的输出操作。

(3)数据结构采用指针数组或二维数组。

以【Enter】键或者空格键作为单词输入结束标志,对重复的单词自动排除可选用查找方法,数据结构可采用指针和数组。

2.程序设计思路在英文词典排版当中,人工统计的速度很慢,且容易出现差错。

本英文词典排版系统的自动化加速了排版工作,提高工作效率本程序主要采用对零散的单词进行自动读取,然后按单词首字母顺序讲单词保存到文档,这样的结构化非常便于程序后续的排版工作。

排版具体过程涉及到自动排除重复单词与添加新单词并重新排版等问题,将于正问中详细介绍。

本程序实现了所有设计要求。

整个系统被设计为单词录入模块、文件存储模块和单词浏览模块。

其中单词录入模块要完成输入单词、检查是否重复、排序操作。

文件存储模块把存放单词的数组中的数据写入文件。

单词浏览模块完成英文词典的输出,即文件的输出操作。

34.数据结构设计数据结构采用指针数组或二维数组:char* dictionary[N];或char dictionary[N][20]。

其中N 是宏定义#define N 256(表示单词个数)。

(1)单词录入模块输入一个单词,存放在一个临时字符数组中,以空格或回车表示单词的结束(这也是默认操作),然后换行输出刚刚输入的单词。

采用插入排序算法的思想把该单词插入单词数组中,不同的是如果两个单词相同则不插入。

(2)文件存储模块采用fwrite 或fprintf 把单词数组输入到文件中。

(3)单词浏览模块采用fread 或fscanf 把单词从文件中读出,然后输出。

5.算法设计(1)主函数【流程图】NY【程序】#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ctype.h"#define ROWS 256#define COLS 32static FILE *fp;static char a[ROWS][COLS];char get_option(void);int b(int count);void c(char *pt[], int count);int check(char arr[], int count);void storage(char *pt[], int count);int n; //****n全局变量*****//char word[N][20];void menu()//***主界面***//{int n,w;//*变量n保存选择菜单数字,w判断输入的数字是否在功能菜单对应数字范围内*//do{puts("\t\t********************MENU********************\n\n");puts("\t\t\tWelcome to dj's program!\n\n");puts("\t\t\t\t 1.Add new word.");puts("\t\t\t\t 2.Browse all the words.");puts("\t\t\t\t 3.Search the word.");puts("\t\t\t\t 4.Sort the words.");puts("\t\t\t\t 5.Order by A-z.");puts("\t\t\t\t 6.Exit!");puts("\n\n\t\t****************************************\n");printf("Choice your number(1-6): [ ]\b\b");scanf("%d",&n);if(n<1||n>6)//*对选择的数字作判断*//{w=1;getchar();}else w=0;}while(w==1);switch(n){case 1:add();break;//*追加模块*//case 2:browse();break;//*浏览模块*//case 3:search();break;//*查找模块*//case 4:sort();break;//*分类模块*//case 5:order();break;//*排序模块*//case 6:exit(0);//*退出*//}}void main() //********主函数*********//{menu();}(2)公共函数【程序】int load()//*加载函数*//{int i,count;int start;char *pt[ROWS];char ch, len;char input;if((fp=fopen("words.txt","a+"))==NULL)//*以输出打开方式,在此前的记录被覆盖*//{printf("\nCannot open file!\n");return NULL;}for(i=0;!feof(fp);i++)fscanf(fp,"%s",&word[i]);fclose(fp);return i+1;//*返回记录个数*//}void save(int n)//*保存函数,保存n个记录*//{FILE *fp;int i;if((fp=fopen("words.txt","a+"))==NULL)//*以输出打开方式,在此前的记录被覆盖*//{printf("\nCannot open file!\n");exit(0);}for(i=0;i<n;i++)fprintf(fp,"%s",&word[i]);fclose(fp);}(3)各功能模块设计1)分类模块【程序】void sort(){int i,j,k;char c[20];if((n=load())==0){printf("\nCannot open file!\n");exit(0);}for(i=0;i<n;i++)for(j=0;j<n-i-1;j++);if(strcmp(word[j],word[j+1])>0){strcmp(c,word[j]);strcmp(word[j],word[j+1]);strcmp(word[j+1],c);}save(n);printf("Successful!^_^.\n");printf("\nNow? 1.browse all 2.back");scanf("%d",&k);if(k==1)browse();else if(k==2)menu();}2)排序模块【流程图】【程序】void order() //*排序模块*//{int a[N],i,j,t;struct words;n=load();for(i=0;i<N;i++)for(i=0;i<N-1;i++)for(j=i+i;j<N;j++)if(a[i]>a[j]){t=a[i];a[i]=a[j];a[j]=t;}for(j=0;j<N;j++)printf("%3d",a[i]); }3)修改模块【流程图】【程序】void modify(int a) //*修改模块*//{char c[20];printf("Enter the new word:");scanf("%s",c);strcpy(word[a],c);save(n);}4)删除模块【流程图】【程序】void del(int a) //*删除模块*//{int x,i,y;printf("Are you sure to delete this word?\n\t1).sure 2).no and back menu [ ]\b\b");scanf("%d",&x); //*输入要修改的单词*//if(x==1){for(i=a;i<n-1;i++) //*查找要修改的单词*//strcpy(word[i],word[i+1]);save(n-1);printf("Successful!^_^.\nNow? 1).one more 2).back menu");scanf("%d",&y);if(y==1)search();}else if(x==2){menu();}}5)追加模块【流程图】【程序】void add() //*追加模块*//{int i,x,w1,w2,w;char c[20];if((n=load())==0)exit(0);else{puts("Enter the new word!\n");scanf("%s",c); //*输入要追加的单词*//for(i=0;i<n;i++)break;}if(i<n){w=1;do{printf("The word has already exit!\n");printf("\n\nWhat do you want to do?\n\t1).enter one more 2).back menu [ ]\b\b");scanf("%d",&x);if(x<1||x>2)w1=1;elsew1=0;}while(w1==1);}else{w2=1;strcpy(word[i],c);save(n+1);printf("Successful!^_^.Now choose what you will do next?\n\t1).add another 2).back menu [ ]\b\b");do{scanf("%d",&x);if(x<1||x>2)w2=1;elsew2=0;}while(w2==1);}switch(x){case 1:add();break;case 2:menu();break;}}6)浏览(全部)模块【流程图】【程序】void browse() //*浏览(全部)模块*//{int i,w;if((n=load())==0) //*加载记录*//{printf("\nCannot open file!\n");exit(0);}for(i=0;i<n-1;i++)printf("%s/n",word[i]);puts("Successful!^_^.Now 1.back menu 2.sort");scanf("%d",&w);if(w==1)menu();else if(w==2)sort();}7【流程图】【程序】void search() //*查找模块*//{int i,x,y;char vs[20];if((n=load())==0) //*加载记录*//{printf("\nCannot open file!\n");exit(0);}printf("Enter the word what you want to search!");scanf("%s",vs); //*输入要查找的单词*//for(i=0;i<n;i++) //*查找要修改的单词*//if(strcmp(word[i],vs)==0){printf("Successful!^_^.\nThe word is:%s\n",word[i]);//*找到需要修改的单词*//printf("What would you like to do with the word?\n\t1).modify 2).delrte 3).nothing []\b\b");modify(i);else{del(i);}}if(i==n)printf("HOHO!Sorry........Not found~~~");printf("Now.....1).one more 2).back 3).exit");scanf("%d",&x);switch(x){ case 1:search();break;case 2:menu();break;case 3:exit(0);}}6.程序代码#include "stdio.h"#include "stdlib.h"#include "string.h"#include "ctype.h"#define ROWS 256#define COLS 32static FILE *fp;static char a[ROWS][COLS];char get_option(void);int b(int count);void c(char *pt[], int count);int check(char arr[], int count);void storage(char *pt[], int count);int main(void){int i,count;int start;char *pt[ROWS];char ch, len;if((fp=fopen("words.txt","a+"))==NULL){fputs("不能打开或建立文件!\n",stderr);exit(1);}fseek(fp,0L,SEEK_END);start=(int)ftell(fp)/32;count=start;rewind(fp);if(fread(a,32*sizeof(char),start,fp)==0){i=0;puts("开始创建词库");puts("请输入单词(每行一个)");puts("在新行输入END结束输入:");while(i<ROWS&&scanf("%s", a[i])==1){fflush(stdin);if(strncmp(a[i],"END",3)==0){count+=i;break;}if(check(a[i], i))continue;i++;}}puts("\t\t*********************欢迎使用字典排版系统*******************\n\n");puts(" MENU ");puts("您要做些什么?");puts("a. 显示已有的单词 b. 添加新单词");puts("c. 对已有的单词进行排序 d. 退出");puts("\n\n\t\t**********************************************************\n");while((input=get_option())!='d')puts("已有的单词:");for(i=0;i<count;i++){printf(" ");puts(a[i]);}}if(input=='b'){puts("开始创建词库");puts("请输入新的单词(每行一个)"); puts("在新行输入END结束输入: "); count=b(count);}if(input=='c'){puts("对单词进行排序:");c(pt, count);for(i=0;i<count;i++){printf(" ");puts(pt[i]);}}puts("还要做些什么?");}storage(pt,count);fclose(fp);puts("谢谢使用,再见!");return 0;}char get_option(void){char ch;while((ch=getchar())<'a'||ch>'d')puts("请输入a,b,c或者d.");}fflush(stdin);return ch;}int b(int count){int i;i=count;while(i<ROWS&&scanf("%s", a[i])==1) {fflush(stdin);if(check(a[i], i))continue;if(strncmp(a[i],"END",3)==0){count=i;break;}i++;}return count;}void c(char *pt[], int count){int i,j;char *temp;for(i=0;i<ROWS;i++)pt[i]=a[i];for(i=0;i<count;i++)for(j=i+1;j<count;j++){temp=pt[i];pt[i]=pt[j];pt[j]=temp;}}}int check(char arr[], int count){int i;int flag=0;for(i=0;i<strlen(arr);i++)if(isalpha(arr[i])==0){printf("%s不是一个单词.\n",arr);flag=1;break;}for(i=0;i<count;i++)if(strncmp(a[i],a[count],strlen(a[count])+1)==0) {puts("重复的单词!");flag=1;}return flag;}void storage(char *pt[], int count){int i,j;char ptr[ROWS][COLS];c(pt, count);for(i=0;i<count;i++)for(j=0;pt[i][j]!='\0';j++)ptr[i][j]=pt[i][j];fp=fopen("words.txt","w+");rewind(fp);7.程序运行结果(1)输入单词(2)显示打入的单词(3)能分辨出单词并对重复的单词和已经输入的单词能自动排除(4)按A—Z的顺序排版(5)添加新单词并重新排版(6)退出(7)将运行结果以文本形式(word.txt)存储8.编程中遇到的困难及解决方法在编程的过程中,很容易出现多个小问题,比如缺少“;”或者“}”。

相关主题