#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<string.h>
struct info
{
int num;//汽车进停车场的序号
char carn[100];//汽车牌照
};
typedef struct stack_num
{
struct info car;
struct stack_num *next;
}Num; //停车场中存放汽车的栈
struct node
{
struct info car;
struct node *next;
};
typedef struct queue //等候队列
{
struct node *front;
struct node *rear;
}que;
int que_flag;
que *ue; //队列
int flag;//标记停车场中汽车听的数量
Num *num_top;//停车场栈中的栈顶
Num *temp_top;//临时栈的栈顶
int park_time[100];//存放停车时间的数组
void cre_num_stack() //创栈
{
Num *num_top = (Num*)malloc(sizeof(Num));
num_top->next = NULL;
}
void cre_temp_stack() //创临时栈
{
Num *temp_top = (Num*)malloc(sizeof(Num));
temp_top->next = NULL;
}
void push_num(struct info new) //压栈
{
Num *p =(Num*)malloc(sizeof(Num));
p->car.num = new.num;
strcpy(p->car.carn,new.carn);
// p->carn = a;
p->next = num_top;
num_top = p;
}
void push_temp(struct info new)
{
Num *p =(Num*)malloc(sizeof(Num));
p->car.num = new.num;
strcpy(p->car.carn,new.carn);
//p->carn = a;
p->next = temp_top;
temp_top = p;
}
void pop_num() //出栈
{
Num *p = (Num*)malloc(sizeof(Num));
p->car = num_top->car;
//p->carn = num_top->carn;
// strcpy(p->carn,num_top->carn);
p = num_top;
num_top = num_top->next;
}
void pop_temp()
{
Num *p = (Num*)malloc(sizeof(Num));
p->car = temp_top->car;
//p->carn = num_top->carn;
// strcpy(p->carn,num_top->carn);
p = temp_top;
temp_top = temp_top->next;
}
struct info get_num()
{
if(num_top == NULL)
{
printf("绌烘爤\n");//获取栈顶元素结构体信息
}
else
{
return num_top->car;
}
}
struct info get_temp()
{
if(temp_top == NULL)
{
printf("绌烘爤\n");
}
else
{
return temp_top->car;
}
}
void print_num() //打印队列信息
{
Num *f = num_top;
while(f != NULL)
{
printf("%d ",f->car.num);
printf("%s ;",f->car.carn);
f = f->next;
}
}
void print_temp()
{
Num *f = temp_top;
while(f != NULL)
{
printf("%d ",f->car.num);
printf("%s\n",f->car.carn);
f = f->next;
}
}
void cre_que() //创建队列
{
ue=(que*)malloc(sizeof(que));
if(ue == NULL)
{
printf("鍒涘缓闃熷垪澶辫触\n");
}
ue->front = (struct node*)malloc(sizeof(struct node));
if(ue->front == NULL)
{
printf("鍒涘缓闃熷垪澶辫触\n'");
}
ue->rear = ue->front;
}
void enter_que(struct info new) //进队列
{
struct node *p = (struct node*)malloc(sizeof(struct node));
p->car.num = new.num;
// printf("%s\n",new.carn);
strcpy(p->car.carn,new.carn);
//printf("%s\n",p->car.carn);
// p->carn = a;
p->next = NULL;
ue->rear->next = p;
ue->rear = p;
}
struct info out_que() //出队列
{
struct node *p = (struct node*)malloc(sizeof(struct node));
struct info first;
first = ue->front->next->car;
p = ue->front;
p->next = p->next->next;
p = p->next;
return first;
}
void print_que() //打印队列
{
struct node *f = ue->front;
if(f->next == NULL)
{
printf("绛夊€欏尯娌℃湁杞﹁締\n");
}
while(f->next != NULL)
{
printf("%d ",f->next->car.num);
printf("%s\n",f->next->car.carn);
f = f->next;
}
}
void sub_one() //将队列里面汽车序号减一
{
struct node *f = ue->front;
while(f->next != NULL)
{
f->next->car.num = f->next->car.num - 1;
f = f->next;
}
}
void park_car() //停车函数,将进入停车场的汽车压入栈中
{
char m[100];
struct info new;
printf("请输入汽车牌照\n");
new.num = flag; //汽车序号为flag的
scanf("%s",m);
strcpy(new.carn,m);
push_num(new);
park_time[que_flag] = time((time_t*)NULL);//停车的开始时间
}
void car_que() //停车场满后,进入队列
{
char a[100];
printf("停车场已满请到等候队列中等候\n");
struct info new_que;
new_que.num = flag - 10;
scanf("%s",a);
strcpy(new_que.carn,a);
enter_que(new_que);
}
void car_from_que() //当停车场有车出来时,队列中的第一辆车进入,依次进入
{
struct info last_car;
if(ue->front == ue->rear )
{
printf("等候队列中没有车\n");
}
else
{
printf("进队列的车停在第一个位置\n");
last_car = out_que();
last_car.num = 10; //从队列进来的车刚开始序号都标为十号
//park_car();
push_num(last_car); //
print_num();
printf("\n");
sub_one();//将原来队列中所有的序号都减一
}
}
void leave(int leave_num) //离开函数,将输入的要离开的停车号上所有的栈顶的元素出栈
{
struct info Top_car;
struct info Temp_car;
Top_car = get_num();
if(leave_num > Top_car.num)
{
printf("请重新输入\n");
}
else
{
while(Top_car.num != leave_num)
{
push_temp(Top_car);
pop_num();
Top_car = get_num();
}
pop_num();
Temp_car = get_temp();
Temp_car.num = Temp_car.num - 1;
while(temp_top->next)
{
push_num(Temp_car);
pop_temp();
Temp_car = get_temp();
Temp_car.num = Temp_car.num - 1;
}
push_num(Temp_car);
car_from_que();
flag --;
}
}
int main()
{
int out_time;
int i;
int j;
int action;
int car_num;
int out_car_num;
int leave_num;
int te;
int en;
int ou;
int parktime;
int temp[100];
flag = que_flag = 1;
cre_num_stack();
cre_temp_stack();
cre_que();
printf("欢迎来到停车场\n");
printf("1:停车\n");
printf("2:离开\n");
printf("3:显示当前停车场停车情况\n");
printf("4:退出\n");
while(1)
{
printf("请选择操作\n");
scanf("%d",&action);
switch(action)
{
case 1:if(flag <= 10)
{
park_car();
flag ++;
que_flag ++;
print_num();
}
else
{
printf("车位已满请到队列中等待\n");
car_que();
flag ++;
print_que();
}
break;
case 2:printf("请输入要离开汽车的车序号\n");
scanf("%d",&leave_num);
leave(leave_num);
break;
case 3:printf("车的序号\n ");
print_num();
printf("\n");
printf("车所停留的时间\n");
out_time = time((time_t*)NULL);
for(j = que_flag - 1;j > 0;j --)
{
printf("%d ",out_time - park_time[j]);
}
printf("\n");
break;
case 4:exit(0);break;
default:break;
}
}
return 0;
}