#include<stdio.h>
#include<stdlib.h>
struct Stack {
int *carnumber;
int *arrivetime;
int top;
int size;
};
void initStack(struct Stack* a,int size) {
a->top = -1;
a->size = size;
a->carnumber = (int*)malloc(sizeof(int) * size);
if (a->carnumber == NULL) {
exit(0);
}
a->arrivetime = (int*)malloc(sizeof(int) * size);
if (a->arrivetime == NULL) {
exit(0);
}
}
void Stack_push(struct Stack* a, int carnumber, int arrivetime) {
a->top++;
a->arrivetime[a->top] = arrivetime;
a->carnumber[a->top] = carnumber;
}
int Stack_top_time(struct Stack a) {
return a.arrivetime[a.top];
}
int Stack_top_number(struct Stack a) {
return a.carnumber[a.top];
}
void Stack_pop(struct Stack* a) {
if (a->top == -1)
exit(0);
a->top--;
}
int StackNum(struct Stack a) {
return a.top+1;
}
int Full_Stack(struct Stack a) {
if (a.top == a.size - 1)
return 1;
else return 0;
}
struct QueueNode {
int carnumber;
int arrivetime;
struct QueueNode* next;
};
struct Queue {
struct QueueNode* front;
struct QueueNode* rear;
int num;
};
void initQueue(struct Queue* a) {
a->front = a->rear = (struct QueueNode*)malloc(sizeof(struct QueueNode));
if (a->rear == NULL) {
exit(0);
}
if (a->front == NULL) {
exit(0);
}
a->front->next =NULL;
a->num = 0;
}
void push_Queue(struct Queue* a, int carnumber, int arrivetime) {
struct QueueNode* t;
t = (struct QueueNode*)malloc(sizeof(struct QueueNode));
if (t == NULL) {
exit(0);
}
t->carnumber = carnumber;
t->arrivetime = arrivetime;
t->next = NULL;
a->rear->next = t;
a->rear = t;
a->num++;
}
int empty_Queue(struct Queue a) {
if (a.front == a.rear)
return 1;
else
return 0;
}
void pop_Queue(struct Queue* a) {
if (empty_Queue(*a))
exit(0);
struct QueueNode* t = a->front->next;
a->front->next = t->next;
free(t);
a->num--;
}
int Queue_Number(struct Queue a) {
return a.front->carnumber;
}
int Queue_Time(struct Queue a) {
return a.front->arrivetime;
}
void Car_in(struct Stack* a, struct Queue* b, int carnumber, int arrivetime) {
if (!Full_Stack(*a)) {
Stack_push(a, carnumber, arrivetime);
printf("车牌号为:%d的车停车成功!\n停车序号为:%d\n", carnumber, a->top + 1);
return;
}
else {
push_Queue(b, carnumber, arrivetime);
printf("停车场已满!\n车牌号为:%d的车停车在便道!\n便道序列为:%d\n", carnumber, b->num);
}
}
void Car_out(struct Stack* a, struct Stack* b, struct Queue* c, int fee,int carnumber,int time) {
int flag = 0,i,j;
for (i = 0; i <= a->top; i++) {
if (carnumber == a->carnumber[i]) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("停车场里无该车!");
return;
}
for (j = a->top; j >= i + 1; j--) {
Stack_push(b, Stack_top_number(*a), Stack_top_time(*a));
Stack_pop(a);
}
printf("车牌号为:%d的车成功出站!\n停车费用为:%d\n", Stack_top_number(*a), fee * (time - Stack_top_time(*a)));
Stack_pop(a);
while (b->top >= 0) {
Stack_push(a, Stack_top_number(*b), Stack_top_time(*b));
}
if (c) {
Car_in(a, c, Queue_Number(*c), Queue_Time(*c));
pop_Queue(c);
}
}
int main(void) {
int fee,size,carnumber,time;
struct Stack* stop, * temp;
printf("\t欢迎进入停车场管理系统!\n请输入要设置的停车场大小及每小时停车费用:\n");
scanf("%d %d", &size, &fee);
initStack(stop, size);
initStack(temp, size);
struct Queue* access;
initQueue(access);
printf("请输入分别到达离去信息、车牌号码以及时间:(I为停车,O为出停车场,E为结束系统)\n");
char signal;
scanf("%c %d %d", &signal, &carnumber, &time);
while (signal != 'E') {
if (signal == 'I') {
Car_in(stop, access, carnumber, time);
}
else if (signal == 'O') {
Car_out(stop, temp, access, fee, carnumber, time);
}
printf("请输入分别到达离去信息、车牌号码以及时间:\n");
scanf("%c %d %d", &signal, &carnumber, &time);
}
if (signal == 'E')
printf("\t退出系统!");
return 0;
}