1、太原理工大学程序设计课程设计报告文本文件单词的检索与计数停车场管理程序设计课程设计 报 告 学 院:软件学院 专业班级:软件 班 学 号: 姓 名: 指导教师: 时 间: 2014年 6月27日题目1: 文本文件单词的检索与计数 1 1.1【问题描述】-1 1.2【设计需求及分析】-1 1.3【设计功能的实现】-1 1.4【实例测试及运行结果】-7 1.4.1 运行实例一-8 1.4.1 运行实例二 -8题目2: 停车场管理-9 2.1【问题描述】-9 2.2【设计需求及分析】-9 2.3【设计功能的实现】-9 2.4【实例测试及运行结果】-15 2.5【实现提示】-16设计题目一1 文本文件
2、单词的检索与计数1.1【问题描述】设计C或C+程序,统计在这样的英文文本文件中,出现了多少个单词,每个单词出现了几次。连续的英文字符都认为单词(不包括数字),单词之间用空格或标点符号分隔。 1.2【设计需求及分析】要统计英文文本文件中出现了哪些单词,就要从文件中读取字符,读取出来的连续英文字符认为是一个单词,遇空格或标点符号单词结束。使用线性表记录单词以及每个单词出现的次数。线性表中的单词按字典顺序存储。线性表的顺序存储结构如下:#define LIST_INIT_SIZE 100 /线性表存储空间的初始分配量#define LISTINCREMENT 10 /线性表存储空间的分配增量type
3、def struct char word21 /存储单词,不超过20个字符 int count; /单词出现的次数 ElemType;typedef struct ElemType *elem; /存储空间基址 int length; /当前长度int listsize; /当前分配的存储容量 Sqlist;1.3【设计功能的实现】(用C或C+语言描述)#include #include #include #define LIST_INIT_SIZE 100#define LISTINCREMENT 10typedef structchar word21;int count; ElemType
4、;typedef structElemType *elem;int length;int listsize; SqList;int InitList(SqList *p)p-elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(p-elem=NULL) return 0;p-length=0;p-listsize=LIST_INIT_SIZE;return 1;int LocateElem(SqList *p,char *word)int low,high,mid;low=0;high=p-length-1;while(lowele
5、mmid.word)=0) /表中进行二分查找p-elemmid.count+;return 0;else if(strcmp(word,p-elemmid.word)length=p-listsize)base=(ElemType*)realloc(p-elem,(p-listsize+LISTINCREMENT)*sizeof(ElemType);if(base=NULL) return 0;p-listsize=p-listsize+LISTINCREMENT; /扩充表长p-elem=base;for(j=p-length;j=i;j-)p-elemj=p-elemj-1;strcpy
6、(p-elemi-1.word,word);p-elemi-1.count=1;p-length+;return 1;void PrintList(SqList *p,int num)FILE *fw;int i;int no=num;fw=fopen(D:单词计数.txt,w);fprintf(fw,该文章共有 %d 个单词n以下按字母顺序来显示每个单词出现的次数n*n,no);fprintf(fw,单词 出现次数n,no);for(i=0;ilength;i+)fprintf(fw,%-24s %-5dn,p-elemi.word,p-elemi.count);fprintf(fw,*n)
7、;fclose(fw);/主函数void main()SqList L;char word21,ch,filename30,filename150;int num=0,i,j=0,mark=0;FILE *fp;InitList(&L);printf(请将要检索的文本文件放入D盘根目录,然后输入文件名(不用写后缀): );scanf(%s,&filename);sprintf(filename1,D:%s.txt,filename);getchar();if(fp=fopen(filename1,r)=NULL)printf(打开文件失败,请确认文件名与文件路径!n);getchar();ex
8、it(0);ch=fgetc(fp);while(ch!=EOF)if(ch=A&ch=a&ch=A&ch20)printf(文章中部分单词太长不予统计);num+;wordj=0;mark=0;j=0;i=LocateElem(&L,word);if(i0)InsertList(&L,i,word);ch=fgetc(fp);fclose(fp);printf(统计结束!请查看打开的 单词计数.txt 里面便是检索结果。);PrintList(&L,num);system(start D:单词计数.txt); getchar();#define LIST_INIT_SIZE 100#defi
9、ne LISTINCREMENT 10typedef structchar word21;int count; ElemType;typedef structElemType *elem;int length;int listsize; SqList;int InitList(SqList *p)p-elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType);if(p-elem=NULL) return 0;p-length=0;p-listsize=LIST_INIT_SIZE;return 1;int LocateElem(SqList *
10、p,char *word)int low,high,mid;low=0;high=p-length-1;while(lowelemmid.word)=0) /表中进行二分查找p-elemmid.count+;return 0;else if(strcmp(word,p-elemmid.word)length=p-listsize)base=(ElemType*)realloc(p-elem,(p-listsize+LISTINCREMENT)*sizeof(ElemType);if(base=NULL) return 0;p-listsize=p-listsize+LISTINCREMENT;
11、 /扩充表长p-elem=base;for(j=p-length;j=i;j-)p-elemj=p-elemj-1;strcpy(p-elemi-1.word,word);p-elemi-1.count=1;p-length+;return 1;void PrintList(SqList *p,int num)FILE *fw;int i;int no=num;fw=fopen(D:单词计数.txt,w);fprintf(fw,该文章共有 %d 个单词n以下按字母顺序来显示每个单词出现的次数n*n,no);fprintf(fw,单词 出现次数n,no);for(i=0;ilength;i+)f
12、printf(fw,%-24s %-5dn,p-elemi.word,p-elemi.count);fprintf(fw,*n);fclose(fw);/主函数void main()SqList L;char word21,ch,filename30,filename150;int num=0,i,j=0,mark=0;FILE *fp;InitList(&L);printf(请将要检索的文本文件放入D盘根目录,然后输入文件名(不用写后缀): );scanf(%s,&filename);sprintf(filename1,D:%s.txt,filename);getchar();if(fp=f
13、open(filename1,r)=NULL)printf(打开文件失败,请确认文件名与文件路径!n);getchar();exit(0);ch=fgetc(fp);while(ch!=EOF)if(ch=A&ch=a&ch=A&ch20)printf(文章中部分单词太长不予统计);num+;wordj=0;mark=0;j=0;i=LocateElem(&L,word);if(i0)InsertList(&L,i,word);ch=fgetc(fp);fclose(fp);printf(统计结束!请查看打开的 单词计数.txt 里面便是检索结果。);PrintList(&L,num);sys
14、tem(start D:单词计数.txt); getchar();1.3.1 实现顺序表的基本操作顺序表的初始化:InitList(SqList &L)顺序表上查找指定的单词:LocateElem(SqList &L,char *s) 若找到,单词的出现次数增1,返回0,否则返回该单词的插入位置。在顺序表上插入新的单词:InsertList(SqList &L,int i,char *s) 要求按字典顺序有序。新单词的出现次数为1.输出顺序表上存储的单词统计信息:PrintList(SqList &L) 输出文件中每个单词出现的次数以及文件中总的单词数(可输出到文件中)。1.3.2 统计单词数
15、统计过程如下:(1)输入要统计单词的文本文件名,打开相应的文件;(2)初始化顺序表;(3)从文本文件中读取字符,直到文件结束。具体描述如下:While (读文件没有结束) 过滤单词前的非字母字符; 读取一个单词,已字符串形式存储在一个字符数组中; 在线性表中查找该单词,若找到,单词的出现次数加1,否则返回其插入位置; 上一步中,若没找到,则进行插入操作; 处理下一个单词。(4)关闭文件,输出统计结果。1.4【实例测试及运行结果】1.4.1 运行实例一 文章:love(我自己写的,只有一句话) 运行结果:1.4.1 运行实例二文章:tyut运行结果: 设计题目二 2停车场管理2.1【问题描述】设
16、停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若停车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。2.2【设计需求及分析】以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数
17、据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。2.3【设计功能的实现】(用C或C+语言描述)#include#include #include#include#define MAX 10#define price 0.05 typedef struct time int hour;int min;time;typ
18、edef struct carnodechar num10;time reach;time leave;carnode;typedef struct carstackcarnode *stackMAX+1;int top;carstack;typedef struct qnodecarnode *data;struct qnode *next;qnode;typedef struct nodeqnode *head;qnode *rear;linkqueue;void initstack(carstack *s)int i;s-top=0;for(i=0;istacks-top=NULL;in
19、t initqueue(linkqueue *Q) Q-head=(qnode *)malloc(sizeof(qnode);if(Q-head!=NULL)Q-head-next=NULL;Q-rear=Q-head;return 1;elsereturn -1;/车辆到达int arrival(carstack *enter,linkqueue *w)carnode *p;qnode *t;p=(carnode *)malloc(sizeof(carnode);printf(n请您输入车牌号:);scanf(%s,&p-num);if(enter-toptop+;printf(n请您把车停
20、在 %d号 位置n,enter-top);printf(n请输入您进入停车场的时间:);scanf(%d:%d,&(p-reach.hour),&(p-reach.min);enter-stackenter-top=p;return 1;else /没有空车位printf(对不起,停车场已经没有空位了,您可以把车停在便道上!);t=(qnode *)malloc(sizeof(qnode);t-data=p;t-next=NULL;w-rear-next=t;w-rear=t;return 1;void print(carnode *p,int room)/汽车离站时缴费显示printf(n车
21、辆离开的时间:);scanf(%d:%d,&p-leave.hour,&p-leave.min);printf(n离开车辆的车牌号为:%s,p-num);printf(n其到达时间为:%02d:%02d,p-reach.hour,p-reach.min);printf(n其离开时间为:%02d:%02d,p-leave.hour,p-leave.min);printf(n应缴费用为:%.2f元,(p-leave.hour-p-reach.hour)*60+(p-leave.min-p-reach.min)*price);free(p);/车辆离开void leave(carstack *ent
22、er,carstack *temp,linkqueue *w)int room;carnode *p,*t;qnode *q;if(enter-top0)/有车while(1)printf(n请输入您的车在停车场上的位置:);scanf(%d,&(room);if(room=1&roomtop)break;while(enter-toproom)/位置不在栈顶的汽车出栈temp-top+;temp-stacktemp-top=enter-stackenter-top;enter-stackenter-top=NULL;enter-top-;p=enter-stackenter-top;ente
23、r-stackenter-top=NULL;enter-top-;while(temp-top=1)/当暂时存储汽车的栈结构中有汽车时enter-top+;enter-stackenter-top=temp-stacktemp-top;temp-stacktemp-top=NULL;temp-top-;print(p,room); /判断便道上是否有车及停车场上是否已满if(w-head!=w-rear)&enter-tophead-next;t=q-data;enter-top+;printf(n请便道上的%s号车进入%d号位置。,t-num,enter-top);printf(n请输入现在的时间:);scanf(%d:%d,&(t-reach.hour),&(t-reach.min);w-head-next=q-next;if(q=w-rear)w-rear=w-head;enter-stackenter-top=t;free(q);elseprintf(n便道里没有车!);elseprintf(n现在停车场里没有车了!); /显示停车场的信息void list1(carstack *s)int i;if(s-top0)printf(n停车场:);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1