数据结构课程设计-C语言停车场管理器|数据结构课程设计
/*******************************停车场管理器*************************************/#include
#include #include#include /********************************************************************************/#define MAXSTACKSIZE 2 /*车库容量*/#define price 0.1 /*每车每分钟费用*/typedef struct time{ int hour;int min;}Time; /*时间结点*/ typedef struct {char num[10];Time reach; Time leave; }CarNode; /*车辆信息结点*/typedef struct {CarNode *base;CarNode *top;int stacksize;}SqStackCar; /*模拟车站*/typedef struct car{ CarNode *data;struct car *next; }QueueNode;typedef struct {QueueNode *front;QueueNode *rear; }LinkQueueCar; /*模拟通道*/ int QueueEmpty(LinkQueueCar Q) /*便道判空函数*/{ if(Q.front==Q.rear) return 1; else return 0;}/********************************************************************************/void InitStack(SqStackCar *s) /*初始化栈*/{ s->base=(CarNode *)malloc(MAXSTACKSIZE*sizeof(CarNode)); if(!s->base) exit(0);/*分配失败*/ s->top=s->base; s->stacksize=MAXSTACKSIZE;}
int Push(SqStackCar *s,CarNode *e) /*进站函数*/{ if(s->top-s->base>=s->stacksize) return 0; else *s->top++=*e; return 1;}
int Pop(SqStackCar *s,CarNode *e) /*出站函数*/{ if(s->top==s->base) return 0; *e=*--s->top; return 1;}
int StackEmpty(SqStackCar s) /*判空函数*/{ if(s.base==s.top) return 1; else return 0;}int InitQueue(LinkQueueCar *Q) /*初始化便道*/ { Q->front=Q->rear=(QueueNode *)malloc(sizeof(QueueNode)); if(!Q->front) exit(0); Q->front->next=NULL; return 1;} /**************************************************************/int EnQueue(LinkQueueCar *Q,CarNode *e) /*便道插入函数*/{ QueueNode *p; p=(QueueNode *)malloc(sizeof(QueueNode)); if(!p) exit(0); p->data=e; p->next=NULL; Q->rear->next=p; Q->rear=p; return 1;}int DeQueue(LinkQueueCar *Q,CarNode *e) /*便道删除函数*/{ QueueNode *p; if(Q->front==Q->rear) return 0; p=Q->front->next; e=p->data; Q->front->next=p->next; if(Q->rear==p) Q->rear=Q->front; free(p); return 1;}
/********************************************************************************/int Arrive(SqStackCar *In,LinkQueueCar *Wait) /*车辆到达函数*/{ CarNode *i; QueueNode *w; i=(CarNode *)malloc(sizeof(CarNode)); flushall(); printf("Input the car number:"); gets(i->num); if(In->top-In->basereach.hour,&i->reach.min); Push(In,i); printf("\nCar in success!!"); sleep(1); return 1; } else /*停车场已满,车进便道*/ { w=(QueueNode *)malloc(sizeof(QueueNode)); w->data=i; w->next=NULL; Wait->rear->next=w; Wait->rear=w; printf("The PART is full,car must wait in the road!"); sleep(1); return 1; } return 0;}/********************************************************************************/int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait) /*车辆离开函数*/{ int flag=0,a1,a2,b1,b2, money; CarNode *p,*t; QueueNode *q; p=(CarNode *)malloc(sizeof(CarNode)); flushall(); printf("Input the out car number: "); gets(p->num); while(!StackEmpty(*In)) { t=(CarNode *)malloc(sizeof(CarNode)); Pop(In,t); if(strcmp(p->num,t->num)==0) /*比较车场中有无这辆车,有即出站*/ { printf("Input the time the car out(00:00):"); scanf("%d:%d",&p->leave.hour,&p->leave.min); printf("The "); printf("%s",p->num); printf(" Car out the part!"); a1= p->leave.hour; a2= t->reach.hour; b1= p->leave.min; b2= t->reach.min; money = ((a1-a2+24)%24*60+(b1-b2+60)%60)*price; /*计算车辆需要的费用*/ printf("\nThe time the car arrive: %d:%d",t->reach.hour,t->reach.min); printf("\nThe time the car leave: %d:%d",p->leave.hour,p->leave.min); printf("\nNeed: %d yuan",money); flag=1; getch(); free(t); break; } else Push(temp,t); } /*while*/
if(!flag) { printf("No this car!!"); getch(); } while(!StackEmpty(*temp)) { Pop(temp,p); Push(In,p); } free(p); if(flag&&Wait->front!=Wait->rear) /*车站中有空位,便道有车,车入站*/ { q=(QueueNode *)malloc(sizeof(QueueNode)); q=Wait->front->next; t=q->data; if(q!=NULL) { Push(In,t); printf("\nThe "); printf("%s",t->num); printf(" car in part!"); printf("\nInput the time the car arrive(00:00): "); scanf("%d:%d",&t->reach.hour,&t->leave.min); } Wait->front->next=q->next; if(q==Wait->rear) Wait->rear=Wait->front; free(q); } return 1;}/********************************************************************************/void Print(SqStackCar *In,SqStackCar *Temp,LinkQueueCar *Wait) /*打印函数*/{ int c=0; int count=1; CarNode *p,*t; QueueNode *q; q=(QueueNode *)malloc(sizeof(QueueNode)); p=(CarNode *)malloc(sizeof(CarNode)); t=(CarNode *)malloc(sizeof(CarNode)); while(1&&c!='3') { clrscr(); gotoxy(1,10); printf("1. Print the road!"); gotoxy(1,11); printf("2. Print the part!"); gotoxy(1,12); printf("3. return."); do{ printf("\nInput your choice:"); c = getche(); printf("\n"); }while(c!='1'&&c!='2'&&c!='3');
if(c=='2') /*打印停车场*/{ printf("The car in the part!\n"); count=1;
while(!StackEmpty(*In)) { Pop(In,t); Push(Temp,t); } while(!StackEmpty(*Temp)) { Pop(Temp,t); printf("The "); printf("%d",count); printf(" car number is: "); count++; puts(t->num); Push(In,t); } printf("Press any key to continue..."); getch();
}if(c=='1') /*打印便道*/ { printf("The car in the road!\n"); count=1; q=Wait->front->next; if(Wait->front!=Wait->rear) /**/ { while(q!=NULL) { p=q->data; printf("The "); printf("%d",count); printf(" Car number is: "); puts(p->num); q=q->next; count++; } } else printf("\nNo car in the road."); printf("Press any key to continue..."); getch(); } }}/***************************主程序***********************************************/int Arrive(SqStackCar *In,LinkQueueCar *Wait);int Departure(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);void Print(SqStackCar *In,SqStackCar *temp,LinkQueueCar *Wait);void initialization();char readcommand();void interpret(char cmd);main(){ char cmd; SqStackCar part,temp; LinkQueueCar road; InitStack(&part); InitStack(&temp); InitQueue(&road); printf("Press any key to continue......"); getch(); while(1) { initialization(); /*初始化界面*/ cmd = readcommand(); /*读取停车场状况*/ clrscr(); switch(cmd) { case 'a': Arrive(&part,&road); break; case 'A': Arrive(&part,&road); break; case 'd': Departure(&part,&temp,&road); break; case 'D': Departure(&part,&temp,&road); break; case 'p': Print(&part,&temp,&road); break; case 'P': Print(&part,&temp,&road); break; case 'e': printf("Press any to continue...");getch();exit(0); break; case 'E': printf("Press any to continue...");getch();exit(0); break; default : printf("ERROR!"); break; } }}/********************************************************************************/void initialization() /*初始函数*/{ int i; clrscr(); gotoxy(0,0); for(i=1;i<=240;i++) printf("\1"); gotoxy(15,8); printf("THIS IS A CAR PART MANAGE SYSYTEM!"); gotoxy(15,12); printf("NAME: LIYONGJUN."); gotoxy(15,13); printf("NUM: 3104006893."); gotoxy(15,14); printf("GRADE: 2004."); gotoxy(15,15); printf("CLASS: COMPUTER SCIENCE AND TECHNOLOGY 10"); gotoxy(1,20); printf("\n********************************************************************************"); printf("1. Car Arrive--A 2. Car Departure--D 3. Print Car--P 4.Exit--E"); printf("\n********************************************************************************"); printf("Input C,D,P,E choose!!\n");
}
char readcommand() /*选择函数*/{ char cmd; do{ printf("Input your choice:"); cmd = getche(); printf("\n"); }while((cmd!='a')&&(cmd!='A')&&(cmd!='d')&&(cmd!='D')&&(cmd!='p')&&(cmd!='P')&&(cmd!='E')&&(cmd!='e')); return cmd;}