70,020
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACKSIZE 2
#define LICENCESIZE 10
#define CHARGE 0.2 //每分钟的停车费
#define TRUE 1
#define FALSE 0
typedef struct Time
{
int hour;
int minute;
}Time;
typedef struct Car
{
int leaved;
char licence[LICENCESIZE];
Time arrive;
Time leave;
}Car;
typedef struct Stack
{
Car elem[STACKSIZE];
int top;
}Stack;
typedef struct QueueNode
{
Car data;
struct QueueNode *next;
}QueueNode;
typedef struct
{
struct QueueNode *front;
struct QueueNode *rear;
}Queue;
void SetTime(Time *t)
{
scanf("%d:%d%*c", &t->hour, &t->minute);
}
void ShowTime(Time *t)
{
printf("%d:%d\n", t->hour, t->minute);
}
double Fee(Time t1, Time t2) //t1为进停车场的时间,t2为出停车场的时间
{
double t;
if (t1.hour == -1)
return 0;
else
t = (t2.hour - t1.hour) * 60 + (t2.minute - t1.minute);
return (t * CHARGE);
}
void InitStack(Stack *S)
{
S->top = -1;
}
int EmptyStack(Stack *S)
{
if (S->top == -1)
return (TRUE);
else
return (FALSE);
}
int FullStack(Stack *S)
{
if (S->top == STACKSIZE - 1)
return (TRUE);
else
return (FALSE);
}
int Push(Stack *S, Car c)
{
if (FullStack(S))
return (FALSE);
else
{
S->top++;
S->elem[S->top] = c;
return (TRUE);
}
}
int Pop(Stack *S, Car *c)
{
if (EmptyStack(S))
return (FALSE);
else
{
(*c) = S->elem[S->top];
S->top--;
return (TRUE);
}
}
int InitQueue(Queue *Q)
{
Q->front = (QueueNode *)malloc(sizeof(QueueNode));
if (Q->front == NULL)
return (FALSE);
else
{
Q->rear = Q->front;
Q->front->next = NULL;
return (TRUE);
}
}
int EnterQueue(Queue *Q, Car c)
{
Q->rear->next = (QueueNode *)malloc(sizeof(QueueNode));
if (Q->rear->next == NULL)
return (FALSE);
else
{
Q->rear = Q->rear->next;
Q->rear->data = c;
Q->rear->next = NULL;
return (TRUE);
}
}
int EmptyQueue(Queue *Q)
{
if (Q->front == Q->rear)
return (TRUE);
else
return (FALSE);
}
int DeleteQueue(Queue *Q, Car *c)
{
QueueNode *t;
if (EmptyQueue(Q))
return (FALSE);
else
{
t = Q->front->next;
if (Q->rear == t)
Q->rear = Q->front;
(*c) = t->data;
Q->front->next = t->next;
free(t);
return (TRUE);
}
}
void CarIn(Stack *park, Queue *shortcut)
{
Car c;
Time t;
printf("请输入当前时间:");
SetTime(&t);
printf("请输入车牌号:");
gets(c.licence);
c.arrive.hour = -1;
if (FullStack(park))
{
printf("停车场已满,新来的车在过道等待。\n");
EnterQueue(shortcut, c);
}
else
{
printf("车已在停车场停车,现在开始计时。\n");
c.arrive = t;
Push(park, c);
}
printf("\n");
}
void CarOut(Stack *park, Stack *temp, Queue *shortcut)
{
char licence[LICENCESIZE];
Car c;
Time t;
printf("请输入当前时间:");
SetTime(&t);
printf("请输入车牌号:");
gets(licence);
Pop(park, &c);
while (strcmp(c.licence, licence) != 0)
{
Push(temp, c);
Pop(park, &c);
}
c.leave = t;
printf("该车应付:%.2lf元\n", Fee(c.arrive, c.leave));
while (!EmptyStack(temp))
{
Pop(temp, &c);
Push(park, c);
}
if (!EmptyQueue(shortcut))
{
DeleteQueue(shortcut, &c);
c.arrive = t;
Push(park, c);
}
printf("\n");
}
void LookNow(Stack *park, Stack *temp, Queue *shortcut)
{
Car c;
QueueNode *p;
while (!EmptyStack(park))
{
Pop(park, &c);
Push(temp, c);
}
while (!EmptyStack(temp))
{
Pop(temp, &c);
printf("%-10s:停车\n", c.licence);
Push(park, c);
}
p = shortcut->front->next;
while (p != NULL)
{
printf("%-10s:等待\n", p->data.licence);
p = p->next;
}
printf("\n");
}
int main()
{
Stack park, temp;
Queue shortcut;
int t = 1;
InitStack(&park);
InitStack(&temp);
InitQueue(&shortcut);
while (t)
{
printf("请选择,1进车,2出车,3查看现状,0退出:");
scanf("%d%*c", &t);
switch (t)
{
case 1: CarIn(&park, &shortcut); break;
case 2: CarOut(&park, &temp, &shortcut); break;
case 3: LookNow(&park, &temp, &shortcut); break;
case 0: break;
}
}
return 0;
}