C语言课程设计停车场报告_c语言课程设计停车场
C语言课程设计停车场报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“c语言课程设计停车场”。
C语言上机实习报告
指导老师:吴杰 学生姓名:刘超 班级序号:02305231 学生证号:20051004279
一.题目要求
设有一个可以停放N辆汽车的狭长停车场,它只有1个大门可以供车辆进出。车辆按到达停车时间的早晚依次从停车场罪戾面向大门口停放。如果停车场已放满N辆车,则后来的车辆只能在停车场大门外的便道上等待,一旦停车场内有车开走,则排放在便道上的第一辆车就进入停车场。停车场内如有某辆车要开走,在他之后进来的车都必须先退出停车场为它让路,等待其开出停车场后,这些车辆再依原来次序进场。每辆车在离开停车场时,都应根据它在停车场内停留的时间长短交费。如果停留在便道上的车未进停车场就要离开,允许其离去,不收停车费,并且仍然保持在便道上等待的车辆次序。
二.需求分析
停车场采用栈式结构,停车场外的便道采用队列结构(即便道就是等候队列)。
停车场的管理流程如下:①当车辆要进入停车场时,检查停车场是否已满,如果未满则车辆进栈(车辆进入停车场);如果停车场已满,则车辆进入等候队列(车辆进入便道等候)。②当车辆要求出栈时,该车到栈顶的那些车辆先弹出栈(在它之后进入的车辆必须先退出车场为它让路),再让该车出栈,其他车辆再按原次序进栈(进入车场)。当车辆出栈完毕后,检查等候队列(便道)中是否有车,有车则从队列头取出一辆车压入栈中。
三.总体设计
采用面向对象设计方法:先抽象出类,再分析每个类有哪些成员变量,每个类应该实现哪些功能(即应该有哪些成员函数)。最后,根据要求实现类的成员函数,完成该模拟的逻辑设计和实现。
四.详细设计
#include #include #include
#define MAX 2 /*车库容量*/ #define price 0.05 /*每车每分钟费用*/ typedef struct time{ int hour;int min;}Time;/*时间结点*/ typedef struct node{ char num[10];Time reach;Time leave;}CarNode;/*车辆信息结点*/ typedef struct NODE{ CarNode *stack[MAX+1];int top;}SeqStackCar;/*模拟车站*/ typedef struct car{ CarNode *data;struct car *next;}QueueNode;typedef struct Node{ QueueNode *head;QueueNode *rear;}LinkQueueCar;/*模拟通道*/
void InitStack(SeqStackCar *);/*初始化栈*/ int InitQueue(LinkQueueCar *);/*初始化便道*/ int Arrival(SeqStackCar *,LinkQueueCar *);/*车辆到达*/ void Leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *);/*车辆离开*/ void List(SeqStackCar,LinkQueueCar);/*显示存车信息*/
void main(){ SeqStackCar Enter,Temp;LinkQueueCar Wait;int ch;InitStack(&Enter);/*初始化车站*/ InitStack(&Temp);/*初始化让路的临时栈*/ InitQueue(&Wait);/*初始化通道*/ while(1){ printf(“n1.the car arrive”);printf(“ 2.the car leave”);printf(“ 3.the schedule ”);printf(“ 4.outn”);while(1){ scanf(“%d”,&ch);if(ch>=1&&ch
void InitStack(SeqStackCar *s)/*初始化栈*/ { int i;s->top=0;for(i=0;istack[s->top]=NULL;} int InitQueue(LinkQueueCar *Q)/*初始化便道*/ { Q->head=(QueueNode *)malloc(sizeof(QueueNode));if(Q->head!=NULL){ Q->head->next=NULL;Q->rear=Q->head;return(1);} else return(-1);} void PRINT(CarNode *p)/*打印出站车的信息*/ { int A1,A2,B1,B2;printf(“nplease input thedepart time:/**:**/”);scanf(“%d:%d”,&(p->leave.hour),&(p->leave.min));printf(“nthe number of the car:”);puts(p->num);printf(“nthe time the car arrive: %d:%d”,p->reach.hour,p->reach.min);printf(“the depart time: %d:%d”,p->leave.hour,p->leave.min);A1=p->reach.hour;A2=p->reach.min;B1=p->leave.hour;B2=p->leave.min;printf(“nthe fee: %2.1f元”,((B1-A1)*60+(B2-A2))*price);free(p);} int Arrival(SeqStackCar *Enter,LinkQueueCar *W)/*车辆到达*/ { CarNode *p;QueueNode *t;p=(CarNode *)malloc(sizeof(CarNode));flushall();printf(“ninput the number of the car(例:陕A1234):”);gets(p->num);if(Enter->toptop++;printf(“nthe place of the car.”,Enter->top);printf(“nthe time thecar arrive:/**:**/”);scanf(“%d:%d”,&(p->reach.hour),&(p->reach.min));Enter->stack[Enter->top]=p;return(1);} else /*车场已满,车进便道*/ { printf(“n该车须在便道等待!”);t=(QueueNode *)malloc(sizeof(QueueNode));t->data=p;t->next=NULL;W->rear->next=t;W->rear=t;return(1);} } void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W){ /*车辆离开*/ int i, room;CarNode *p,*t;QueueNode *q;/*判断车场内是否有车*/ if(Enter->top>0)/*有车*/ { while(1)/*输入离开车辆的信息*/ { printf(“n请输入车在车场的位置/1--%d/:”,Enter->top);scanf(“%d”,&room);if(room>=1&&roomtop)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);/*判断通道上是否有车及车站是否已满*/ 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->stack[Enter->top]=t;free(q);} else printf(“n便道里没有车.n”);} else printf(“n车场里没有车.”);/*没车*/ } void List1(SeqStackCar *S)/*列表显示车场信息*/ { int i;if(S->top>0)/*判断车站内是否有车*/ { printf(“n车场:”);printf(“n 位置 到达时间 车牌号n”);for(i=1;itop;i++){ printf(“ %d ”,i);printf(“%d:%d ”,S->stack[i]->reach.hour,S->stack[i]->reach.min);puts(S->stack[i]->num);} } else printf(“n车场里没有车”);} void List2(LinkQueueCar *W)/*列表显示便道信息*/ { QueueNode *p;p=W->head->next;if(W->head!=W->rear)/*判断通道上是否有车*/ { printf(“n等待车辆的号码为:”);while(p!=NULL){ puts(p->data->num);p=p->next;} } else printf(“n便道里没有车.”);} void List(SeqStackCar S,LinkQueueCar W){ int flag,tag;flag=1;while(flag){ printf(“n请选择 1|2|3:”);printf(“n1.车场n2.便道n3.返回n”);while(1){ scanf(“%d”,&tag);if(tag>=1||tag
程序运行过程:
1。主系统程序
2.选择所需系统
存储完毕程序继续进行。
选择2为车辆离开启动程序:
选择4退出程序。
五.总结
通过该实例的设计分析,掌握了模块设计的方法,理解和运用了结构化程序设计的思想和方法。掌握和提高了利用C语言进行编程设计的能力。