- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define MAX 5//停车场的容量
- #define PRICE 2.0//停车收费价格:元/小时
- typedef struct car
- {
- char name[10];//车牌号
- struct tm intime;//入库时间
- struct tm outtime;//出库时间
- int expense;//停车费用
- int length;//停车时间
- int position;//车位
- }CAR;//车位信息
- typedef struct stack
- {
- int top;
- CAR num[MAX];
- }Stack;//栈
- struct carnode
- {
- CAR data;
- struct carnode*next;
- };//过道车的结点
- typedef struct carnode Carnode;
- typedef struct carnode* Link;
- typedef struct queue
- {
- Carnode*head;
- Carnode*rear;
- }Queue;//队列
- int initstack(Stack **s)//初始化栈
- {
- (*s) = (Stack*)malloc(sizeof(Stack));
- if(*s == NULL)
- {
- printf("分配内存出错!n");
- exit(-1);
- }
- (*s)->top = -1;
- return 0;
- }
- int stackisempty(Stack **s)//判断栈是否为空
- {
- if((*s)->top == -1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int stackisfull(Stack **s)//判断栈是否为满
- {
- if((*s)->top == MAX-1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int stackpush(Stack **s,CAR car)//入栈
- {
- if(!stackisfull(s))
- {
- (*s)->top++;
- (*s)->num[(*s)->top] = car;
- }
- }
- CAR stackpop(Stack **s)//出栈
- {
- CAR car;
- if(!stackisempty(s))
- {
- car = (*s)->num[(*s)->top];
- (*s)->top--;
- }
- return car;
- }
- int initqueue(Queue **q)//初始化队列
- {
- (*q) = (Queue*)malloc(sizeof(Queue));
- if(*q == NULL)
- {
- printf("内存分配出错!\n");
- exit(-1);
- }
- (*q)->head = NULL;
- (*q)->rear = NULL;
- return 0;
- }
- int queueisempty(Queue **q)//判断队列是否为空
- {
- if((*q)->head == NULL && (*q)->rear == NULL)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- int queuepush(Queue **q,CAR car)//入队列
- {
- Link tmp = NULL;
- tmp = (Link)malloc(sizeof(Carnode));
- if(tmp == NULL)
- {
- printf("内存分配失败!\n");
- exit(-1);
- }
- tmp->data = car;
- tmp->next = (*q)->head;
- (*q)->head = tmp;
- return 0;
- }
- CAR queuepop(Queue **q)//出队列
- {
- CAR car;
- Link tmp = NULL;
- Link p = NULL;
- tmp = (Link)malloc(sizeof(Carnode));
- if(tmp == NULL)
- {
- printf("内存分配失败!\n");
- exit(-1);
- }
- tmp = (*q)->head;
- if(!queueisempty(q))
- {
- if(tmp == (*q)->rear)
- {
- car = tmp->data;
- free(tmp);
- (*q)->head = NULL;
- (*q)->rear = NULL;
- return car;
- }
- while(tmp->next != (*q)->rear)
- {
- p = tmp;
- tmp = tmp->next;
- }
- if((*q)->rear == NULL)
- {
- if(tmp == (*q)->head)
- {
- car = ((*q)->head)->data;
- free((*q)->head);
- (*q)->head = NULL;
- }
- else
- {
- car = tmp->data;
- free(tmp);
- (*q)->rear = p;
- p->next = NULL;
- }
- }
- else
- {
- car = ((*q)->rear)->data;
- free((*q)->rear);
- (*q)->rear = tmp;
- tmp->next = NULL;
- }
- }
- return car;
- }
- int printfdata(struct tm gm)//显示时间
- {
- printf("%d/%d %2d:%2d:%2d\n",gm.tm_mon,gm.tm_mday,gm.tm_hour+8,gm.tm_min,gm.tm_sec);
- return 0;
- }
- int showstack(Stack **s)//查看车库车辆
- {
- int i;
- printf("****************************************\n");
- printf("车位使用情况\n");
- if(stackisempty(s))
- {
- printf("停车场已没有车辆!\n");
- }
- else
- {
- printf("位置\t车牌号\t进站时间\n");
- for(i = 0; i <= (*s)->top;i++)
- {
- printf("%d\t",(*s)->num[i].position);
- printf("%s\t",(*s)->num[i].name);
- printfdata((*s)->num[i].intime);
- }
- printf("\t\t\t共%d辆车!",(*s)->top+1);
- if((*s)->top == MAX-1)
- {
- printf("(已满)\n");
- }
- else
- {
- printf("还可以停放%d辆车\n",MAX - 1 - (*s)->top);
- }
- }
- printf("*******************************************\n");
- return 0;
- }
- int showqueue(Queue **q)//查看过道车辆
- {
- printf("****************************************\n");
- if(!queueisempty(q))
- {
- Carnode *p = NULL;
- p = (Link)malloc(sizeof(Carnode));
- if(p == NULL)
- {
- printf("内存分配出错!\n");
- exit(-1);
- }
- p = (*q)->head;
- printf("过道使用情况!\n");
- printf("车牌号\t进入时间\n");
- while(p != NULL)
- {
- printf("%s\t",p->data.name);
- printfdata(p->data.intime);
- p = p->next;
- }
- }
- else
- {
- printf("过道上没有车在等待\n");
- }
- printf("****************************************\n");
- return 0;
- }
- int showall(Stack **s,Queue **q)//查看整个停车场车辆
- {
- showstack(s);
- showqueue(q);
- menu(s,q);
- return 0;
- }
- int reach(Stack **s,Queue **q)//入库
- {
- CAR car;
- struct tm *gm;
- time_t lt;
- time(<);
- gm = gmtime(<);
- car.intime = *gm;
- printf("输入车牌号:");
- scanf("%s",car.name);
- if(!stackisfull(s) && queueisempty(q))//栈未满,入栈
- {
- car.position = ((*s)->top) + 2;
- stackpush(s,car);
- showstack(s);
- }
- else if(stackisfull(s) || !queueisempty(q))//栈满,入队列
- {
- printf("提示:车位满,只有先停放在过道上!\n");
- car.position = MAX;
- queuepush(q,car);
- showall(s,q);
- }
- menu(s,q);
- return 0;
- }
- int printfrate(CAR *car)//打印帐单
- {
- printf("****************************************\n");
- printf("帐单\n");
- printf("车牌号:%s\n",car->name);
- printf("停车位置:%d\n",car->position);
- printf("进入时间:");
- printfdata(car->intime);
- //printf("\n");
- printf("离开时间:");
- printfdata(car->outtime);
- //printf("\n");
- printf("停车时间(秒):%d\n",car->length);
- printf("费用(元):%d\n",car->expense);
- //printf("\n\n");
- printf("****************************************\n");
- return 0;
- }
- int leave_s(Stack **s,Queue **q)//离开车库
- {
- struct tm *gm;
- time_t lt;
- Stack *p = NULL;
- initstack(&p);
- //char nowtime[10];
- CAR car;
- int i;
- int pos;
- if(stackisempty(s))
- {
- printf("所有车位是空的,没有车辆需要离开!\n");
- }
- else
- {
- printf("现在车位使用情况!\n");
- showstack(s);
- printf("哪个车位的车辆需要离开:");
- scanf("%d",&pos);
- if(pos > 0 && pos <= (*s)->top+1)
- {
- for(i = (*s)->top+1; i > pos;i--)
- {
- car = stackpop(s);
- car.position = car.position - 1;
- stackpush(&p,car);
- }
- car = stackpop(s);
- time(<);
- gm = gmtime(<);
- car.outtime = (*gm);//出栈时间
- car.length = mktime(&car.outtime)-mktime(&car.intime);
- car.expense = (car.length/3600+1)*PRICE;
- printfrate(&car);
- while(!stackisempty(&p))
- {
- car = stackpop(&p);
- stackpush(s,car);
- }
- while(!stackisfull(s) && !queueisempty(q))
- {
- car = queuepop(q);
- time(<);
- gm = gmtime(<);
- car.intime = (*gm);
- stackpush(s,car);
- }
- }
- else
- {
- printf("输入车位错误,那个车位没有车!\n");
- }
- }
- menu(s,q);
- return 0;
- }
- int leave_q(Stack **s,Queue **q)
- {
- char name[10];
- Carnode *tmp = NULL;
- Carnode *p = NULL;
- CAR car;
- /*tmp = (Link)malloc(sizeof(Carnode));
- if(tmp == NULL)
- {
- printf("内存分配失败!\n");
- exit(-1);
- }*/
- tmp = (*q)->head;
- if(queueisempty(q))
- {
- printf("过道上是空的,没有车辆需要离开!\n");
- }
- else
- {
- showqueue(q);
- printf("过道上哪个车需要离开,输入车牌号:");
- scanf("%s",name);
- while(tmp != NULL)
- {
- if(strcmp(name,(tmp->data).name) == 0)
- {
- break;
- }
- p = tmp;
- tmp = tmp->next;
- }
- if(tmp == NULL)
- {
- printf("过道没有这辆车!\n");
- }
- else
- {
- if((*q)->rear == NULL)
- {
- if(tmp->next == NULL)
- {
- if(tmp == (*q)->head)
- {
- car = tmp->data;
- free(tmp);
- (*q)->head = NULL;
- }
- else
- {
- car = tmp->data;
- p->next = NULL;
- }
- }
- else
- {
- if(tmp == (*q)->head)
- {
- car = tmp->data;
- (*q)->head = tmp->next;
- free(tmp);
- }
- else
- {
- car = tmp->data;
- p->next = tmp->next;
- free(tmp);
- }
- }
- }
- else
- {
- if(tmp == (*q)->head)
- {
- if(tmp == (*q)->rear)
- {
- car = tmp->data;
- free(tmp);
- (*q)->head = NULL;
- (*q)->rear = NULL;
- }
- else
- {
- car = tmp->data;
- (*q)->head = tmp->next;
- free(tmp);
- }
- }
- else
- {
- if(tmp == (*q)->rear)
- {
- car = tmp->data;
- (*q)->rear = p;
- p->next = NULL;
- free(tmp);
- }
- else
- {
- car = tmp->data;
- p->next = tmp->next;
- free(tmp);
- }
- }
- }
- printf("************************\n");
- printf("车牌号:%s\n",car.name);
- printf("进入过道时间:");
- printfdata(car.intime);
- printf("不收费,欢迎再次使用!\n");
- printf("************************\n");
- }
- }
- return 0;
- }
- int leave(Stack **s,Queue **q)
- {
- char ch;
- while(1)
- {
- printf("输入你的车位置(1)车库(2)过道:");
- scanf(" %c",&ch);
- if(ch == '1' || ch == '2')
- {
- break;
- }
- else
- {
- printf("输入序号有误!\n");
- }
- }
- switch(ch)
- {
- case '1':leave_s(s,q);
- break;
- case '2':leave_q(s,q);
- break;
- }
- menu(s,q);
- return 0;
- }
- int quit(Stack **s,Queue **q)//退出系统
- {
- printf("欢迎你的下次使用!\n");
- exit(0);
- return 0;
- }
- int menu(Stack **s,Queue **q)//菜单
- {
- char ch;
- printf("******欢迎使用停车场系统******\n");
- printf(" (1)驶入停车场 \n");
- printf(" (2)离开停车场 \n");
- printf(" (3)查看停车场信息 \n");
- printf(" (4)退出系统 \n");
- printf("提示:本停车场共%d个车位,停满后的车辆停在过道上!\n",MAX);
- printf("收费标准,停在停车场的车辆:%.2f元/小时,停在过道上的车辆不收费!\n",PRICE);
- while(1)
- {
- printf("输入你的选择(1-4):");
- scanf(" %c",&ch);
- if(ch >= '1' && ch <= '4')
- {
- break;
- }
- else
- {
- printf("输入序号有误!\n");
- }
- }
- switch(ch)
- {
- case '1': reach(s,q);
- break;
- case '2': leave(s,q);
- break;
- case '3': showall(s,q);
- break;
- case '4': quit(s,q);
- break;
- }
- return 0;
- }
- int main()
- {
- Stack *s = NULL;
- initstack(&s);
- Queue *q = NULL;
- initqueue(&q);
- menu(&s,&q);
- return 0;
- }
停车场
最新推荐文章于 2024-03-15 12:45:30 发布