项目要求:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后依次进入。汽车离开时按停放时间收费。
基本功能要求:
1)建立三个数据结构分别是:停放队列,让路栈,等候队列
2)输入数据模拟管理过程,数据(入或出,车号)。
#ifndef __PARK_H__
#define __PARK_H__
#define FALSE 0
#define TRUE 1
#define SIZE 20
#define CAR_SIZE 10
//创建三个结构体 停放栈 让路栈 等候队列
//车辆信息
typedef struct _car
{
char ID[SIZE]; //存放车牌号
time_t intime; //进栈时间
time_t outtime; //出栈时间
float money; //停车费用 (1秒0.1元)
}car;
//停放栈
typedef struct _park
{
car num1[CAR_SIZE]; //存放车辆信息
int top1; //栈头元素下标
}park;
//让路栈
typedef struct _wait
{
car num2[SIZE];
int top2;
}wait;
//等候队列
typedef struct _queue
{
car data;
struct _queue *next;
}Queue;
typedef struct _linkqueue
{
Queue *front; //对头指针
Queue *rear; //队尾指针
}LinkQueue;
//停车场的状态
int func(park *s, LinkQueue *q);
// 置空栈
int InitStack (park *s);
//判断是否空栈
int EmptyStack(park *s);
//判断是否满栈
int FullStack(park *s);
//置空让路栈
int initstack(wait *s);
//判断让路栈是否满栈
int fullstack(wait *s);
//判断让路栈是否空栈
int emptystack(wait *s);
//创建队列
LinkQueue *Creat_Queue();
//判断队空
int EmptyQueue(LinkQueue *q);
//进队
int InQueue(LinkQueue *q,car *x);
//出队进停车栈
int OutQueue(LinkQueue *q, car *x, park *s);
//有车进停车栈
int Push(park *s, car *x, LinkQueue *p);
//有队列的车进停车栈
//int queuepush(park *s , LinkQueue *q);
//有车出栈
int popstack(park *s, car *x);
//让路栈出栈
int pop1stack(wait *p ,park *s);
//让路栈进栈
int push1stack(wait *p ,park *s);
void menu(park *s, LinkQueue *q);
//查看停车场情况
int display(park *s);
//参看等候队列情况
int voidplay(LinkQueue *q);
//出停放栈
int pop(park *s,car *x, wait *p ,LinkQueue *q);
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "Park.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int func(park *s, LinkQueue *q)
{
int i = 0;
if(s == NULL)
return FALSE;
if(q == NULL)
return FALSE;
Queue *p = q->front;
while(p)
{
i++;
p = p->next;
}
printf("\t停车场共有6个车位,目前停车场共有%d辆车,等候区共有%d辆\n",s->top1+1, i);
return TRUE;
}
void menu(park *s, LinkQueue *q)
{
printf("\t******************停车场目前的状态*****************~\n");
func(s, q);
printf("\t****************************************************\n");
printf("\t~ 1 >>>>>>>>停车 ~\n");
printf("\t~ 2 >>>>>>>>离开 ~\n");
printf("\t~ 3 >>>>>>>>查看停车场情况 ~\n");
printf("\t~ 4 >>>>>>>>查看等候车道情况 ~\n");
printf("\t~ 5 >>>>>>>>退出 ~\n");
printf("\t~ ~\n");
printf("\t~ 作者:倪鹏程 ~\n");
printf("\t***************************************************~\n");
printf("\t请按对应数字提示完成对应操作 ~\n");
printf("\t 收费标准:0.1元/秒\n");
}
//查看停车场情况
int display(park *s)
{
if(s == NULL)
return FALSE;
int i = 0;
if(s->top1 == -1)
{
printf("停车场中没有车\n");
}
time_t curtime;
time(&curtime);
while(i <= s->top1)
{
int len = curtime - s->num1[i].intime;
s->num1[s->top1].money = 0.1 * len;
printf("%d号车位 车牌号为%s 停车时间%s",
i+1,s->num1[i].ID,ctime(&s->num1[i].intime));
printf("停车时长%d秒 需支付%.1f元\n", len,s->num1[s->top1].money);
printf("*****************************************\n");
i++;
}
return TRUE;
}
//参看等候队列情况
int voidplay(LinkQueue *q)
{
int i = 1;
Queue *p = q->front;
if(q == NULL)
{
return FALSE;
}
if(EmptyQueue(q))
{
printf("等候队列中没有车辆信息\n");
return FALSE;
}
time_t pretime;
time(&pretime);
while(p != NULL)
{
int len = pretime - p->data.intime;
printf("———————————————————————————————————————————————————\n");
printf("车牌号%s 停在了等候车道的%d号车位\n", p->data.ID, i++);
printf("停车时间%s\n",ctime(&p->data.intime));
printf("已经等候了 %d秒\n",len);
printf("———————————————————————————————————————————————————\n");
p = p->next;
}
return 0;
}
//置空停放栈
int InitStack(park *s)
{
if(s == NULL)
return FALSE;
s->top1 = -1;
return TRUE;
}
//判断是否空栈
int EmptyStack(park *s)
{
if(s == NULL)
return FALSE;
return s->top1 == -1;
}
//判断是否满栈
int FullStack(park *s)
{
if(s == NULL)
return FALSE;
return s->top1 == (CAR_SIZE - 1);
}
//置空让路栈
int initstack(wait *s)
{
if(s == NULL)
return FALSE;
s->top2 = -1;
return TRUE;
}
//判断让路栈是否满栈
int fullstack(wait *s)
{
if(s == NULL)
return FALSE;
return s->top2 == SIZE - 1;
}
//判断让路栈是否空栈
int emptystack(wait *s)
{
if(s == NULL)
return FALSE;
return s->top2 == -1;
}
//创建队列
LinkQueue *Creat_Queue()
{
LinkQueue *q = (LinkQueue*)malloc(sizeof(LinkQueue));
if(q == NULL)
return FALSE;
//置空队
q->front = NULL;
q->rear = NULL;
return q;
}
//判断队空
int EmptyQueue(LinkQueue *q)
{
if(q == NULL)
return FALSE;
return q->front == NULL;
}
//进队
int InQueue(LinkQueue *q ,car *x)
{
if(q == NULL)
return FALSE;
Queue *node =(Queue *)malloc(sizeof(Queue));
if(node == NULL)
return FALSE;
time(&x->intime);
node->next = NULL;
node->data = *x;
if(q->front == NULL)
{
q->front = node;
q->rear = node;
printf("您的车已进入等候车道\n");
}
else
{
q->rear->next = node;
q->rear = node;
printf("您的车已进入等候车道\n");
}
return TRUE;
}
//有车进停车栈
int Push(park *s, car *x, LinkQueue *p)
{
if(s == NULL)
return FALSE;
char a;
if(FullStack(s))
{
printf("停车位已满,是否先转到等候队列\n");
printf("Y/y or N/n\n");
printf("请输入:");
getchar();
scanf("%c",&a);
if(a == 'Y' || a == 'y')
{
InQueue(p, x);
return TRUE;
}
else if(a == 'N' || a == 'n')
{
printf("您已放弃本次停车。\n");
return TRUE;
}
}
else
{
s->top1++;
//x->intime = time(NULL);
s->num1[s->top1] = *x;
time(&s->num1[s->top1].intime);
printf("车牌号:%s 成功停入第%d个停车位。\n", s->num1[s->top1].ID,s->top1+1);
printf("当前时间为 %s\n",ctime(&s->num1[s->top1].intime));
}
return TRUE;
}
//出队进停车栈
int OutQueue(LinkQueue *q, car *x,park *s)
{
if(q == NULL)
return FALSE;
if(EmptyQueue(q))
return FALSE;
if(s == NULL)
return FALSE;
Queue *p = q->front;
*x = p->data;
q->front = p->next;
free(p);
p = NULL;
if(q->front == NULL)
{
q->rear = NULL;
}
Push(s, x, q);
printf("等候车道的车已成功进入停入到停车栈中!\n");
return TRUE;
}
//出栈
int popstack(park *s, car *x)
{
if(s == NULL)
return FALSE;
if(EmptyStack(s))
{
printf("停车场中没有车!\n");
return FALSE;
}
time(&s->num1[s->top1].outtime);
*x = s->num1[s->top1];
printf("车牌号为%s的车,在%s成功从第%d个停车位离开停车场!\n",
s->num1[s->top1].ID,ctime(&s->num1[s->top1].outtime),s->top1+1);
printf("欢迎下次再来我的停车场,祝您一路顺风!\n");
s->top1--;
return TRUE;
}
//让路栈进栈
int push1stack(wait *p ,park *s)
{
if(p == NULL)
return FALSE;
if(fullstack(p))
return FALSE;
if(EmptyStack(s))
return FALSE;
p->top2++;
p->num2[p->top2] = s->num1[s->top1];
s->top1--;
return TRUE;
}
//让路栈出栈
int pop1stack(wait *p ,park *s)
{
if(p == NULL)
return FALSE;
if(emptystack(p))
return FALSE;
s->top1++;
s->num1[s->top1] = p->num2[p->top2];
p->top2--;
return TRUE;
}
//出停放栈
int pop(park *s,car *x, wait *p ,LinkQueue *q)
{
if(s == NULL)
return FALSE;
if(p == NULL)
return FALSE;
if(EmptyStack(s))
{
printf("停车场中没有车\n");
return FALSE;
}
if(FullStack(s))
{
int flag = 0;
char a[20];
int i = s->top1;
printf("请输入你要离开的车牌号:");
scanf("%s", a);
printf("\n");
while(s->top1 >= 0)
{
if(strcmp(s->num1[s->top1].ID, a) == 0)
{
popstack(s , x);
flag = 1;
break;
}
else
{
push1stack(p , s);
}
}
if(flag != 1)
{
printf("停车场中没有此车牌号车的信息!\n");
}
if(flag == 1)
{
printf("——————————————————————————————\n");
if(EmptyQueue(q))
{
return;
}
else
{
OutQueue(q, x, s);
}
printf("——————————————————————————————\n");
while(s->top1 < i) //把摆放在让路栈的车回到停放栈中
{
pop1stack(p, s);
}
}
}
else
{
popstack(s , x);
}
return TRUE;
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "Park.h"
int main()
{
int num; //用于case
int num2; //用车输入要离开的车牌号
car *x = (car *)malloc(sizeof(car));
if(x == NULL)
return FALSE;
park *p = (park *)malloc(sizeof(park));
if(p == NULL)
return FALSE;
wait *w = (wait*)malloc(sizeof(wait));
if(w == NULL)
return FALSE;
LinkQueue *q = Creat_Queue(); //创建等候队列
if(q == NULL)
return FALSE;
InitStack (p); //置空停车栈
initstack (w); //置空让路栈
while(1)
{
system("clear");
menu(p, q);
printf("请按功能输入数字:");
scanf("%d", &num);
switch(num)
{
case 1:
{
system("clear");
printf("请输出您的车牌号:");
scanf("%s", x->ID);
printf("\n");
Push(p, x, q);
getchar();
printf("请按回车键返回主菜单!");
getchar();
break;
}
case 2:
{
system("clear");
pop(p, x, w ,q);
getchar();
printf("请按回车键返回主菜单!");
getchar();
break;
}
case 3:
{
system("clear");
display(p);
getchar();
printf("请按回车键返回主菜单!");
getchar();
break;
}
case 4:
{
system("clear");
voidplay(q);
getchar();
printf("请按回车键返回主菜单!");
getchar();
break;
}
case 5:
{
system("clear");
exit(0);
break;
}
default :
{
system("clear");
getchar();
printf("请严格按照数字选择对应操作\n");
printf("请按回车键返回主菜单!");
getchar();
break;
}
}
}
return 0;
}