停车场管理系统(数据结构课程设计)
- 格式:doc
- 大小:67.50 KB
- 文档页数:10
停车场管理系统一编程思想:将停车场设计成一个虚拟的栈,将其入口设计成栈顶,在设计一个倒车场,也将其设计成一个虚拟的栈,使其入口为栈顶,再根据队列设计一个供车来停放的便道,当停车场里的车辆不为满时,车辆进入停车场,当停车场里的车辆为满时,车辆在便道里等候,当车辆从停车场里出来的时候,如果出来的不是第一个车辆,则其前面的车辆将进入倒车场里面,待车辆出去后,倒车场里的车再依次进入停车场,如果停车场不满的话,便道里等候的第一辆车进入停车场,进入停车场的时间为停车场中车辆出来的时间,再设计一个函数用来显示停车场以及便道中车辆的信息。
二程序流程图:三,程序代码:#include<stdio.h>#define Size 3 /*车库容量*/#define Price 5 /*单位价格*/#define Null 0typedef struct time{int hour; /*时间结点*/}Time;typedef struct{int car_number;Time arrivetime,leavetime; /*车信息结点*/ int fee;}car_info;typedef struct{car_info *north;car_info *south; /*停车场信息*/int number;}car_park;typedef struct{car_info *west;car_info *east; /*倒车场信息*/int number;}car_park_back;Typedef struct car{car_info *data;struct car *next; /*结点信息*/}carnode;typedef struct node{carnode *head;carnode *rear; /*便道信息*/int number;}car_park_temp;void init_car_park(car_park *cp){cp->north=(car_info *)malloc(Size * sizeof(car_info)); /*初始化停车场*/ if(!cp->north) printf("error\n");cp->south=cp->north; /*令栈顶等于栈底*/cp->number=0;}void enter_car_park(car_park *cp,car_info *car){*cp->south++=*car; /*车辆进入停车场*/cp->number++;}int notfull_car_park(car_park *cp){int e;if(cp->south-cp->north>=Size) /*判断常常是否已满*/e=0;elsee=1;return(e);int notempty_car_park_back(car_park_back *cpb){int e;if(cpb->east==cpb->west)e=0; /*判断倒车场是否不空*/ elsee=1;return(e);}void back_car_park(car_park *cp,car_info *car){*car=*cp->south; /*进行倒车操作*/cp->number--;}void init_car_park_back(car_park_back *cpb){cpb->west=(car_info *)malloc(Size *sizeof(car_info));if(!cpb->west) printf("error\n"); /*倒车场初始化*/cpb->east=cpb->west;cpb->number=0;}void enter_car_park_back(car_park_back *cpb,car_info *car) {*cpb->east++=*car;cpb->number++; /*进入倒车场操作*/}void leave_car_park_back(car_park_back *cpb,car_info *car) {*car=*--cpb->east;cpb->number--; /*离开倒车场操作*/void init_car_park_temp(car_park_temp *cpt){cpt->head=cpt->rear=(carnode *)malloc(sizeof(carnode)); /*初始化便道*/ cpt->head->next=Null;cpt->number=0;}void enter_car_park_temp(car_park_temp *cpt,car_info *car){carnode *p;p=(carnode *)malloc(sizeof(carnode)); /*进入便道操作*/p->data=car;p->next=Null;cpt->rear->next=p;cpt->rear=p;cpt->number++;}void leave_car_park_temp(car_park_temp *cpt,car_info *car,car_park *cp) {carnode *p;p=cpt->head->next;car=p->data; /*离开便道操作*/cpt->head->next=p->next;enter_car_park(cp, car); /*进入停车场操作*/cpt->number--;}int notempty_car_park_temp(car_park_temp *cpt){int e;if(cpt->head==cpt->rear) /*判断便道是否为空*/e=0;elsee=1;return(e);}void leave_car_park(car_park *cp,car_info *car,car_park_back *cpb){int e, a1,b1,t; /*定义时间变量*/car_info *car1,*car2;car1=(car_info *)malloc(sizeof(car_info)); /*车辆实体化*/car2=(car_info *)malloc(sizeof(car_info));while((--cp->south)->car_number!=car->car_number) /*判断车号是否为要出去的车号*/ {back_car_park(cp,car1); /*进行倒车操作*/enter_car_park_back(cpb,car1); /*进入倒车场*/}car->arrivetime.hour=cp->south->arrivetime.hour;a1=car->arrivetime.hour;b1=car->leavetime.hour;t=(b1-a1);car->fee=t*Price; /*计算价格*/printf("the time of the car is %3d hour\n",t);printf("the money is %3d yuan\n",car->fee);e=notempty_car_park_back(cpb); /*判断倒车场是否为空*/while(e==1){leave_car_park_back(cpb,car2); /*离开倒车场enter_car_park(cp,car2); 进入停车场e=notempty_car_park_back(cpb); 判断倒车场是否为空*/}cp->number--;}void main() /*主函数*/{char ch; /*定义字符和int e,n,i; 整形变量*/car_park_back *cpb; /* 定义停车场,倒车场以及便道的变量*/ car_park *cp;car_park_temp *cpt,*cpt2;car_info *car;cp=(car_park *)malloc(sizeof(car_park)); /*实体化变量*/cpb=(car_park_back *)malloc(sizeof(car_park));cpt=(car_park_temp *)malloc(sizeof(car_park_temp));init_car_park(cp); /*实体化停车场,倒车场,便道*/init_car_park_back(cpb);init_car_park_temp(cpt);do{car=(car_info *)malloc(sizeof(car_info));printf("\ninput the 'A' or 'L' or 'X' ,end with '0' :");/*输入待操作的命令*/ scanf("%s",&ch);e=notfull_car_park(cp); /*判断车场是否为空*/switch(ch) /*判断要输入的命令*/{case 'A':if(e==1) /*车场不满,进入车辆*/{printf("input the car_number:"); /*输入车辆的基本信息*/scanf("%d",&car->car_number);printf("input the arrivetime:");scanf("%d",&(*car).arrivetime.hour);enter_car_park(cp,car); /*进入车场*/printf("the car is in the car_park,the place is %d\n",cp->number);}else /*若车场满,进入便道*/{enter_car_park_temp(cpt,car);printf("input the car_number:");scanf("%d",&car->car_number);printf("the car is in the car_park_temp,the place of temp is %d\n",cpt->number);}break;case 'L': /*离开停车场*/printf("input the car_number:"); /*输入要离开车辆的号码以及离开时间*/scanf("%d",&car->car_number);printf("input the leavetime of the car:");scanf("%d",&(*car).leavetime.hour);leave_car_park(cp,car,cpb); /*执行离开车场*/i=(*car).leavetime.hour; /*令便道里车辆进入车场的时间和车场里面的车离开的时间相等*/n=notempty_car_park_temp(cpt); /*判断便道是否不空*/if(n==1)printf("The car %d",cpt->head->next->data->car_number); /*记住便道里第一辆车的号码*/ leave_car_park_temp(cpt,car,cp); /*离开便道,进入停车场*/printf(" is enter the car_park,arrivetime is %d ,the place of carpark is %d\n",i,cp->number);break;case 'X': /*查看车场以及便道里的车辆信息*/printf("\nThere are %d cars in car park!\nFolowing is the carnumber in the car park:\n ",cp->number);for(i=1;i<=cp->number;i++) /*做循环,依次输出停车场里车辆的信息*/printf("%d,",(cp->north++)->car_number);cp->north=cp->north-cp->number;cpt2->head=cpt->head; /*设一个虚拟指针使其指向头结点的下一个位置*/ if(cpt->number==0) /*便道里没有车的情况*/printf("\nThere is no cars in temp!") ;else{ printf("\nThere are %d cars in car temp!\nFolowing is the carnumber in the car temp:\n ",cpt->number);for(i=1;i<=cpt->number;i++) /*做一个循环,输出便道里车辆的信息*/ {printf("%d ",cpt2->head->next->data->car_number); /* 输出车辆的号码*/ cpt2->head=cpt2->head->next;}}break;default:break; /*退出循环*/}}while(ch!='0'); /*退出操作*/}。