停车场管理
问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1) 建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2) 输入数据模拟管理过程,数据(入或出,车名)。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
struct node
{
time_t time;
char *name;
struct node *next;
};
struct node *wait_top;
struct node *park_top;
struct node *wait_que;
struct node *first;
struct node *last;
void init_park_top() //初始化停车栈。
{
park_top = (struct node *)malloc(sizeof(struct node));
park_top = NULL;
}
void init_wait_park() //初始化等待栈。
{
wait_top = (struct node *)malloc(sizeof(struct node));
wait_top = NULL;
}
void init_wait_que() //初始化等待队列。
{
wait_que = (struct node *)malloc(sizeof(struct node));
wait_que->next = NULL;
first = last = wait_que;
}
int isfull() //判断是否栈满。
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr = park_top;
int num = 0;
while(ptr != NULL)
{
num++;
ptr = ptr->next;
}
if(num >= 5)
{
return 1;
}
else
{
return 0;
}
}
void park_pop(char *str) //停车场停车。
{
time_t t;
if((isfull()) == 0)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->name = (char *)malloc(100);
strcpy(ptr->name,str);
time(&t);
ptr->time = t;
ptr->next = park_top;
park_top = ptr;
}
}
void wait_park_pop(char *str)//进入等候栈
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->name = (char *)malloc(100);
strcpy(ptr->name,str);
ptr->next = wait_top;
wait_top = ptr;
}
void enter_que(char *str)//进入等候队列
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->name = (char *)malloc(100);
strcpy(ptr->name,str);
last->next = ptr;
ptr->next = NULL;
first = wait_que->next;
}
/************若车在停车场内部,出车***********/
void swap(char *str)
{
int time_sum;
time_t time_now;
struct tm *tt;
struct node *ptr = (struct node *)malloc(sizeof(struct node));
while(strcmp(park_top->name,str) != 0)
{
wait_park_pop(park_top->name);
park_top = park_top->next;
}
time(&time_now);
tt = localtime(&time_now);
time_sum = time_now-park_top->time;
printf("离开成功,离开时间为:%d:%d:%d\n",tt->tm_hour,tt->tm_min,tt->tm_sec);
printf("车主%s停车时间为%d秒。\n",park_top->name,time_sum);
park_top = park_top->next;
while(wait_top != NULL)
{
park_pop(wait_top->name);
wait_top = wait_top->next;
}
}
void display()
{
time_t time_now;
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr = park_top;
time(&time_now);
while(ptr != NULL)
{
printf("%s %d秒,",ptr->name,time_now-ptr->time);
ptr = ptr->next;
}
printf("\n");
}
void my_print()
{
printf("-----welcome to our car parking-----\n");
printf("1.---------停车\n");
printf("2.---------离开\n");
printf("3.---------查看停车场停车情况\n");
printf("4.---------退出\n");
}
/**************数停车场里有多少车****************/
int count_park()
{
int num = 0;
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr = park_top;
while(ptr != NULL)
{
num++;
ptr = ptr->next;
}
return num;
}
/**********数等候队列里有多少车******************/
int count_que()
{
int count = 0;
struct node *ptr = wait_que;
if(ptr->next == NULL)
{
return 0;
}
else
{
while(ptr->next != NULL)
{
count++;
ptr = ptr->next;
}
return count;
}
}
/***********查停车场里是否有输入的姓名*************/
int search(char *name)
{
int flag = 0;
struct node *ptr = park_top;
while(ptr != NULL)
{
if(strcmp(ptr->name,name) == 0)
{
flag = 1;
}
ptr = ptr->next;
}
return flag;
}
void main_screen()
{
printf("***********************************************\n");
printf("停车场共5个车位,目前停车场共有%d辆车,等候区有%d辆车。\n",count_park(),count_que());
printf("***********************************************\n");
}
int main()
{
int choice;
struct tm *tt;
time_t time_now;
int time_sum;
char park_name[20];
char *wait_name = (char *)malloc(100);
char *leave_name = (char *)malloc(100);
init_park_top();
init_wait_que();
init_wait_park();
system("clear");
main_screen();
my_print();
while(1)
{
printf("请输入选择:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
system("clear");
main_screen();
my_print();
if(isfull() == 1)
{
printf("停车场已满,进入等候区排队。\n");
printf("输入等候车主姓名:");
scanf("%s",wait_name);
enter_que(wait_name);
}
else
{
printf("输入车主姓名:\n");
scanf("%s",park_name);
if(search(park_name) == 1)
{
strcat(park_name,"001");
printf("该姓名已存在,默认姓名后加001为%s\n",park_name);
}
park_pop(park_name);
tt = localtime(&park_top->time);
printf("停车成功,停车时间为:%d:%d:%d\n",tt->tm_hour,tt->tm_min,tt->tm_sec);
}
break;
case 2:
system("clear");
main_screen();
display();
my_print();
printf("输入离开车主姓名:\n");
scanf("%s",leave_name);
if(search(leave_name) == 0)
{
system("clear");
main_screen();
display();
my_print();
printf("查无此车!\n");
}
else
{
system("clear");
main_screen();
display();
my_print();
if(strcmp(leave_name,park_top->name) == 0)
{
time(&time_now);
time_sum = time_now-park_top->time;
tt = localtime(&time_now);
printf("离开成功,离开时间为:%d:%d:%d\n",tt->tm_hour,tt->tm_min,tt->tm_sec);
printf("车主%s停车时间为%d秒。\n",park_top->name,time_sum);
park_top = park_top->next;
if(first != wait_que)
{
park_pop(first->name);
wait_que->next = first->next;
first = wait_que->next;
}
}
else
{
swap(leave_name);
if(first != wait_que)
{
park_pop(first->name);
wait_que->next = first->next;
first = wait_que->next;
}
}
}
break;
case 3:
system("clear");
main_screen();
printf("****停车场停车信息****\n");
display();
my_print();
break;
case 4:
exit(0);
default:
break;
}
}
return 0;
}