停车场管理
问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1) 建立三个数据结构分别是:停放队列、让路栈、等候队列。
(2) 输入数据模拟管理过程,数据(入或出,车号)。
初始停放0辆车,最多能停放5辆车,后面的车如果需要停放,需要等待,如果有空位,会自动停车,会显示停车时间
界面有待美化
主要代码如下:
head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define FALSE 0
#define TRUE 1
#define MAX 5//最大停车数
//链队列
typedef struct node
{
char id[100];
char sj[100];
long second;
struct node *next;
}Node;
typedef struct queue
{
Node *f;
Node *r;
}Q;
//链式栈
typedef struct zhan
{
//int data[MAX];
char id[100];
char sj[100];
long second;
struct zhan * next;
}Znode;
typedef struct lianshizhan
{
Znode* top;
}LZ;
//创建链式栈
LZ*create();
//判断栈是否空
int zhanshifoukong(LZ *s);
//入栈
int ruzhan(LZ * s,char *id);
//出栈
int popz_z1(LZ *z,LZ *z1);
//出栈
int popz(LZ *z);
int popz1_z(LZ *z,LZ*z1);
//显示栈内情况
void displayzhan(LZ *s);
//队列创建
Q*create1();
//队列是否空
int qempty(Q *q);
//入队
int enterq(Q*q);
//出队列
int deq(Q*q,LZ *s);
//查询
int find(LZ *z,LZ*z1);
//菜单
void caidan();
#endif
head.c
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<string.h>
#include"head.h"
int n=0; //全局变量计数停车场内车数量
int n2=0; //全局变量计数等待的车辆
//链式栈的创建
LZ*create()
{
LZ* s=(LZ*)malloc(sizeof(LZ));
if(s==NULL)
{
return NULL;
}
//zhikongzhan
s->top=NULL;
return s;
}
//判断栈是否空
int zhanshifoukong(LZ *s)
{
if(s==NULL)
{
printf("不是栈\n");
return FALSE;
}
return s->top==NULL;
}
//入栈
int ruzhan(LZ * s,char *id)
{
time_t timep;
time(&timep);
if(n>MAX-1)
{
printf("车库满\n");
return FALSE;
}
Znode* node=(Znode*)malloc(sizeof(Znode));
if(node==NULL)
return FALSE;
strcpy(node->id,id);
node->second=time(&timep);
strcpy(node->sj,ctime(&timep));
node->next=s->top;
s->top=node;
n++;
n2--;
return TRUE;
}
//出栈
int popz_z1(LZ *z,LZ *z1)
{
if(zhanshifoukong(z))
{
printf("没有车可以出了!\n");
return FALSE;
}
Znode* node=(Znode*)malloc(sizeof(Znode));
if(node==NULL)
return FALSE;
strcpy(node->id,z->top->id);
strcpy(node->sj,z->top->sj);
node->second=z->top->second;
node->next=z1->top;
z1->top=node;
z->top=z->top->next;
return TRUE;
}
//出栈
int popz(LZ *z)
{
Znode *p=z->top;
z->top=z->top->next;
free(p);
n--;
return TRUE;
}
//出栈
int popz1_z(LZ *z,LZ*z1)
{
while(z1->top!=NULL)
{
Znode* node=(Znode*)malloc(sizeof(Znode));
if(node==NULL)
return FALSE;
strcpy(node->id,z1->top->id);
strcpy(node->sj,z1->top->sj);
node->second=z1->top->second;
node->next=z->top;
z->top=node;
z1->top=z1->top->next;
}
return TRUE;
}
//遍历栈并显示
void displayzhan(LZ *s)
{
time_t timep;
time(&timep);
long second=time(&timep);
if(n==0)
{
printf("车库没有车\n");
return ;
}
Znode *p=s->top;
printf("车库中车情况,有车%d辆,在等车的有%d\n",n,n2);
printf("车号 已停车时间\n");
while(s->top!=NULL)
{
printf("%s %ld\n",s->top->id,second-s->top->second);
s->top=s->top->next;
}
printf("\n");
s->top=p;
}
//创建爱你初始化队列
Q*create1()
{
Q*q=(Q*)malloc(sizeof(Q));
if(q==NULL)
{
printf("q内存没分配成功\n");
return NULL;
}
q->f=NULL;
q->r=NULL;
return q;
}
//是否空队列
int qempty(Q *q)
{
if(q==NULL)
{
printf("没有链队\n");
return FALSE;
}
return q->f==NULL;
}
//入进入等待列
int enterq(Q*q)
{
time_t timep;
time(&timep);
if(q==NULL)
{
printf("没有链队\n");
return FALSE;
}
Node * node = (Node*)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
printf("内存分配失败\n");
return FALSE;
}
printf("车牌号\n");
scanf("%s",&node->id);
n2++;
node->next = NULL;
if (q->f == NULL)
{
q->f = node;
q->r = node;
}
else
{
q->r->next = node;
q->r = node;
}
return TRUE;
}
//出等待链队
int deq(Q*q,LZ *s)
{
char id[100];
if(q==NULL)
{
printf("没有链队\n");
return FALSE;
}
if(qempty(q))
{
printf("等待队已空\n");
return FALSE;
}
Node *p=q->f;
strcpy(id,p->id);////xuyao zou
if(n>MAX-1)
{
printf("停车失败,车库满\n");
return FALSE;
}
ruzhan(s,id);
q->f=p->next;
free(p);
if(q->f==NULL)
{
q->r=NULL;
}
return TRUE;
}
//查找要走的车
int find(LZ *z,LZ*z1)
{
char id[100];
if(n==0)
{
printf("车库中没有车\n");
return FALSE;
}
displayzhan(z);
printf("输入要走的车牌号\n");
scanf("%s",&id);
Znode *p =z->top;
int leap=0; //标识符
while(z->top!=NULL)
{
if(strcmp((z->top->id),id)==0)
{
leap=1;
}
z->top=z->top->next;
}
z->top=p;
if(leap==0)
{
printf("没有此车\n");
return FALSE;
}
p=z->top->next;
while(z->top!=NULL)
{
if(strcmp((z->top->id),id)==0)
{
popz(z);
break;
}
popz_z1(z,z1);
}
popz1_z(z,z1);
return TRUE;
}
//菜单
void caidan()
{
printf("\t欢迎来本车库,本车库最大容量%d\n",MAX);
printf("\t\t1---停车\n");
printf("\t\t2---离开\n");
printf("\t\t3---查看车库\n");
printf("\t\t4---退出\n");
}
main.c
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<stdlib.h>
#include"head.h"
extern n; //全局变量停车场内的车数量
extern n2; //全局变量等待的车数量
int main()
{
LZ * z,*z1;
Q * q;
z=create(); //定义两个链式栈一个队列并初始化
z1=create();
q=create1();
int i;
caidan(); //菜单
while(1)
{
printf("你想使用的功能:");
scanf("%d",&i);
if(i==1) //停车
{
enterq(q);
if(deq(q,z))
printf("停车成功\n");
}
else if(i==2) //离开
{
find(z,z1); //先查找再判定离开
}
else if(i==3) //查看车库
{
displayzhan(z);
}
else //否则退出
{
printf("欢迎使用,谢谢\n");
break;
}
while(n<5&&qempty(q)!=TRUE) //当停车场没有停满,等待有车自动停入
if(deq(q,z))
printf("停车成功\n");
}
return 0;
}
界面及功能有待改进或优化及增加,谢谢