程序课程设计报告文本文件单词的检索与计数 停车场管理.docx
《程序课程设计报告文本文件单词的检索与计数 停车场管理.docx》由会员分享,可在线阅读,更多相关《程序课程设计报告文本文件单词的检索与计数 停车场管理.docx(23页珍藏版)》请在冰豆网上搜索。
![程序课程设计报告文本文件单词的检索与计数 停车场管理.docx](https://file1.bdocx.com/fileroot1/2023-1/3/0ee878e0-5266-49ae-a617-0ff18a775837/0ee878e0-5266-49ae-a617-0ff18a7758371.gif)
程序课程设计报告文本文件单词的检索与计数停车场管理
程序设计课程设计
报告
1.1【问题描述】--------------------------------------------------1
1.2【设计需求及分析】--------------------------------------------1
1.3【设计功能的实现】--------------------------------------------1
1.4【实例测试及运行结果】----------------------------------------7
题目2:
停车场管理-----------------------------------------------------------------------------------9
2.1【问题描述】--------------------------------------------------9
2.2【设计需求及分析】--------------------------------------------9
2.3【设计功能的实现】--------------------------------------------9
2.4【实例测试及运行结果】---------------------------------------15
2.5【实现提示】-------------------------------------------------16
设计题目一
1.文本文件单词的检索与计数
1.1【问题描述】
设计C或C++程序,统计在这样的英文文本文件中,出现了多少个单词,每个单词出现了几次。
连续的英文字符都认为单词(不包括数字),单词之间用空格或标点符号分隔。
1.2【设计需求及分析】
要统计英文文本文件中出现了哪些单词,就要从文件中读取字符,读取出来的连续英文字符认为是一个单词,遇空格或标点符号单词结束。
使用线性表记录单词以及每个单词出现的次数。
线性表中的单词按字典顺序存储。
线性表的顺序存储结构如下:
#defineLIST_INIT_SIZE100//线性表存储空间的初始分配量
#defineLISTINCREMENT10//线性表存储空间的分配增量
typedefstruct{
charword[21]//存储单词,不超过20个字符
intcount;//单词出现的次数
}ElemType;
typedefstruct{
ElemType*elem;//存储空间基址
intlength;//当前长度
intlistsize;//当前分配的存储容量
}Sqlist;
1.3【设计功能的实现】(用C或C++语言描述)
#include
#include
#include
#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
typedefstruct{
charword[21];
intcount;
}ElemType;
typedefstruct{
ElemType*elem;
intlength;
intlistsize;
}SqList;
intInitList(SqList*p){
p->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(p->elem==NULL)return0;
p->length=0;
p->listsize=LIST_INIT_SIZE;
return1;
}
intLocateElem(SqList*p,char*word)
{
intlow,high,mid;
low=0;high=p->length-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(word,p->elem[mid].word)==0)//表中进行二分查找
{
p->elem[mid].count++;
return0;
}
elseif(strcmp(word,p->elem[mid].word)<0)
high=mid-1;
else
low=mid+1;
}
returnlow+1;//返回插入点序号
}
intInsertList(SqList*p,inti,char*word)
{
intj;
ElemType*base;
if(p->length>=p->listsize)
{
base=(ElemType*)realloc(p->elem,(p->listsize+LISTINCREMENT)*sizeof(ElemType));
if(base==NULL)return0;
p->listsize=p->listsize+LISTINCREMENT;//扩充表长
p->elem=base;
}
for(j=p->length;j>=i;j--)
p->elem[j]=p->elem[j-1];
strcpy(p->elem[i-1].word,word);
p->elem[i-1].count=1;
p->length++;
return1;
}
voidPrintList(SqList*p,intnum)
{
FILE*fw;
inti;
intno=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%-5d\n",p->elem[i].word,p->elem[i].count);
fprintf(fw,"******************************************\n");
fclose(fw);
}
//主函数
voidmain()
{
SqListL;
charword[21],ch,filename[30],filename1[50];
intnum=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();
exit(0);
}
ch=fgetc(fp);
while(ch!
=EOF)
{
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
ch=ch>='A'&&ch<='Z'?
ch+32:
ch;
word[j++]=ch;
mark=1;
}
else
{
if(mark==1)
{
if(j>20)
{printf("文章中部分单词太长不予统计");}
num++;
word[j]='\0';
mark=0;
j=0;
i=LocateElem(&L,word);
if(i>0)
InsertList(&L,i,word);
}
}
ch=fgetc(fp);
}
fclose(fp);
printf("统计结束!
请查看打开的单词计数.txt里面便是检索结果。
");
PrintList(&L,num);
system("start\"\"D:
\\单词计数.txt");
getchar();
}#defineLIST_INIT_SIZE100
#defineLISTINCREMENT10
typedefstruct{
charword[21];
intcount;
}ElemType;
typedefstruct{
ElemType*elem;
intlength;
intlistsize;
}SqList;
intInitList(SqList*p){
p->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(p->elem==NULL)return0;
p->length=0;
p->listsize=LIST_INIT_SIZE;
return1;
}
intLocateElem(SqList*p,char*word)
{
intlow,high,mid;
low=0;high=p->length-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(word,p->elem[mid].word)==0)//表中进行二分查找
{
p->elem[mid].count++;
return0;
}
elseif(strcmp(word,p->elem[mid].word)<0)
high=mid-1;
else
low=mid+1;
}
returnlow+1;//返回插入点序号
}
intInsertList(SqList*p,inti,char*word)
{
intj;
ElemType*base;
if(p->length>=p->listsize)
{
base=(ElemType*)realloc(p->elem,(p->listsize+LISTINCREMENT)*sizeof(ElemType));
if(base==NULL)return0;
p->listsize=p->listsize+LISTINCREMENT;//扩充表长
p->elem=base;
}
for(j=p->length;j>=i;j--)
p->elem[j]=p->elem[j-1];
strcpy(p->elem[i-1].word,word);
p->elem[i-1].count=1;
p->length++;
return1;
}
voidPrintList(SqList*p,intnum)
{
FILE*fw;
inti;
intno=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%-5d\n",p->elem[i].word,p->elem[i].count);
fprintf(fw,"******************************************\n");
fclose(fw);
}
//主函数
voidmain()
{
SqListL;
charword[21],ch,filename[30],filename1[50];
intnum=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();
exit(0);
}
ch=fgetc(fp);
while(ch!
=EOF)
{
if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z'))
{
ch=ch>='A'&&ch<='Z'?
ch+32:
ch;
word[j++]=ch;
mark=1;
}
else
{
if(mark==1)
{
if(j>20)
{printf("文章中部分单词太长不予统计");}
num++;
word[j]='\0';
mark=0;
j=0;
i=LocateElem(&L,word);
if(i>0)
InsertList(&L,i,word);
}
}
ch=fgetc(fp);
}
fclose(fp);
printf("统计结束!
请查看打开的单词计数.txt里面便是检索结果。
");
PrintList(&L,num);
system("start\"\"D:
\\单词计数.txt");
getchar();
}1.3.1实现顺序表的基本操作
⑴顺序表的初始化:
InitList(SqList&L)
⑵顺序表上查找指定的单词:
LocateElem(SqList&L,char*s)
若找到,单词的出现次数增1,返回0,否则返回该单词的插入位置。
⑶在顺序表上插入新的单词:
InsertList(SqList&L,inti,char*s)
要求按字典顺序有序。
新单词的出现次数为1.
⑷输出顺序表上存储的单词统计信息:
PrintList(SqList&L)
输出文件中每个单词出现的次数以及文件中总的单词数(可输出到文件中)。
1.3.2统计单词数
统计过程如下:
(1)输入要统计单词的文本文件名,打开相应的文件;
(2)初始化顺序表;
(3)从文本文件中读取字符,直到文件结束。
具体描述如下:
While(读文件没有结束)
{
过滤单词前的非字母字符;
读取一个单词,已字符串形式存储在一个字符数组中;
在线性表中查找该单词,若找到,单词的出现次数加1,否则返回其插入位置;
上一步中,若没找到,则进行插入操作;
处理下一个单词。
}
(4)关闭文件,输出统计结果。
1.4【实例测试及运行结果】
1.4.1运行实例一
文章:
love(我自己写的,只有一句话)
运行结果:
1.4.1运行实例二
文章:
tyut
运行结果:
设计题目二
2.停车场管理
2.1【问题描述】
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。
汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若停车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。
试为停车场编制按上述要求进行管理的模拟程序。
2.2【设计需求及分析】
以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。
每一组输入数据包括三个数据项:
汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。
对每一组输入数据进行操作后的输出信息为:
若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。
栈以顺序结构实现,队列以链表结构实现。
2.3【设计功能的实现】(用C或C++语言描述)
#include
#include
#include
#include
#defineMAX10
#defineprice0.05
typedefstructtime
{
inthour;
intmin;
}time;
typedefstructcarnode
{
charnum[10];
timereach;
timeleave;
}carnode;
typedefstructcarstack
{
carnode*stack[MAX+1];
inttop;
}carstack;
typedefstructqnode
{
carnode*data;
structqnode*next;
}qnode;
typedefstructnode
{
qnode*head;
qnode*rear;
}linkqueue;
voidinitstack(carstack*s)
{
inti;
s->top=0;
for(i=0;i<=MAX;i++)
s->stack[s->top]=NULL;
}
intinitqueue(linkqueue*Q)
{
Q->head=(qnode*)malloc(sizeof(qnode));
if(Q->head!
=NULL)
{
Q->head->next=NULL;
Q->rear=Q->head;
return1;
}
else
return-1;
}
//车辆到达
intarrival(carstack*enter,linkqueue*w)
{
carnode*p;
qnode*t;
p=(carnode*)malloc(sizeof(carnode));
printf("\n请您输入车牌号:
");
scanf("%s",&p->num);
if(enter->top{
enter->top++;
printf("\n请您把车停在%d号位置\n",enter->top);
printf("\n请输入您进入停车场的时间:
");
scanf("%d:
%d",&(p->reach.hour),&(p->reach.min));
enter->stack[enter->top]=p;
return1;
}
else//没有空车位
{
printf("对不起,停车场已经没有空位了,您可以把车停在便道上!
");
t=(qnode*)malloc(sizeof(qnode));
t->data=p;
t->next=NULL;
w->rear->next=t;
w->rear=t;
return1;
}
}
voidprint(carnode*p,introom)//汽车离站时缴费显示
{
printf("\n车辆离开的时间:
");
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);
}
//车辆离开
voidleave(carstack*enter,carstack*temp,linkqueue*w)
{
introom;
carnode*p,*t;
qnode*q;
if(enter->top>0)//有车
{
while
(1)
{
printf("\n请输入您的车在停车场上的位置:
");
scanf("%d",&(room));
if(room>=1&&room<=enter->top)
break;
}
}
while(enter->top>room)//位置不在栈顶的汽车出栈
{
temp->top++;
temp->stack[temp->top]=enter->stack[enter->top];
enter->stack[enter->top]=NULL;
enter->top--;
}
p=enter->stack[enter->top];
enter->stack[enter->top]=NULL;
enter->top--;
while(temp->top>=1)//当暂时存储汽车的栈结构中有汽车时
{
enter->top++;
enter->stack[enter->top]=temp->stack[temp->top];
temp->stack[temp->top]=NULL;
temp->top--;
}
print(p,room);
//判断便道上是否有车及停车场上是否已满
if((w->head!
=w->rear)&&enter->top{
q=w->head->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->stack[enter->top]=t;
free(q);
}
else
printf("\n便道里没有车!
");
}
else
printf("\n现在停车场里没有车了!
");
}
//显示停车场的信息
voidlist1(carstack*s)
{
inti;
if(s->top>0)
{
printf("\n停车场:
");
printf("\n停车位置\t到达时间\t车牌号\n");
for(i=1;i<=s->top;i++)
{
printf("%d\t",i);
printf("%02d:
%02d",s->stack[i]->reach.hour,s->stack[i]->reach.min);
printf("\t%s",s->stack[i]->num);
}