停车场管理系统(C语言作业)

问题描述:

设停车场是可停放5辆车,且只有一个大门可供汽车进出。在停车场内,汽车按到达的先后次序停放。若停车场内已停满5辆车,则后来的汽车需在停车场外等候,当有车开走时,停车场外上的第一辆车可进入。当停车场内某辆车要离开时,在它之后进入的车辆必须先退出停车场为它让路,待该辆车开出大门后,其他车辆再按原次序返回车场。每辆车离开停车场时,应按其停留时间的交费。

设计要求:

1.模拟上述管理过程。要求以顺序堆栈模拟停车场,以链式队列模拟停车场外等待车辆。
2.从终端读入汽车到达或离去的数据,每组数据包括三项:(①是“到达”还是“离开”;②汽车牌照号码;③“到达”或“离开”的时刻)。
3.与每组输入信息相应的输出信息为:如果是到达的车辆,则输出其在停车场中或停车场外的位置;如果是离去的车辆,则输出其在停车场中停留的时间和应交的费用。

源代码

#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();
	}
}


猜你喜欢

转载自blog.csdn.net/qq_45914759/article/details/106978187