Parking lot management system (C language operation)

Problem Description:

There is a parking lot that can park 5 cars, and there is only one gate for cars to enter and exit. In the parking lot, cars are parked in order of arrival. If the parking lot is full of 5 cars, the subsequent cars must wait outside the parking lot. When there is a car driving away, the first car on the outside of the parking lot can enter. When a car in the parking lot wants to leave, the vehicles entering after it must first exit the parking lot to make way for it, and after the car leaves the gate, other vehicles will return to the parking lot in the original order. When each vehicle leaves the parking lot, the fee shall be paid according to the length of stay.

Design requirements:

1. Simulate the above management process. It is required to simulate a parking lot with sequential stacking and a chain queue to simulate waiting vehicles outside the parking lot.
2. Read in the arrival or departure data of the car from the terminal. Each group of data includes three items: (①Whether it is "arrival" or "departure"; ②car license plate number; ③the time of arrival" or "departure").
3. The output information corresponding to each set of input information is: if it is an arriving vehicle, output its position in the parking lot or outside the parking lot; if it is a departing vehicle, output its staying time and response in the parking lot. Fees.

Source code

#include<stdio.h>
#include<stdlib.h>
#include <time.h>

#define ElemType int
#define MAX 5
#define FEE_per_s 1

typedef struct {
	ElemType s_num[MAX];
	int top;
	int time[MAX];
}SeqStack;//堆栈

typedef struct QNode{
	ElemType q_num;
	struct QNode *next;
}QNode;//队列的结点

typedef struct{
	QNode *front;
	QNode *rear;
}LinkQueue;

void InitStack(SeqStack *S){
	S->top = 0;
}

void InitQueue(LinkQueue *Q){
	Q->front = Q->rear = (QNode*)malloc(sizeof(QNode));
	Q->front->next = NULL;
}

int Push(SeqStack *S, ElemType c){
	if (S->top == MAX){//判断是否满
		return 0;
	}
	S->s_num[S->top] = c;
	S->top++;
	return 1;
}

int Pop(SeqStack *S, ElemType *c){
	if (S->top == 0){//非空否
		return 0;
	}
	*c = S->s_num[S->top];
	S->top--;
	return 1;
}

void EnQueue(LinkQueue *Q, ElemType c){
	QNode* p = (QNode*)malloc(sizeof(QNode));
	p->q_num = c;
	p->next = NULL;
	Q->rear->next = p;
	Q->rear = p;
}
int DeQueue(LinkQueue *Q, ElemType *c){
	if (Q->front == Q->rear){
		return 0;
	}
	
	QNode* p = Q->front->next;
	*c = p->q_num;
	Q->front->next = p->next;
	free(p);
	return 1;
}

int COUNT_TIME() {
	time_t tmpcal_ptr;
	struct tm *tmp_ptr = NULL;
	time(&tmpcal_ptr);
	return tmpcal_ptr;
}

int COUNT_FEE(SeqStack S, int gar_NUM) {
	int fee;
	time_t tmpcal_ptr;
	struct tm *tmp_ptr = NULL;
	time(&tmpcal_ptr);
	fee = (tmpcal_ptr - S.time[gar_NUM])*FEE_per_s;
	return fee;
}

void Find(SeqStack *S, ElemType car){
	int money;
	int i = S->top;
	ElemType car_t = S->s_num[i - 1];
	int time_t = S->time[i - 1];

	while (i){
		if (car_t == car){
			S->top--;
			i--;
			car_t = S->s_num[i - 1];
			time_t = S->time[i - 1];
			money = COUNT_FEE(*S, i);
			printf("%d出库成功,停车%d秒,需付费%d元", car, money, money);
			break;
		}
		else{
			i--;
			car_t = S->s_num[i - 1];
			time_t = S->time[i - 1];
			S->s_num[i - 1] = S->s_num[i];
			S->time[i - 1] = S->time[i];
		}
	}
	if (i == 0){
		printf("车库里没有%d车", car);
	}


}

int main(){
	SeqStack S;
	LinkQueue Qc;
	InitStack(&S);
	InitQueue(&Qc);
	char ch;
	ElemType car;
	int time;
	printf("请输入:\n");
	printf("a:进入车辆\n");
	printf("b:驶出车辆\n");
	printf("c:退出系统\n");
	printf("每秒收费%d元\n", FEE_per_s);
	while (1) {
		scanf_s("%c", &ch);
		if (ch == 'a'){
			scanf_s("%d", &car);
			if (Push(&S, car) == 0){
				printf("车库已满!");
				EnQueue(&Qc, car);
			}
			else{
				printf("%d入库成功", car);
				S.time[S.top - 1] = COUNT_TIME();//加入初始时间
			}
		}
		else if (ch == 'b'){
			scanf_s("%d", &car);
			
				Find(&S, car);
			
		}
		else if (ch == 'c'){
			return 0;
		}
		
		printf("\n");
		getchar();
	}
}


Guess you like

Origin blog.csdn.net/qq_45914759/article/details/106978187
Recommended