1.问题描述:
设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达的时间先后顺序依次排列,若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦停车场内有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,由于停车场内是狭长的通道,在他之后开入的车辆必须先退出车场为它让路,待该辆车开出大门外,为它让路的车辆再按原次序进入车场。假设汽车不能从便道上开走。
2.基本要求
(1)栈用顺序结构实现,队列用链式结构实现。
(2)每一组输入数据包括三个数据项:汽车"到达"或"离去"的信息、汽车牌照号码、汽车到达或离去的时刻。
(3)对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出车辆在停车场内或便道上的停车位置;若是车辆离去,则输出车辆在停车场内停留的时间和应缴纳的费用(假设在便道上等候的时间不收费)。
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include<conio.h>
#include <time.h>
#define Stack_Size 4
typedef struct{ //车辆信息
char number[10]; //车牌号
int reachHour;
int reachMin;
int seconds;
}Car;
typedef struct Stack1{ //停车场
Car carNode[Stack_Size];
int top;
}ParkingLot;
typedef struct Stack2{ //临时栈,存放让路车
Car carNode[Stack_Size];
int top;
}TempStack;
typedef struct LNode{ //连接便道
Car data;
LNode *next;
}*Linklist;
typedef struct Queque{ //便道 ,封装队列头尾指针
Linklist front;
Linklist rear;
}Path;
void initStack(ParkingLot *pl){
pl->top=-1;
}
void initTempStak(TempStack *ts){
ts->top=-1;
}
void initQueque(Path *path){ //头结点为空
path->front=(LNode *)malloc(sizeof(LNode));
path->front->next=NULL;
path->rear=path->front;
}
void listStack(ParkingLot *pl){
int index;
system("cls");
printf("\n车牌号\t车道\t进场时间"); //车道=pl->top+1
for(index=0;index<=pl->top;index++){
printf("\n%s\t%d\t%d:%d",pl->carNode[index].number,index+1,pl->carNode[index].reachHour,pl->carNode[index].reachMin);
}
getch();
}
void listQueque(Path *path){
system("cls");
printf("\n便道状况:\n");
Linklist p;
p=path->front;
p=p->next;
while(p!=NULL){
printf("%s号车正在便道等待\n",p->data.number);
p=p->next;
}
printf("\n\n按任意键继续!");
getch();
}
void backPL(ParkingLot *pl,TempStack *ts){ //将临时栈的车开回停车场,更新车道值
pl->top=0;
int tsTop;
tsTop=ts->top;
while(pl->top!=tsTop||tsTop!=-1){ //当临时栈不为空栈,且临时栈顶不等于停车场栈顶
pl->carNode[pl->top]=ts->carNode[ts->top];
pl->top++;
ts->top--;
}
}
void arrival(ParkingLot *pl,Path *path){
time_t p;
struct tm *reach;
Linklist temp;
char c='y';
char num[10];
while(c=='y'||c=='Y'){
printf("\n请输入车牌号:");
scanf("%s",&num);
if(pl->top==Stack_Size-1){ //当车位已满
temp=(LNode *)malloc(sizeof(LNode));
strcpy(temp->data.number,num);
temp->next=NULL;
path->rear->next=temp;
path->rear=temp;
printf("\n车位已满,%s号车辆进入便道等候!",path->rear->data.number);
printf("\n停车成功,是否继续停车(y/n)?");
fflush(stdin);
scanf("%c",&c);
}else{ //当车位未满
pl->top++;
time (&p);
reach=localtime(&p);
pl->carNode[pl->top].seconds=p;
pl->carNode[pl->top].reachHour=reach->tm_hour;
pl->carNode[pl->top].reachMin=reach->tm_min;
strcpy(pl->carNode[pl->top].number,num);
printf("\n停车成功,是否继续停车(y/n)?");
fflush(stdin);
scanf("%c",&c);
}
}
}
void leave(ParkingLot *pl,Path *path,TempStack *ts){
char ch='y';
int index;
time_t p;
struct tm *leave;
while(ch=='y'||ch=='Y'){
system("cls");
listStack(pl);
printf("\n请输入要出场车辆的车道:");
scanf("%d",&index);
fflush(stdin);
if(index-1>pl->top||index-1<0){ //index-1对应pl->top
printf("\n请输入正确的车道!");
printf("\n按任意键继续!");
getch();
}else{
ts->top=-1;
while(pl->top!=index-1){ //当要出车的车道不等于栈顶时,该车道之前的车进临时栈
ts->top++;
ts->carNode[ts->top]=pl->carNode[pl->top];
pl->top--;
} //此时要出车的车道等于栈顶时,直接出车,不需进临时栈
printf("\n提车成功!");
time (&p);
leave=localtime(&p);
printf("\n%s的驶离时间为%d:%d,",pl->carNode[pl->top].number,leave->tm_hour,leave->tm_min);
printf("收费 %2.1f元",(p-pl->carNode[pl->top].seconds)*0.02); //假设每秒收费0.02元
backPL(pl,ts);
printf("\n是否继续提车?(y/n)");
scanf("%c",&ch);
}
}
}
int menu(){
int flag;
system("cls");
printf("\n************欢迎使用停车场管理系统************\n");
printf("\n\t 按[1]停车");
printf("\n\t 按[2]提车");
printf("\n\t 按[3]查看停车场状况");
printf("\n\t 按[4]查看便道状况");
printf("\n\t 按[5]退出系统");
printf("\n***************************************\n");
do{
printf("请输入1~5!!\n");
scanf("%d",&flag);
}while(flag<1||flag>5);
return flag;
}
void quit(){
printf("\n\n谢谢使用!");
exit(0);
}
void initTempStak(TempStack *ts);
void initStack(ParkingLot *pl);
void initQueque(Path *path);
void arrival(ParkingLot *pl,Path *path);
void leave(ParkingLot *pl,Path *path,TempStack *ts);
void listStack(ParkingLot *pl);
void backPL(ParkingLot *pl,TempStack *ts);
int menu();
void listQueque(Path *path);
int main(){
ParkingLot pl;
Path path;
TempStack ts;
initTempStak(&ts);
initStack(&pl);
initQueque(&path);
while(1){
switch(menu()){
case 1: arrival(&pl,&path); break;
case 2: leave(&pl,&path,&ts); break;
case 3: listStack(&pl); break;
case 4: listQueque(&path); break;
case 5: quit(); break;
}
}
getch();
}