#include<iostream> #include<iomanip> #include<malloc.h> using namespace std; #define Max 3 //停车场容量 #define Price 0.10 //收费价格 #define ERROR -1 typedef struct { int hour; int min; } Time; typedef struct Node { char num[20]; Time reach; Time leave; } CarNode; typedef struct stackNode { CarNode *data[Max + 1]; int top; } StackCar; typedef struct LinkNode { CarNode *data; struct LinkNode *next; } QueueNode; typedef struct { QueueNode *front; QueueNode *rear; } QueueCar; int InitStack(StackCar *S) { S->top = 0; return 0; } int InitQueue(QueueCar *Q) { Q->front = Q->rear = (QueueNode*)malloc(sizeof(QueueNode)); if (!Q->front) exit(ERROR); Q->front->next = NULL; return 0; } int Push(StackCar *S, CarNode *p) { S->top++; cout << "汽车停在停车场第" << S->top << "个位置" << endl; loop: cout << "汽车到达的时间为[小时(0-23)和分钟(0-59)以空格隔开]:"; cin >> p->reach.hour >> p->reach.min; while ((p->reach.hour<0 || p->reach.hour>23) || (p->reach.min<0 || p->reach.min>59)) { cout << endl << "你的输入时间范围有误,请重新输入!" << endl << endl; goto loop; } S->data[S->top] = p; cout << endl << "*************汽车进入停车场成功*************" << endl << endl; return 0; } int PushStack(StackCar *S, CarNode *p) { S->top++; S->data[S->top] = p; return 0; } int Pop(StackCar *S, CarNode *(&p)) { p = S->data[S->top]; S->top--; return 0; } int EnQueue(QueueCar *Q, CarNode *p) { cout << endl << "停车场车位已满,请该汽车在便道上停靠等待!" << endl; QueueNode *t; t = (QueueNode*)malloc(sizeof(QueueNode)); if (!t) exit(ERROR); t->data = p; t->next = NULL; Q->rear->next = t; Q->rear = t; cout << "************汽车在便道上停靠成功***********" << endl << endl; return 0; } int DeQueue(QueueCar *Q, CarNode *(&q)) { QueueNode *t; t = Q->front->next; q = t->data; Q->front->next = t->next; if (Q->rear == t) Q->rear = Q->front; free(t); return 0; } void PriceCal(CarNode *p, int Location) { //计算价格 loop: cout << "请输入汽车离开的时间[小时(0-23)和分钟(0-59)以空格隔开]:"; cin >> p->leave.hour >> p->leave.min; while ((p->leave.hour<0 || p->leave.hour>23) || (p->leave.min<0 || p->leave.min>59)) { cout << "你的输入时间范围有误,请重新输入!" << endl; goto loop; } cout << "离开汽车的车牌号为:"; cout << p->num << endl; cout << "该汽车到达的时间为:"; cout << p->reach.hour << ":" << p->reach.min << endl; cout << "该汽车离开的时间为:"; cout << p->leave.hour << ":" << p->leave.min << endl; cout << "该汽车应交费用为:<单位:元>"; if ((p->leave.hour<p->reach.hour) || ((p->leave.hour == p->reach.hour) && (p->leave.min<p->reach.min))) cout << fixed << setprecision(2) << ((23 - p->reach.hour + p->leave.hour) * 60 + (60 - p->reach.min + p->leave.min))*Price << endl << endl; else cout << fixed << setprecision(2) << ((p->leave.hour - p->reach.hour) * 60 + (p->leave.min - p->reach.min))*Price << endl << endl; free(p); } void InCarPark(StackCar *Enter, QueueCar *Q) { CarNode *p; p = (CarNode*)malloc(sizeof(CarNode)); cout << "请输入到达汽车的车牌号:"; cin >> p->num; if (Enter->top<Max) { Push(Enter, p); } else { EnQueue(Q, p); } } void OutCarPark(StackCar *Out, StackCar *Temp, QueueCar *Q) { int Location; CarNode *p, *q; if (Out->top>0) { loop: cout << "请输入汽车在停车场的位置<1-" << Max << ">:"; cin >> Location; if (Location<1 || Location>Out->top) { cout << "停车场该车位还未停车,请重新输入!" << endl; goto loop; } while (Out->top>Location) { Pop(Out, p); //Out,Out->data[Out->top]; PushStack(Temp, p); //Temp,Out->data[Out->top]; } Pop(Out, p); PriceCal(p, Location); while (Temp->top >= 1) { Pop(Temp, p); //Temp,Temp->data[Temp->top]; PushStack(Out, p); //Out,Temp->data[Temp->top]; } if ((Q->front != Q->rear) && Out->top<Max) { DeQueue(Q, q); //将队列结点t取值data赋给结点q并出队列; cout << "*************便道车位位置[1]的汽车将进入停车场**************" << endl; Push(Out, q); } else cout << endl << "*******便道上没有汽车,停车场没有进入新的汽车!**********" << endl << endl; } else cout << "***********停车场里没有汽车可以选择离去!*************" << endl << endl; } void DisplayStack(StackCar *S) { int i; cout << "你需要查看的停车场停车情况如下:" << endl; if (S->top>0) { for (i = 1; i <= S->top; i++) { cout << "在停车场的车位位置:" << i << endl; cout << "该车位汽车的车牌号:" << S->data[i]->num << endl; cout << "到达时间:" << S->data[i]->reach.hour << ":" << S->data[i]->reach.min << endl << endl; } } else cout << endl << "停车场里没有车,所以无法显示你要查看的停车信息!" << endl << endl; } void DisplayQueue(QueueCar *Q) { QueueNode *t; int i; t = Q->front->next; if (Q->front != Q->rear) { cout << "你需要查看的便道停车情况如下:" << endl; for (i = 1; t != NULL; i++) { cout << "在便道上的车位位置:" << i << endl; cout << "该车位汽车的车牌号:" << t->data->num << endl << endl; t = t->next; } } else cout << "便道上没有车,所以无法显示你要查看的停车信息!" << endl << endl; } void menu() { cout <<" ┏------------------------------┓"<< endl; cout <<" ┠ 停车场管理系统 ┨"<< endl; cout <<" ┠------------------------------┨"<< endl; cout <<" ┠ 输入1:汽车到达停车场操作 ┨"<< endl; cout <<" ┠ 输入2:汽车离去停车场操作 ┨"<< endl; cout <<" ┠ 输入3:停车场停车信息显示 ┨"<< endl; cout <<" ┠ 输入4:便道停车信息显示 ┨"<< endl; cout <<" ┠ 输入5:退出停车场管理系统 ┨"<< endl; cout <<" ┗------------------------------┛"<< endl << endl; } int main() { StackCar S, Temp; QueueCar Q; char M; InitStack(&S); InitStack(&Temp); InitQueue(&Q); while (1) { menu(); cout << "请输入你要选择的功能序号:"; cin >> M; getchar(); cout << endl; switch (M) { case '1': InCarPark(&S, &Q); system("pause"); system("cls"); break; case '2': OutCarPark(&S, &Temp, &Q); system("pause"); system("cls"); break; case '3': DisplayStack(&S); system("pause"); system("cls"); break; case '4': DisplayQueue(&Q); system("pause"); system("cls"); break; case '5': return 0; default: cout << "你的选择有误,此管理系统没有此项功能!" << endl << endl; } } system("pause"); }
实验要求
1.1实验目的
深入掌握栈和队列应用的算法设计。
1.2实验内容
编写满足以下要求的停车场管理程序exp3-9.cpp。设停车场内只有一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。
汽车在停车场内按车辆到达时间的先后顺序依次由南向北排列(大门在最北端,最先到达的第一辆车停放在停车场的最南端),若停车场内已停满n辆车,则后来的汽车只能在门外的便道(即候车场)等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该车辆开出大门外,其他车辆再按原次序进入停车场,每辆停放在停车场的车在它离开停车场时必须按停留的时间长短交纳费用。