//停车场管理系统
#include <stdio.h>
#include <stdlib.h>
#define OK 1
typedef struct{
char car_action; //A-车辆到达;D-车辆离开;E-结束标志
int car_no; // 车辆号
int time_point; //车辆离开或到达的时间
int cost; //停车费
}CarInfo;
typedef struct{
int top;
CarInfo *elements;
int MaxSize;
}Stack;
void InitStack(Stack *b,int n)
{
b->MaxSize=n;
b->elements=(CarInfo *)malloc(b->MaxSize*sizeof(CarInfo));
if(b->elements==NULL)
printf("申请空间失败...\n");
b->top=-1;
}
int IsFull(Stack *b)
{
if(b->top==(b->MaxSize-1)) return(1);
else return(0);
}
int IsEmpty(Stack *b)
{
if(b->top==-1) return(1);
else return(0);
}
int Push(Stack *b,CarInfo *t)
{
if(IsFull(b)) return(0);
b->top++;
b->elements[b->top].car_action=t->car_action;
b->elements[b->top].car_no=t->car_no;
b->elements[b->top].time_point=t->time_point;
b->elements[b->top].cost=t->cost;
return OK;
}
CarInfo Pop(Stack *b)
{
if(IsEmpty(b)){printf("栈为空\n");}
else
{
return(b->elements[b->top--]);
}
}
int SearchInStack(Stack *a,Stack *b,CarInfo *current_car,int park_cost)
{ // 在停车场中查找是否有离开车辆车号
int find=0;
int cost;
CarInfo temp_info;
while(!IsEmpty(a)&&!find)
{
temp_info=Pop(a);
if(temp_info.car_no==current_car->car_no)
{
find=1;
cost=(current_car->time_point-temp_info.time_point)*park_cost;
temp_info.cost+=cost;
cost=temp_info.cost;
}
else Push(b,&temp_info);
}
while(!IsEmpty(b))
{
temp_info=Pop(b);
Push(a,&temp_info);
}
if(find==0) return(0);
else return(cost);
}
typedef struct QNode{
CarInfo data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
int num;
}Queue;
int InitQueue(Queue *Q)
{
Q->front=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front) return(0);
Q->rear=Q->front;Q->front->next=NULL;
Q->num=0;
return(1);
}
int EnQueue(Queue *Q,CarInfo *current_car)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) return(0);
p->data.car_action=current_car->car_action;
p->data.car_no=current_car->car_no;
p->data.cost=current_car->cost;
p->data.time_point=current_car->time_point;
p->next=NULL;Q->rear->next=p;Q->rear=p;
Q->num++;return(1);
}
CarInfo DeQueue(Queue *Q)
{
CarInfo temp_car;
QueuePtr p;
if(Q->front==Q->rear) {printf("队列空\n");}
else
{
p=Q->front->next;
temp_car.car_action=p->data.car_action;
temp_car.car_no=p->data.car_no;
temp_car.cost=p->data.cost;
temp_car.time_point=p->data.time_point;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);Q->num--;
return(temp_car);
}
}
int SearchInQueue(Queue *Q,CarInfo *current_car,int road_cost)
{// 在停车道中查找要离开的车
int i=0,num;
int find=0;
int cost;
CarInfo temp_car;
num=Q->num;
if (!num) {printf("队列为空");return(0);}
while(i<num)
{
temp_car=DeQueue(Q);
if(temp_car.car_no==current_car->car_no)
{
find=1;
cost=temp_car.cost+(current_car->time_point-temp_car.time_point)*road_cost;
}
else
{
EnQueue(Q,&temp_car);
}
i++;
}
if(find) return(cost);
else return(0);
}
void CarArrive(Stack *Park,Queue *Road,CarInfo car_info)
{
if(IsFull(Park))
{
EnQueue(Road,&car_info);
printf("车号为%d的车辆进入便道.\n",car_info.car_no);
}
else
{
Push(Park,&car_info);
printf("车号为%d的车辆进入停车场.\n",car_info.car_no);
}
}
void CarLeave(Stack *a,Stack *b,Queue *Road,CarInfo car_info,int park_cost,int road_cost)
{
CarInfo temp_car;
int temp_cost;
if(temp_cost=SearchInStack(a,b,&car_info,park_cost))
{
printf("车号为%d的车辆离开停车场,停车费为:%d.\n",car_info.car_no,temp_cost);
if(Road->num>0){
temp_car=DeQueue(Road);
temp_car.cost+=(car_info.time_point-temp_car.time_point)*road_cost;
temp_car.time_point=car_info.time_point;
Push(a,&temp_car);
printf("车号为%d的车辆从便道进入停车场.\n",temp_car.car_no);
}
}
else
if(temp_cost=SearchInQueue(Road,&car_info,road_cost))
{
printf("车号为%d的车辆离开便道,停车费为:%d\n",car_info.car_no,temp_cost);
}
else
{
printf("对不起,您输入的信息无效, 请核对后重新输入.\n");
}
}
void main()
{
Stack Park,Aux_Park;
Queue Road;
int park_cost,road_cost,park_hold,temp_cost;
CarInfo car_info,temp_car;
printf("\n");
printf("\t\t\t数据结构上机实习(二) \n\n");
printf("\t信息学院2001级计科(2)班 姓名:黄和昌 学号:2105613\n\n");
printf("请输入停车场的单位时间停车费:");
scanf("%d",&park_cost);
printf("请输入停车场的容量:");
scanf("\n%d",&park_hold);
InitStack(&Park,park_hold);
InitStack(&Aux_Park,park_hold);
InitQueue(&Road);
printf("请输入车辆信息(到达 离开或退出标志A D或E,车牌号,当前时间):");
scanf("\n%c,%d,%d",&car_info.car_action,&car_info.car_no,&car_info.time_point);
car_info.cost=0;
while(car_info.car_action!='e'&&car_info.car_action!='E')
{ switch(car_info.car_action)
{ case 'a':
case 'A':CarArrive(&Park,&Road,car_info); break;
case 'd':
case 'D':CarLeave(&Park,&Aux_Park,&Road,car_info,park_cost,road_cost);break;
default: printf("对不起,您的输入有错误, 请核对后重新输入.\n");
}
printf("请输入车辆信息(到达离开或退出标志A D或E,车牌号,当前时间):");
scanf("\n%c,%d,%d",&car_info.car_action,&car_info.car_no,&car_info.time_point);
}
printf("程序正常结束!\n");
}