|
楼主 |
发表于 2021-1-3 00:12:44
|
显示全部楼层
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef int ElemType;
#define MaxSize 100
#define QNODE struct QNode
typedef struct Node //车辆信息
{
char AL;
int NO;//等待名次
int time;//停车时间
}Node;
typedef struct Stack //栈定义
{
struct Node data[MaxSize];
int top;
int num;
}SqStack;
QNODE //队列节点
{
struct Node data;
QNODE *next;
};
typedef struct linkqueue //队列结构体定义
{
QNODE *front,*rear;
int num;
}LinkQueue;
SqStack *Init_SeqStack() //置空栈
{
SqStack *s;
s=(SqStack*)malloc(sizeof(SqStack));
s->top=-1;
s->num=0;
return s;
}
LinkQueue *Init_LQueue() //创建空队列
{
LinkQueue *q;
QNODE *p;
q=(LinkQueue*)malloc(sizeof(LinkQueue));
p=(QNODE*)malloc(sizeof(QNODE));
p->next=NULL;
q->front=q->rear=p;
q->num=0;
return q;
}
int ISEmpty_SeqStack(SqStack *s) //判断栈是否为空,栈为空返回1
{
if(s->top ==-1)
return 1;
else
return 0;
}
int ISFULL_SeqStack(SqStack *s,int n) //判断栈是否已满,若栈满返回1
{
if(s->top==n-1)
return 1;
else
return 0;
}
int ISEmpty_LQueue(LinkQueue *q) //判断队列是否为空,队列为空返回1
{
if(q->front==q->rear)
return 1;
else
return 0;
}
void IN_Lqueue( LinkQueue *q,struct Node s) //入队
{
QNODE *p;
p=(QNODE*)malloc(sizeof(QNODE));
p->data=s;
q->num++;
p->next=NULL;
q->rear->next =p;
q->rear =p;
}
void Push_SeqStack(SqStack *p,struct Node s) //入栈
{
p->top ++;
p->data[p->top]=s;
p->num++;
}
int POP_SeqStack(SqStack *s,struct Node car)//出栈
{
SqStack *p;
int t;
p=Init_SeqStack();
while(s->data[s->top].NO !=car.NO)//找到车牌号为P.NO的车,
{
Push_SeqStack(p,s->data[s->top]);
s->top--;
s->num--;
}
t=car.time-s->data[s->top].time;
s->top--;
s->num--;
while(ISEmpty_SeqStack(p)==0)
{
Push_SeqStack(s,p->data[p->top]);
p->top--;
p->num--;
}
return t;
}
struct Node Out_LQueue(LinkQueue *q) //出队
{
QNODE *p;
p=q->front->next;
q->front->next=p->next;
q->num --;
if( q->front->next==NULL)
q->rear=q->front;
return p->data;
free(p);
}
int main()
{
SqStack *parkstack; //parkstack停车场栈
LinkQueue *parkqueue; //parkqueue等待位置队列
struct Node car;
int n,a=0,t; //n停车场栈最大容量
int menu=0;
int change=1;
int x;
float f; //f收费
parkstack=Init_SeqStack();
parkqueue=Init_LQueue(); //初始界面
printf("***************停车场信息查询***************\n");
printf("请输入停车场最大容量:");
scanf("%d",&n);
printf("\n请输入停车每分钟收取费用:");
scanf("%f",&f);
printf("主菜单\n");
printf("1.停车\n");
printf("2.取车\n");
printf("3.查看停车场情况\n");
printf("4.查看等待队列情况\n");
printf("请选择:");
while(change==1){
scanf("%d",&menu);
switch(menu){
case 1:
menu=1;
for(x=1;x<=n;x++){
printf("\n请输入停入的车辆信息\n");
scanf("%c,%d,%d",&car.AL,&car.NO,&car.time);
if(ISFULL_SeqStack(parkstack,n)==1) //栈满的情况
{
IN_Lqueue(parkqueue,car); //进入队列等待
printf("这辆车在停车场外第%d个位置等待中...\n",parkqueue->num);
}
if(ISFULL_SeqStack(parkstack,n)==0){
Push_SeqStack(parkstack,car); //入栈
printf("这辆车已停入停车场,在停车场内第%d个位置\n",parkstack->num);
}
}
break;
case 2:
menu=2;
printf("请输入你要取的车:\n");
scanf("%c,%d,%d",&car.AL,&car.NO,&car.time);
t=POP_SeqStack(parkstack,car);//出栈
printf("这辆车停留时间为%d,收费为%f。\n",t,f*t);
printf("\n");
break;
case 3:
menu=3;
printf("\n当前的车辆有:");
printf("停车场的车数为%d\n",parkstack->num);
printf("\n");
break;
case 4:
menu=4;
printf("当前的等待队列有:");
printf("候车场的车数为%d\n",parkqueue->num);
printf("\n");
break;
default:
break;
}
}
return menu;
}
|
|