#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define MAXNUM 3
#define PRIZE 3.0
typedef struct car
{
char num[10];
struct tm intime;
struct tm outtime;
double expense;
int length;
int position;
}CAR;
typedef struct stack
{
CAR park[MAXNUM];
int top;
}SeqStack;
typedef struct carnode
{
CAR data;
struct carnode * next;
}CarNode;
typedef CarNode * CarLink;
typedef struct queue
{
CarLink head;
CarLink rear;
}CarQueue;
void stackinit(SeqStack * stack);
int stack_full(SeqStack * stack);
int stack_empty(SeqStack * stack);
void push(SeqStack * stack, CAR car);
CAR pop(SeqStack * stack);
CAR getcar(SeqStack * stack,int nu);
void queueinit(CarQueue * queue);
void create_carnode(CarLink * carnode);
void
is_malloc_ok(CarLink carnode);
void enqueue(CarQueue ** queue,CarLink carnode,CAR car);
CAR dequeue(CarQueue ** queue);
int queue_empty(CarQueue * queue);
void choice(SeqStack *stack, CarQueue * queue);
void printdata(struct tm gm_data);
void showpark(SeqStack *stack);
void showaisle(CarQueue * queue);
void showall(SeqStack *stack,CarQueue * queue);
void inpark(SeqStack *stack, CarQueue * queue);
void outpark(SeqStack *stack, CarQueue * queue);
void printpay(CAR car);
int main()
{
SeqStack Park;
CarQueue Aisle;
stackinit(&Park);
queueinit(&Aisle);
load(&Park, &Aisle);
choice(&Park,&Aisle);
return 0;
}
void stackinit(SeqStack * stack)
{
stack->top = -1;
}
int stack_full(SeqStack * stack)
{
if (stack->top == MAXNUM – 1)
{
return 1;
}
else
{
return 0;
}
}
int stack_empty(SeqStack * stack)
{
if (stack->top == -1)
{
return 1;
}
else
{
return 0;
}
}
void push(SeqStack * stack, CAR car)
{
if (stack_full(stack) != 1)
{
stack->top++;
stack->park[stack->top] = car;
}
else
{
printf(“停车场已满,入场失败!\n”);
}
}
CAR pop(SeqStack * stack)
{
return stack->park[stack->top–];
}
CAR getcar(SeqStack * stack,int n)
{
return stack->park[n];
}
void queueinit(CarQueue * queue)
{
queue->head = NULL;
queue->rear = NULL;
}
void create_carnode(CarLink * carnode)
{
*carnode = (CarLink)malloc(sizeof(CarNode));
is_malloc_ok(*carnode);
}
void is_malloc_ok(CarLink carnode)
{
if (carnode == NULL)
{
printf(“malloc error!\n”);
exit(0);
}
}
void enqueue(CarQueue ** queue, CarLink carnode,CAR car)
{
if (queue_empty(*queue) == 1)
{
carnode->next = NULL;
(*queue)->head = carnode;
}
else
{
carnode->next = NULL;
(*queue)->rear->next = carnode;
}
carnode->data = car;
(*queue)->rear = carnode;
}
CAR dequeue(CarQueue ** queue)
{
CarLink p;
CAR car;
p = (*queue)->head;
car = p->data;
(*queue)->head = (*queue)->head->next;
free(p);
if ((*queue)->head == NULL)
{
(*queue)->rear = NULL;
}
return car;
}
int queue_empty(CarQueue * queue)
{
if (queue->head ==NULL && queue->rear==NULL)
{
return 1;
}
else
{
return 0;
}
}
void choice(SeqStack *stack, CarQueue * queue)
{
int selection;
while (1)
{
printf(“
|————–欢迎使用煜D停车场管理系统—————–|\n”);
printf(“
| 该系统自动读取保存记录 |\n”);
printf(“
| (1)车辆进入 |\n”);
printf(“
| (2)车辆离开 |\n”);
printf(“
| (3)查看停车场状况 |\n”);
printf(“
| (0)离开系统 |\n”);
printf(“
| |\n”);
printf(“
| 提示:本停车场共有%d个车位,车位停满后将放到过道上 |\n”,MAXNUM);
printf(“
| 本停车场计费标准为%lf元/小时,过道上的车不计费|\n”,PRIZE);
printf(“
输入0~3进行操作: \n”);
scanf(“%d”, &selection);
fflush(stdin);
switch (selection)
{
case 1:inpark(stack, queue); getchar();
break;
case 2:outpark(stack, queue); getchar();
break;
case 3:showall(stack, queue); getchar(); getchar();
break;
case 0:
printf(“欢迎再次使用,再见!\n”);
save(stack, queue);
exit(0);
}
system(“cls”);
}
}
void printdata(struct tm gm_data)
{
printf(“%d/%d %02d:%02d:%02d\n”, gm_data.tm_mon, gm_data.tm_mday, gm_data.tm_hour+8, gm_data.tm_min,
gm_data.tm_sec);
}
void showpark(SeqStack * stack)
{
int i;
printf(“车位使用状况:\n”);
printf(“——————————\n”);
if (stack_empty(stack) == 1)
{
printf(“停车场中没有车辆!\n”);
}
else
{
printf(“位置\t车牌号\t进场时间\n”);
for (i = 0; i <= stack->top; i++)
{
printf(“%d\t%s\t”, stack->park[i].position, stack->park[i].num);
printdata(stack->park[i].intime);
}
printf(“\t\t\t共%d辆”, stack->top + 1);
if (stack->top + 1 == MAXNUM)
{
printf(“已满!\n”);
}
else
{
printf(“还可停放%d\n”, MAXNUM – 1 – stack->top);
}
}
printf(“\n”);
}
void showaisle(CarQueue * queue)
{
printf(“过道使用情况:\n”);
printf(“——————————\n”);
if (queue_empty(queue) != 1)
{
CarNode *p;
p = queue->head;
printf(“车牌号\t进入时间\n”);
while (p != NULL)
{
printf(“%s\t”, p->data.num);
printdata(p->data.intime);
p = p->next;
}
}
else
{
printf(“过道上没有车在等待!\n”);
}
}
void showall(SeqStack *stack, CarQueue * queue)
{
showpark(stack);
showaisle(queue);
}
void inpark(SeqStack * stack, CarQueue * queue)
{
CAR car;
struct tm *gm_date;
time_t seconds;
time(&seconds);
gm_date = gmtime(&seconds);
printf(“请输入车牌号:\n”);
scanf(“%s”, &car.num);
getchar();
car.intime = *gm_date;
if (stack_full(stack) != 1 && queue_empty(queue) == 1)
{
car.position = stack->top + 2;
push(stack, car);
showpark(stack);
}
else if (stack_full(stack) == 1 || queue_empty(queue) != 1)
{
CarLink carnode = NULL;
create_carnode(&carnode);
printf(“停车场已满,车停在过道上!\n”);
car.position = MAXNUM;
enqueue(&queue, carnode, car);
showall(stack, queue);
}
}
void outpark(SeqStack * stack, CarQueue * queue)
{
struct tm *gm_date;
time_t seconds;
SeqStack p;
stackinit(&p);
int i, pos;
CAR car;
if (stack_empty(stack) == 1)
{
printf(“停车场为空,没有车需要离开!\n”);
}
else
{
showpark(stack);
printf(“请输入要离开车的车位号:\n”);
scanf(“%d”, &pos);
if (pos >0 && pos <= stack->top + 1)
{
for (i = stack->top+1; i > pos; i–)
{
car = pop(stack);
car.position = car.position – 1;
push(&p, car);
}
car = pop(stack);
time(&seconds);
gm_date = gmtime(&seconds);
car.outtime = *gm_date;
car.length = mktime(&car.outtime) – mktime(&car.intime);
car.expense = (car.length / 3600 + 1)*PRIZE;
printpay(car);
while (stack_empty(&p) != 1)
{
car = pop(&p);
push(stack, car);
}
while (stack_full(stack) != 1 && queue_empty(queue) != 1)
{
car = dequeue(&queue);
time(&seconds);
gm_date = gmtime(&seconds);
car.intime = *gm_date;
push(stack, car);
}
}
else
{
printf(“车号错误,停车场中无该车或者该车在过道上!\n”);
}
}
getchar();
}
void printpay(CAR car)
{
printf(“停车账单显示如下:\n”);
printf(“——————————\n”);
printf(“车牌:%s\n”, car.num);
printf(“停车位置:%d\n”, car.position);
printf(“进入时间:”);
printdata(car.intime);
printf(“离开时间:”);
printdata(car.outtime);
printf(“停车时间(秒):%d\n”, car.length);
printf(“停车费用(元):%2lf\n”, car.expense);
printf(“——————————\n\n”);
}