C语言停车场管理模拟系统

【问题描述】
某停车场是一个可停放n辆汽车的狭长通道,且只有 一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。按上述要求试为停车场编制车辆管理的模拟程序。

【基本要求】
以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。

【测试数据】
设n=2,输入数据为:(`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).其中:`A`表示到达(Arrival);`D`表示离去(Departure);`E`表示输入结束(End)。

【实现要求】

需另设一个栈,临时停放为给要离去的汽车让路而从停车场退出来的汽车,也用顺序存储结构实现。输入数据按到达或离去的时刻有序。栈中每个元素表示一辆汽车,包含两个数据项:汽车的牌照号码和进入停车场的时刻。

特别注意:
整个车辆的入场和出场必须严格的通过调用队列和栈的相关函数实现。

【运行结果】

(1)当停车场内车辆未满时:

 (2)当下一辆车辆入栈,显示当前停车场内车辆信息

 

 (3)当停车场内车辆容量已达上限,提示停车场内车辆已满,显示栈道内车辆信息:

 (4)当车辆出停车场,显示应收费信息:

 

源代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <malloc.h>
#define Max 2	//停车场容量 
#define PRICE 2 //收费标准 :2元/小时 

typedef struct car{
	char id[10];//车牌 
	char sta;	//状态:A,D,E 
	int t;		//入场或离场时间 
}car;
typedef struct S *SNode;
struct S{//栈 
	car data[Max];
	int top;
};
typedef  SNode Stack;
typedef struct Qd{
	car data;
	struct Qd *next;
} *QNode;
typedef struct Q{//队列 
	QNode front,rear;
}*Que;
bool push(Stack s,car carp);
bool pop(Stack s,car *carp);
bool insertQ(Que q,car carp);
bool DeleQ(Que q,car *carp);
void printQ(Que q);//输出队列中所有车辆信息
void printS(Stack s);//输出停车场中所有车辆信息 
void menu();
int main(){
	int choice;
	car carp,*tp,*tp2;
	tp=(struct car*)malloc(sizeof(struct car));
	tp2=(struct car*)malloc(sizeof(struct car));
	Stack s1,s2;
	s1=(struct S*)malloc(sizeof(struct S));
	s2=(struct S*)malloc(sizeof(struct S));
	Que q;
	//初始化停车场 
	s1->top=-1;//停车场 
	s2->top=-1; // 中转 
	//初始化便道 
	q=(Que)malloc(sizeof(struct Q));
	q->front=q->rear=NULL;
	while(1){
		printf("\n\n\n\n\t\t\t\t........欢迎进入停车场管理系统!.......\n ");
		printf("\t\t\t\t     请输入操作信息(输入E退出系统)\n");
		printf("例:(A 蒙E 1 )表示车牌号为蒙E的车辆于1时进入停车场\n   (D 蒙E 5 )表示车牌号为蒙E的车辆于5时离开停车场\n"); 
		scanf("%c",&carp.sta);
		if(carp.sta=='E'||carp.sta=='e'){
			  printf("感谢使用停车场管理系统!\n");
			  exit(0);
			  break;
		}
		scanf("%s %d",carp.id  ,&carp.t );
		switch (carp.sta){
			case 'A'://来车 
			case 'a': 
				if(s1->top ==Max-1) 
				  //进入便道,同时输出便道上所有车辆信息 
				{
					printf("\n停车场已满,该车将驶入便道等候!\n\n");
					insertQ(q,carp);
				  
				    printS(s1); 
				    printQ(q); 
			    }
				else{
				 //进入停车场 ,同时输出便道上所有车辆信息
				 	push(s1,carp);
				 	printS(s1);
				}   
				break;
			case 'D'://走车
			case 'd':
				if(s1->top ==-1) //停车场为空 
				  printf("Not found!\n"); 
				else{//离开停车场 
					while(strcmp(s1->data [s1->top].id,carp.id )!=0 &&s1->top !=-1){//查找 
						push(s2,s1->data [s1->top ]);
						pop(s1,tp);
					}
					if(s1->top ==-1)
					  printf("Not found!\n");
					else{
					  pop(s1,tp);
					  tp2->t=carp.t;
					  printf("车牌号码为 %s 的车辆停留了%d小时,应收费用%d元\n", carp.id,carp.t -tp->t,(carp.t -tp->t)*PRICE );
					  void printS(Stack s);
					  while(s2->top !=-1){
						  pop(s2,tp);
						  push(s1,*tp);	
					  	}
					 if(q->front !=NULL){ //便道上有车,将头车入库 
					  	DeleQ(q,&carp);
					  	carp.t=tp2->t;
						push(s1,carp);
					  } 	
					}
				} 
				break;
		}
		system("pause");
		system("cls") ;
		getchar();
	}
	return 0; 
}


void printS(Stack s)
{
	int i;
	printf("停车场内车辆信息:\n");
	for(i=0;i<=s->top;i++)
	{
		printf("车牌号	到达时间\n%s\t %d时\n\n",s->data[i].id,s->data[i].t);
	}
}

void printQ(Que q)
{
	struct Qd* p;
	p=(struct Qd*)malloc(sizeof(struct Qd));
	p=q->front;
	printf("---------------------------\n"); 
	printf("便道内车辆信息:\n");
	printf("车牌号	到达时间\n%s\t %d时\n",p->data.id,p->data.t);
}

bool insertQ(Que q,car carp)
{
	QNode p;
	p=(QNode)malloc(sizeof(struct Qd));
	p->data=carp;
	p->next=NULL;
	if(q->front==NULL)
	{
		q->front=p;
		q->rear=p;
	}
	else{
		q->rear->next=p;
		q->rear=p;
	}
}

bool push(Stack s,car carp)
{
	s->top++;
	s->data[s->top]=carp;
}

bool pop(Stack s,car* carp)
{
	*carp=s->data[s->top--];
}

bool DeleQ(Que q,car* carp)
{
	QNode p;
	*carp=q->front->data;
	p=q->front;
	q->front=q->front->next;
	free(p);
}


  • 9
    点赞
  • 164
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
停车场管理系统通常包括车辆和停车场两个主要的实体。 对于车辆,我们需要记录车牌号、入场时间、停车时长等信息。对于停车场,我们需要记录停车场的总车位数、当前空闲车位数、停车费用等信息。 以下是一个简单的 C 语言代码示例,模拟停车场管理系统: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_SIZE 100 // 停车场最大容量 // 车辆结构体 typedef struct Car { char license[10]; // 车牌号 time_t enter_time; // 入场时间 int parked_time; // 停车时长(分钟) } Car; // 停车场结构体 typedef struct ParkingLot { int total_space; // 总车位数 int available_space; // 空闲车位数 float fee_rate; // 停车费率(每小时多少元) Car cars[MAX_SIZE]; // 停车场中的车辆数组 } ParkingLot; // 初始化停车场 void init_parking_lot(ParkingLot *parking_lot, int total_space, float fee_rate) { parking_lot->total_space = total_space; parking_lot->available_space = total_space; parking_lot->fee_rate = fee_rate; } // 进入停车场 void enter_parking_lot(ParkingLot *parking_lot, char *license) { if (parking_lot->available_space <= 0) { printf("停车场已满,无法进入!\n"); return; } // 生成随机入场时间 srand((unsigned int)time(NULL)); time_t enter_time = time(NULL) - rand() % 3600; // 添加车辆到停车场 Car car = { .enter_time = enter_time, .parked_time = 0 }; strcpy(car.license, license); parking_lot->cars[parking_lot->total_space - parking_lot->available_space] = car; parking_lot->available_space--; printf("车辆 %s 进入停车场,入场时间:%s", license, ctime(&enter_time)); } // 离开停车场 void leave_parking_lot(ParkingLot *parking_lot, char *license) { int i; for (i = 0; i < parking_lot->total_space; i++) { if (strcmp(parking_lot->cars[i].license, license) == 0) { // 计算停车费用 int parked_time = (int)difftime(time(NULL), parking_lot->cars[i].enter_time) / 60; float fee = parked_time * parking_lot->fee_rate; printf("车辆 %s 离开停车场,停车时长:%d分钟,停车费用:%.2f元\n", license, parked_time, fee); // 从停车场中删除车辆 int j; for (j = i; j < parking_lot->total_space - 1; j++) { parking_lot->cars[j] = parking_lot->cars[j + 1]; } parking_lot->available_space++; return; } } printf("未找到车辆 %s,无法离开停车场!\n", license); } // 显示停车场信息 void show_parking_lot_info(ParkingLot *parking_lot) { printf("停车场总车位数:%d,当前空闲车位数:%d,停车费率:%.2f元/小时\n", parking_lot->total_space, parking_lot->available_space, parking_lot->fee_rate); } int main() { ParkingLot parking_lot; init_parking_lot(&parking_lot, 100, 5); enter_parking_lot(&parking_lot, "京A12345"); enter_parking_lot(&parking_lot, "京B67890"); show_parking_lot_info(&parking_lot); leave_parking_lot(&parking_lot, "京A12345"); show_parking_lot_info(&parking_lot); return 0; } ``` 以上代码中,我们定义了两个结构体 `Car` 和 `ParkingLot`,分别表示车辆和停车场。在 `main` 函数中,我们先初始化了一个停车场,然后模拟了两辆车进入停车场,并输出了停车场当前的信息。接着,我们让一辆车离开了停车场,并再次输出了停车场的信息。 这只是一个简单的停车场管理系统代码示例,实际的停车场管理系统还需要考虑很多其他因素,比如停车场的收费策略、车辆进出的安全性等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值