资源描述
目录
一课题分析3
二逻辑分析3
2。1 数据结构的描述和每个基本操作的功能说明3
2。1 给出本程序包含的模块及模块之间的调用关系图4
2。3 写出重要部分的伪码算法4
三详细设计5
3。1 数据结构的定义,及其基本操作的实现5
3。2主函数和其他函数的实现或伪码算法6
3.3 程序的层次结构的函数调用关系图8
3.4 详细设计8
四程序源代码9
五程序调试与测试17
5.1 主界面17
5。2 具体操作18
5.2。1进站时间与车牌号18
5.2。2 车站已满,请进入临时车道19
5。2.3 出站与收费20
5。2。4 结束20
5。3 相关操作20
六程序中遇到的问题与解决方法21
6.1 写提纲21
6。2 在程序调试过程,遇到的相关问题21
七总结25
八参考文献26
一 课题分析
a) 该程序主要利用栈和队列来实现车的到达及其离开功能,其中主要有对各种情况的处理,要求如下:
1、要求以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的数据序列进行模拟管理
2、要求处理的数据元素包括三个数据项:汽车“到达"或“离去”信息,汽车牌照号码及到达或离去的时间
3、该系统完成以下功能:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是离去,则输出汽车在停车场内停留的时间和应缴纳的费用(在便道上停留的时间不收费)
4、要求栈以顺序结构实现;
b) 程序的输入,程序功能选择,字符型,A为到达,D离开,E退出系统;车的编号是整型,输入数字即可;时间是float类型,可精确到分钟
c) 程序的输出,当车到达,程序输出车的编号及到达时间,若栈满则提示停到便道上;车离开时,输出车的编号及其所需费用。
d) 测试数据,(A,1,5),(A,2,10),(D,1,15),(A,3,20),(A,4,25),(A,5,30),(D,2,35),(D,4,40),E以及a)中的要求。其中‘A'表示到达,D表示离开,E表示结束。
二逻辑分析
2。1 数据结构的描述和每个基本操作的功能说明
ADT stack{
数据对象:D={ai | ai∈CharSet,i=1,2,…,n,n≥0}
数据关系:R1={〈ai—1,ai〉| ai—1, ai∈D,i=2,…,n}
基本操作:
initstack()
操作结果:构造一个空栈,并返回地址。
gettop(&S)
初使条件:栈S已存在。
操作结果:栈S不为空,输出顶元素.
stop(&S,e)
初始条件:栈S已存在。
操作结果:要栈S中栈顶插入新的栈顶元素e.
ADT queue{
数据对象:D={ai | ai∈ElemSet,i=1,2,…,n,n≥0}
数据关系:R1={<ai-1,ai>| ai-1, ai∈D,i=2,…,n}
约定其中ai端为队列的头,an端为队列的尾
}
initqueue(&Q)
操作结果:构造一个空栈,并返回地址.
gethead(&S)
初使条件:栈S已存在。
操作结果:栈S不为空,输出顶元素。
enqueue(&S,e)
初始条件:栈S已存在.
操作结果:要栈S中栈顶插入新的栈顶元素e
2。1 给出本程序包含的模块及模块之间的调用关系图
本程序包含三个模块:
1)主程序模块:
Void main()
{
初始化;
do{
接受命令;
处理命令;
}while(命令!=“退出”)
}
2)栈模块——实现栈抽象数据类型
3)队列模块——实现队列抽象数据类型
2。3写出重要部分的伪码算法
车辆到达或者离开的伪码算法:
do{
输出菜单选项;
如果选择A,即车辆到达,
则{若栈不满,车辆进栈,停到停车场;
否则,车入队,车停在便道上;
}
如果选择D,即车辆离开,
则{ 如果队不空并且栈不满,被选的车辆离开,队列上的车出队入栈;
被选的车辆离开;
计算时间及其所需费用;
}
如果选择E,退出程序;
}while(输入的菜单选项不正确)
三 详细设计
3。1数据结构的定义,及其基本操作的实现
typedef struct time
{
int hour;
int min;
}Time;
typedef struct//车信息
{
char label[10];
float time;
}Car,Car2;
typedef struct//车库信息
{
Car *top;
Car *base;
int stacksize;
}SqStack;
typedef struct//临时车道
{
Car2 *top2;
Car2 *base2;
int stacksize2;
}SqStack2;
typedef struct QNode//车道信息
{
Car data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
3。2主函数和其他函数的实现或伪码算法
void main()
{
SqStack S;
SqStack2 S2;
InitStack(&S);
InitStack2(&S2);
InitQueue(&Q);//初始化
while(ch==1)
{
do
{
printf(”\n\t\t\t\tA-车辆到达 \n\t\t\t\tD-车辆离开 \n\t\t\t\tE—退出\n\t\t\t\tA/D/E ?\b”);
scanf(”%c”,&status);
getchar();
}while(status!='A'&&status!='a'&&status!='D’&&status!='d’&&status!='E’&&status!=’e');
if(status==’A'||status==’a’)//当车到达
{
输入车号;
输入车到达时间;
if(!StackFull(S))//栈不满
{
车入栈;
printf("\n\t\t\t\t继续请输入1\n\t\t\t\t放弃请输入 0 ?\b");
scanf(”%d”,&ch);
getchar();
}//if
else
{
车入队;
printf(”\n\t\t\t\t继续请输入1\n\t\t\t\t放弃请输入 0 ?\b");
scanf("%d”,&ch);
getchar();
}//else
}//if当车到达
else if(status=='D’||status==’d')/*当车离开*/
{
do
{
输入车号;
输入车离开时间;
do
{
Pop(&S,&car_M);
if(car_D。label!=car_M。label)
Push2(&S2,car_M);
else
car_I。time=car_M。time;
}while(car_D。label!=car_M。label);
position_s-—;
while(!StackEmpty2(S2))//栈2不空
{
Pop2(&S2,&car_M);
Push(&S,car_M);
}//while
while(!QueueEmpty(Q)&&!StackFull(S))
{
if(!StackFull(S))
{
栈不满的话,临时车道上的车进栈
}//if
}//while
time=car_D。time—car_I。time;
if(time<0。00)
{
printf(”\n\t\t\t\tSorry!You input a wrong time !\n\t\t\t\tPlease check and type again.\n");
}//if
else
{
计算停车时间;
计算所需费用;
}//else
printf("\n\t\t\t\tWelcome to back ! Do you want to continue\n\t\t\t\t1—contine/0—quit ?\b”);
scanf(”%d”,&ch);
getchar();
}while(!mistime);
}//else if
else exit(0);//退出程序
}//第一个while
}//main
3。3程序的层次结构的函数调用关系图
StackFull()
Main()
initqueue()
Initstack2()
DeQueue()
initstack()
QueueEmpty()
EnQueue()
3。4 详细设计
根据老师的要求,在这里我主要分析程序设计中停车场管理系统的进站模块:该模块涉及多个函数,过程依次有初始化,选择进站输入A,车牌号,时间。若站满,则车进入临时车道,分别记录时间和车牌号以及临时车道上的位置,站不满时临时车道上的车进站。该程序流程图如下:
车进站流程图
进站
Main()
选择A
开始
继续
进站
进入临时车道
临时车道车进入车站
结束
四程序源代码
#include "stdio。h"
#include ”stdlib。h”
#include ”string。h”
#define NULL 0
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 2//车库容量
typedef struct time{
int hour;
int min;
}Time; //时间结点
typedef struct//车信息
{
char label[10];
float time;
}Car,Car2;
typedef struct//车库信息
{
Car *top;
Car *base;
int stacksize;
}SqStack;
int InitStack(SqStack *S)//栈的初始化
{
S—〉base=(Car *)malloc(STACK_INIT_SIZE*sizeof(Car));
if(!(S-〉base)) return ERROR;
S-〉top=S->base;
S-〉stacksize=STACK_INIT_SIZE;
return OK;
}
int StackEmpty(SqStack S)//栈空
{
if(S。top==S。base)
return OK;
else
return ERROR;
}
int StackFull(SqStack S)//栈满
{
if(S。top—S。base>=STACK_INIT_SIZE)
return OK;
else
return ERROR;
}
int Push(SqStack *S,Car e)//入栈
{
if(S->top—S—〉base〉=STACK_INIT_SIZE)
return OVERFLOW;
else {
*(S-〉top++)=e;
return OK;
}//else
}
int Pop(SqStack *S,Car *e)//出栈
{
if(S—〉top==S-〉base)
return ERROR;
else *e=*(——(S—〉top));
return OK;
}
int GetTop(SqStack *S,Car *e)//返回栈顶元素
{if(S-〉top==S->base) return ERROR;
else *e=*(S-〉top—1);
return OK;
}
typedef struct//临时车道
{
Car2 *top2;
Car2 *base2;
int stacksize2;
}SqStack2;
int InitStack2(SqStack2 *S2)
{
S2-〉base2=(Car2 *)malloc(STACK_INIT_SIZE*sizeof(Car2));
if(!(S2—〉top2)) return ERROR;
S2—>top2=S2—〉base2;
S2—〉stacksize2=STACK_INIT_SIZE;
return OK;
}
int Push2(SqStack2 *S2,Car2 e2)
{
if(S2->top2—S2—〉base2〉=STACK_INIT_SIZE)
return OVERFLOW;
*(S2—〉top2++)=e2;
return OK;
}
int Pop2(SqStack2 *S2,Car2 *e2)
{
if(S2—>top2==S2—〉base2)
exit(OVERFLOW);
*e2=*(-—(S2->top2));
return OK;
}
int StackEmpty2(SqStack2 S2)
{
if(S2。top2==S2。base2)
return OK;
else
return ERROR;
}
typedef struct QNode//车道信息
{
Car data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
int InitQueue(LinkQueue *Q)//初始化队列
{
Q—>front=Q—>rear=(QueuePtr)malloc(sizeof(QNode));
if(!(Q-〉front)) return ERROR;
Q—>front-〉next=NULL;
return OK;
}
int EnQueue(LinkQueue *Q,Car e)//插入元素
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return ERROR;
p—〉data=e;
p—〉next=NULL;
Q—〉rear—>next=p;
Q—〉rear=p;
return OK;
}
int QueueEmpty(LinkQueue Q)//队空
{
if(Q.front==Q。rear)
return OK;
else
return ERROR;
}
int DeQueue(LinkQueue *Q,Car *e)
{
QueuePtr p;
if(Q-〉front==Q-〉rear) return ERROR;
p=Q—〉front->next;
*e=p—〉data;
Q—>front->next=p—〉next;
if(Q—>rear==p)
Q-〉rear=Q—〉front;
free(p);
return OK;
}
void main()
{
int i,position_s=1,position_q=1,mistime=1;
int ch=1;
char status;
float time,money;
LinkQueue Q;
Car car_I,car_D,car_M,car_S;
SqStack S;
SqStack2 S2;
InitStack(&S);
InitStack2(&S2);
InitQueue(&Q);
for(i=0;i〈80;i++)
printf(”*”);
printf(”\n\t\t\t\t停车场管理系统”);
printf(”\n\n\n\t\t\t09计算机科学与技术学院 刘婷 200917020117\n”);
printf(”\n\t\t\t\t停车场\t\t”);
printf(”\n”);
printf("\t\t\t\t相关信息:\n”);
printf(”\t\t\t\t请输入字符(A D E)进行相关操作");
printf(”\n");
while(ch==1)
{
do
{
printf(”\n\t\t\t\tA.车辆到达 \n\t\t\t\tD。车辆离开 \n\t\t\t\tE。退出 \n\t\t\t\tA/D/E ?\b”);
scanf(”%c”,&status);
getchar();
}while(status!=’A’&&status!=’a'&&status!='D'&&status!=’d’&&status!=’E’&&status!=’e’);
if(status==’A'||status=='a')
{
printf(”\n\t\t\t\t请输入车牌号 :”);
gets(car_I.label);//scanf(”%d”,&(car_I。label));
printf(”\n\t\t\t\t请输入车辆到达时间: ?\b”);
scanf("%f",&(car_I.time));
if(!StackFull(S))//栈不满
{
Push(&S,car_I);
printf("\n\n”);
for(i=0;i<80;i++) printf(”-”);
printf("\n”);
printf("\t\t\t\t输出车牌号:");
printf(” %s\n",car_I。label);
printf(”\n\t\t\t\t请输出车辆到达时间:”);
printf(” %5。2f”,car_I。time);
printf("\n\n”);
for(i=0;i〈80;i++) printf(”-”);
printf(”\n\n”);
printf(”\n\t\t\t\t欢迎您来我们车站!\n\n\t\t\t\t您的车位号是: %d”,position_s);
position_s++;
printf(”\n”);
printf(”\t\t\t\t继续请输入1\n\t\t\t\t放弃请输入0 ?\b");
scanf("%d”,&ch);
getchar();
}//if
else
{
EnQueue(&Q,car_I);
printf(”\t\t\t\tWelcome to our CAR POSITION ,We are sorry that\n\n\t\t\t\tOur position is full,but you are free to place \
\n\n\t\t\t\tyour car on our road.The position of your car\n\t\t\t\tis %d”,position_q);
position_q++;
printf("\n\t\t\t\t进站请输入1\n\t\t\t\t放弃请输入0 ?\b”);
scanf("%d”,&ch);
getchar();
}//else
}//if当车到达
else if(status=='D’||status==’d’)/*当车离开*/
{
do
{
for(i=0;i〈80;i++)
printf("*");
printf(”\n");
printf(”\t\t\t\tCAR POSITION”);
printf("\n\t\t\t\t”);
printf(”Information");
printf(”\n\n");
printf(”\t\t\t\tYour are going to drive your car away ,\n\t\t\t\tPlease
fill of the form !\n”);
printf(”\t\t\t\tYour car Number :”);
scanf(”%d”,&car_D。label);
printf(”\n\t\t\t\tTime : ?\b”);
scanf(”%f",&(car_D。time));
do
{
Pop(&S,&car_M);
if(car_D。label!=car_M。label)
Push2(&S2,car_M);
else
car_I。time=car_M。time;
}while(car_D。label!=car_M.label);
position_s-—;
while(!StackEmpty2(S2))//栈2不空
{
Pop2(&S2,&car_M);
Push(&S,car_M);
}//while
while(!QueueEmpty(Q)&&!StackFull(S))
{
if(!StackFull(S))
{//栈不满的话,临时车道上的车进栈
DeQueue(&Q,&car_S);
position_q—-;
car_S.time=car_D。time;
Push(&S,car_S);
printf(”\n\t\t\t\tThe car %d just drived away ,\n\n\t\t\t\tthe car %d has entered the CAR POSITION 。\n”,car_D。label,car_S。label);
}//if
}//while
time=car_D.time—car_I。time;
if(time<0。00)
{
printf(”\n\t\t\t\tSorry!You input a wrong time !Please check and type again。\n");
;}//if
else
{
mistime=1;
money=time*2;
printf(”\n\n");
for(i=0;i〈80;i++)
printf("—”);
printf(”\n\t");
printf("\t\t\tYour car number %d :\n\t\t\t\tThe fee is :%5。2f",car_D。label,money);
printf(”\n\n");
for(i=0;i〈80;i++) printf("—”);
printf(”\n\n”);
}//else
printf("\t\t\t\tWelcome to back ! Do you want to continue\n\t\t\t\t 1—contine/0—quit ?\b");
scanf(”%d”,&ch);
getchar();
}while(!mistime);
}//else if
else exit(0);//退出程序
}//第一个while
}//main
五 程序调试与测试
5。1主界面
5。2具体操作
5.2。1进站时间与车牌号
5。2.2车站已满,请进入临时车道
5。2。3出站与收费
5.2.4结束
5.3 相关操作
1、输入(A,1,5)时,显示是CAR NUMBER :1ARRIVE TIME:5。00
2、入(A,2,10)时,显示CAR NUMBER :2ARRIVE TIME:10.00
3、入(D,1,15)时,显示Your car number:1 The fee is:20。00
4、入(A,3,20)时,显示CAR NUMBER :3ARRIVE TIME:20.00
5、输入(A,4,25)时,显示Welcome to our CAR POSITION ,We are sorry thatOur position is full,but you are free to place your caron our roadThe position of your car is1
6、入(A,5,30)时,显示Welcome to our CAR POSITION ,We are sorry thatOur position is full,but you are free to place your caron our roadThe position of your car is2
7、入(D,2,35)时,显示Your car number:2 The fee is:50。00
8、入(D,4,40)时,显示The car 4 just drived away , the car 5 has entered the CAR POSITION .Your car number:4 The fee is:10.00
9、入E时,程序结束,显示Press any key to continue。
六程序中遇到的问题与解决方法
6.1 写提纲
在编写停车场管理系统的时候,首先列出一个基本提纲,里面包括一些基本的函数与函数间的调用,为整个程序提供方向。
6.2 在程序调试过程,遇到的相关问题
1)循环中当输入的(ch!=1&&ch!=0),程序进入死循环,其源代码如下:
while(ch==1)
do{
printf("\n\t\t\t\tA。车辆到达 \n\t\t\t\tD。车辆离开 \n\t\t\t\tE.退出 \n\t\t\t\tA/D/E ?\b”);
scanf(”%c”,&status);
getchar();
}while(status!='A'&&status!=’a’&&status!=’D'&&status!='d’&&status!='E’&&status!='e’);
if(status==’A’||status=='a’)
printf(”\n\t\t\t\t请输入车牌号 :");
gets(car_I。label);//scanf("%d”,&(car_I.label));
printf("\n\t\t\t\t请输入车辆到达时间: ?\b”)
scanf("%f”,&(car_I.time));
if(!StackFull(S))//栈不满
{
Push(&S,car_I);
printf(”\n\n”);
for(i=0;i〈80;i++) printf(”—”);
printf(”\n”);
printf(”\t\t\t\t输出车牌号:”);
printf(” %s\n”,car_I。label);
printf("\n\t\t\t\t请输出车辆到达时间:”);
printf(” %5。2f”,car_I.time);
printf(”\n\n”);
for(i=0;i<80;i++) printf(”—”);
printf(”\n\n”);
printf(”\n\t\t\t\t欢迎您来我们车站!\n\n\t\t\t\t您的车位号是: %d",position_s);
position_s++;
printf(”\n”);
printf("\t\t\t\t进站请输入1\n\t\t\t\t放弃请输入0 ?\b”);
scanf(”%d”,&ch);
getchar();
}//if
else
{
EnQueue(&Q,car_I);
printf(”\t\t\t\tWelcome to our CAR POSITION ,We are sorry that\n\n\t\t\t\tOur position is full,but you are free to place your car\
\n\n\t\t\t\ton our road.The position of your car is %d”,position_q);
position_q++;
printf(”\n\t\t\t\t进站请输入1\n\t\t\t\t放弃请输入0 ?\b");
scanf(”%d”,&ch);
getchar();
}//else
结束死循环,修改后源代码如下:
while(ch==1)
do{
printf(”\n\t\t\t\tA.车辆到达 \n\t\t\t\tD。车辆离开 \n\t\t\t\tE.退出 \n\t\t\t\tA/D/E ?\b”);
scanf(”%c”,&status);
getchar();
}while(status!='A'&&status!='a'&&status!='D'&&status!=’d'&&status!=’E'&&status!=’e');
if(status==’A’||status==’a’)
printf(”\n\t\t\t\t请输入车牌号 :”);
gets(car_I。label);//scanf(”%d",&(car_I.label));
printf("\n\t\t\t\t请输入车辆到达时间: ?\b”)
scanf("%f”,&(car_I。time));
if(!StackFull(S))//栈不满
{
Push(&S,car_I);
printf(”\n\n");
for(i=0;i〈80;i++) printf(”—”);
printf(”\n”);
printf("\t\t\t\t输出车牌号:”);
printf(" %s\n”,car_I。label);
printf(”\n\t\t\t\t请输出车辆到达时间:”);
printf(” %5。2f”,car_I.time);
printf(”\n\n”);
for(i=0;i<80;i++) printf(”—");
printf(”\n\n”);
printf("\n\t\t\t\t欢迎您来我们车站!\n\n\t\t\t\t您的车位号是: %d",position_s);
position_s++;
printf("\n");
printf("\t\t\t\t进站请输入1\n\t\t\t\t放弃请输入0 ?\b”);
scanf(”%d",&ch);
getchar();
}//if
else
{
EnQueue(&Q,car_I);
printf(”\t\t\t\tWelcome to our CAR POSITION ,We are sorry that\n\n\t\t\t\tOur position is full,but you are free to place your car\
\n\n\t\t\t\ton our road.The position of your car is %d",position_q);
position_q++;
printf(”\n\t\t\t\t进站请输入1\n\t\t\t\t放弃请输入0 ?\b");
scanf(”%d",&ch);
if(ch)
getchar();
}//else
2) 最初调试中车牌号编写为int型,输入的只能是数字,但现实生活中车牌号都为汉字+英文字母+数字,在改进过程中,改成字符型,后来发现,那是治标不治本的方法,经过几次折腾与试探,都没有成功!我相信只要坚持,即使这个程序调试不好,也没关系,因为只要尽全力去做了,就不会让自己后悔。
修改前程序源代码:
#include ”stdio。h”
#include ”stdlib。h"
#define NULL 0
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define STACK_INIT_SIZE 2//车库容量
typedef struct time{
int hour;
int min;
}Time; //时间结点
typedef struct//车信息
{
int label;
float time;
}Car,Car2;
if(status=='A'||status==’a')
{
printf(”\n\t\t\t\t请输入车牌号 :”);
scanf(”%d”,&(car_I。label));
printf("\n\t\t\t\t请输入车辆到达时间: ?\b”);
scanf(”%f”,&(car_I。time));
修改后程序源代码:
#include ”stdio。h”
#include "stdlib.h”
#include ”string。h”
#define NULL 0
#define ERROR 0
#define OK 1
#define OVERFLOW —1
#define STACK_INIT_SIZE 2//车库容量
typedef struct time{
int hour;
int min;
}Time; //时间结点
typedef struct//车信息
{
char label[10];
float time;
}Car,Car2;
if(status==’A'||status=='a')
{
printf("\n\t\t\t\t请输入车牌号 :”);
gets(car_I.label);//scanf(”%d”,&(car_I。label));
printf("\n\t\t\t\t请输入车辆到达时间: ?\b”);
scanf(”%f”,&(car_I。time));
七总结
虽然说这次数据结构课程设计是以小组形式进行,我和我的搭档虽然做的是同一课题,但是经过我们深度思考和商量,最终我们决定做同一课题,但为了锻炼我们的动手和操作能力,我们决定各做一个停车场管理系统,在课堂上和课余时间我们互相帮助,互相学习.
这次课程设计通过参考c语言程序设计,c++课程设计与程序设计,数据结构及网上资料完成.看到自己把所出现的错误一个个地改出来,心里真的是很欣慰。我体会到了自己完成一次作业的成就感,更重要的是我体会到了认真对待一件事并好好完成的愉悦,这将对我以后产生很大帮助,理论离不开实践,只有多动手,多动脑,勤动手才能取得成功,活的充实。
通过这一次课程设计,我学到的东西比以前上课学到的还要多,这让我明白,理论永远是理论,要是没有实践,理论永远是一纸空文.我们要从实践中才能学到更多的东西.而实践又要以理论为基础,要是数据结构知识不扎实,做出的程序也是漏洞百出.所以,我们要将理论和实践结合起来,把我们
展开阅读全文