Data Structure - Parking Management (C / C ++)

problem

[Problem Description]
Suppose the parking is narrow opening which can park "cars, and only one door and out of cars for automobile vehicles in the car park by the order of arrival time, arranged in order from north to south (at the southernmost tip of the door , the first to reach the first car parked in the yard of the northernmost), if the car has been parked venue "cars, the car only later on the sidewalk outside the door waiting, once the car drove off, then came in the first car on the sidewalk to open people; when a car leaving the parking lot, after it entered the vehicle must first leave the yard to make way for it to be out of the car outside the gate, and then other vehicles the original order into the parking lot, parked in the yard per car must stay the length of time it costs to pay when it left the parking lot. Parking preparation for the test simulation program management in accordance with the above requirements.
Basic requirements []
to simulate stack parking sidewalk queue to simulate outside the car, the analog input in accordance with the management data to read in sequence from the terminal. Each set of input data include three items: vehicle "arrival" or "left" information, license plate number and time of arrival or departure. Output information for each set of input data to operate as follows: If the vehicle reaches the parking position of the vehicle in the garage or on the sidewalk of the output; if the vehicle away, the residence time of the car in a car park and should the output pay the costs (time spent on the sidewalk free of charge). In order to achieve a stack structure, the list structure to implement the queue.
[Test] Data
provided n = 2, the input data is :( 'A', 1,5), ( 'A', 2, 10), ( 'D', 1,15), ( 'A'.3, 20), ( 'A', 4,25), ( 'A', 5,30), ( 'D', 2,35) ,, ( 'D', 4,40), ( 'E', 0 , 0). Wherein: 'A' represents arrival (Arrival); 'D'
represents a leaving (Departure); 'E' indicates the input end (End).
[Note] realization
Subject to set up a stack, temporary parking exit from the parking lot to make way for the car to the car to leave, also with sequential storage structure to achieve. Input data are ordered in time arrival or departure. Each stack element represents a car containing two data items: the car license number and time into the parking lot.

Realization of ideas

主函数内:{
	停车场
	临时车道
	候补队列
	输入信息
	判断(A )
	执行:
		1、判断(满)
		候补队列入队
		2、判断(非满)
		停车场入栈
	判断(D)
	执行:
		1、入栈临时栈
		2、出栈停车场
		3、出栈临时栈,返回停车场
		4、候补队列首位入栈停车场
		5、候补队列更新
	 判断(E)
	 执行:
	 		退出程序
	 判断(其他数值)
	 执行:
	 	异常报错
		}

code show as below

A custom header file contents:

//test.h
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define A 1
#define D 2
#define E EOF
#define OK 1
#define FALSE -1
#define TURE 1
#define ERROR -1
#define INFEASIBLE        -1
typedef int Status;
typedef int ElemType;

Second, the structure variable declaration

typedef struct CarLnode//定义车辆类型
typedef struct CarList//停车栈
typedef struct Car_Que //等待停车队列
typedef struct SwapList //临时栈,为离开车辆让出位置

Third, the function declaration

//1、初始化
Status InitCarList(CarList &L, int n,double m) //初始化停车场
Status InitQueList(Car_Que &L) //初始化等待队列
Status InitSwapList(SwapList &L, int n) //初始化临时停车道
//2、插入
void InsertQueList(Car_Que &L, CarLnode* car) //尾插入等待队列队列
int InsertCarList(CarList &L, Car_Que &L2, CarLnode *car, int time1)//进入停车场
int InsertSwapList(CarList &L1, SwapList &L2,CarLnode car) //将不离开的车辆移入临时停车道
void InsertSwapList_TurnBack(SwapList &L1, CarList &L2) //将临时车道内的车放回停车场
//3、移动车辆
void DeleteQue(Que &L1) //剩余车辆前移
void DeleteList(CarList &L1, SwapList L2, Que L3, CarLnode car) //离开停车场
 //4、判断车辆动作
int Charge(char n) 
 //5、查看当前停车场使用情况
void Visit(CarList L1, Car_Que L2,int time)

Fourth, the complete code

#include "pch.h"
#include"test.h"
typedef struct CarLnode {//定义车辆类型
	int num;
	int time1;//驶入时间
	int time2;//离开时间
	CarLnode *next;
}CarLnode;
typedef struct CarList{//停车栈
	int size;//停车场大小
	double change;//停车场收费标准
	int length;//停车数量
	CarLnode *carlist;
}CarList;
typedef struct Car_Que {//等待停车队列
	CarLnode *rear;
	CarLnode *carlist;
}Que;
typedef struct SwapList {//临时栈,为离开车辆让出位置
	int length;//剩余车辆数量
	CarLnode *carlist;
}SwapList;
//1、初始化
Status InitCarList(CarList &L, int n,double m) {//初始化停车场
	L.carlist = (CarLnode *)malloc(sizeof(CarLnode)*n);
	L.size = n;
	L.length = 0;
	L.change = m;
	return 0;
}
Status InitQueList(Car_Que &L) {//初始化等待队列
	L.carlist = (CarLnode*)malloc(sizeof(CarLnode));
	L.carlist->next = NULL;
	L.rear = L.carlist;
	return 0;
}
Status InitSwapList(SwapList &L, int n) {//初始化临时停车道
	L.carlist = (CarLnode *)malloc(sizeof(CarLnode)*n);
	L.length = 0;
	return 0;
}
//2、插入
void InsertQueList(Car_Que &L, CarLnode* car) {//尾插入等待队列队列
	car->next = L.rear->next;
	L.rear->next = car;
	L.rear = car;
	//从右往左勾连
}
int InsertCarList(CarList &L, Car_Que &L2, CarLnode *car, int time1) {//进入停车场
	if (L.length == L.size) {//无空闲车位即进入等待队列
		InsertQueList(L2, car);//将car插入等待队列L2
		cout << "车位已满" << car->num << "号车进入等待队列" << endl;//输出提示信息
		return 0;
	}
	else {
		L.carlist[L.length] = *car;//将car插入停车场L的第length+1处
		L.length++;//长度加一
		car->time1 = time1;//记录停放时间
		cout << "时间为第" <<time1 << "分钟时" << car->num << "号车驶入停车场"
			 << L.length << "号车位" << endl;//输出提示信息
		return L.length;
	}
}
int InsertSwapList(CarList &L1, SwapList &L2,CarLnode car) {//将不离开的车辆移入临时停车道
	int i = L1.length-1;//获取最后一辆车的下标
	int j = 0;//临时车道的第一个下标
	int num;//需要将第num+1辆车开出
	for (j; j < L1.length; j++) {//获取num
		if (L1.carlist[j].num == car.num)
		{
			num = j;
			break;
		}
		if (j == L1.length - 1) {//异常:停车场内没有输入车辆,输出提示信息
			cout << "没有检查到该车辆信息" << endl;
			return -1;
		}
	}
	j = 0;
	while (i>num)
	{//将停车场中的车辆从第i+1到第num辆导入到临时车道中
		L2.carlist[j] = L1.carlist[i];
		j++;	L2.length++;
		i--;	L1.length--;
	}
	return num;
}
void InsertSwapList_TurnBack(SwapList &L1, CarList &L2) {//将临时车道内的车放回停车场
	int i = L1.length - 1;
	int j = 0;
	while (i >= 0)
	{//将临时车道中的车辆从第num到第1辆导入到停车场中
		L2.carlist[j] = L1.carlist[i];
		j++;	L2.length++;
		i--;	L1.length--;
	}
}
 //3、移动车辆
void DeleteQue(Que &L1) {//剩余车辆前移
	CarLnode *p=L1.carlist->next;
	L1.carlist->next = L1.carlist->next->next;
	p->next = NULL;
}
void DeleteList(CarList &L1, SwapList L2, Que L3, CarLnode car) {//离开停车场
	int a=InsertSwapList(L1, L2, car);//为离开车辆让位
	if (a == -1) {//异常情况
		cout << "请重新输入" << endl;
	}
	else {//计费,出停车场、等待队列第一位车辆进入停车场
		cout << car.num<< "号车在第" << car.time2 << "分钟时离开停车场" << endl;
		double money = (car.time2 - L1.carlist[a].time1)*L1.change;	//计算停车费用
		cout << car.num << "号车停车费用为:" << money << "元" << endl;
		//停车场内车辆数减一
		L1.length--;
		//将让路车辆放回停车场
		InsertSwapList_TurnBack(L2, L1);
		//判断等待队列是否为空
		if (L3.carlist->next != NULL) {//当等待队列非空时,将队列的第一个车辆放入停车场
			InsertCarList(L1, L3, L3.carlist->next, car.time2);
			DeleteQue(L3);//将队列前移
		}
	}
}
 //4、判断车辆动作
int Charge(char n) {
	if (n == 'A')	   return 1;//车辆停入停车场
	else if (n == 'D') return 2;//车辆离开停车场
	else if (n == 'V') return 3;//查看停车场和等待队列
	else if (n == 'E') return 0;//退出系统
	else {//异常提示
		cout << "请按格式输入!" << endl;
		return 4;
	}
}
 //5、查看当前停车场使用情况
void Visit(CarList L1, Car_Que L2,int time) {
	for (int  i = 0; i < L1.length; i++)
	{
		cout << "第" << i + 1 << "个停车位为" << L1.carlist[i].num << "号车,已经停放" << time - L1.carlist[i].time1 << "分钟" << endl;
	}
	cout << "停车场空闲车位有:" << L1.size - L1.length << "个" << endl;
	CarLnode *p = L2.carlist->next;
	if (p == NULL)
		cout << "目前无车辆排队" << endl;
	else {
		int j = 1;
		do{
			cout << "等待队列第" << j << "位车为:" << p->num << "号车" << endl;;
			j++;
			p = p->next;
		} while (!(p == NULL));
	}
}
int main()
{
	//定义停车场、临时车道、等待队列
	CarList L1;
	SwapList L2;
	Car_Que L3;
	int size, change;//停车场规模、收费标准
	int a;//接受函数返回值
	cout << "请输入停车场大小和收费标准(元)" << endl;
	cin >> size >> change;
	cout << "温馨提示:" << endl
		<< "1、本停车场每10分钟收费: " << change
		<< " 元 不足10分钟按10分钟计算" << endl
		<< "2、查看目前停车场使用情况和排队情况请输入\"V\"和时间" << endl;
	//初始化停车场、临时车道、等待队列
	InitCarList(L1,size,change);
	InitSwapList(L2, size-1);
	InitQueList(L3);
	//判断车辆动作
	char Action;
	cout << "请输入停车信息(格式:(动作,车牌,时间)):" << endl;
	//创建数组存放每次输入的car
	CarLnode car[100];
	//每次输入一组数据,i++
	int i = 0;
	while (cin>>Action)//控制运行
		switch (Charge(Action)) {//判断车辆动作
		case 1://进入停车场
			cin >> car[i].num >> car[i].time1;
			a=InsertCarList(L1, L3,&car[i],car[i].time1);//插入停车栈,栈满调用插入队列函数,进入等待队列
			i++;
			break;
		case 2://离开停车场
			cin >> car[i].num >> car[i].time2;
			DeleteList(L1, L2, L3, car[i]);
			i++;
			break;
		case 3:
			int time;
			cin >> time;
			Visit(L1, L3,time);//查看停车场情况
		case 4:
			continue;//输入异常时,提示错误,并继续运行
		case 0:
			exit(0);
		}
	return 0;
}
Released five original articles · won praise 14 · views 608

Guess you like

Origin blog.csdn.net/qq_43732324/article/details/102987626