Parking lot management

Main idea of the topic: there is a long and narrow parking lot that can park n vehicles. It has only one gate for vehicles to enter and exit. Vehicles can park from the innermost part of the parking lot to the gate in turn according to the time of arrival at the parking lot (the first vehicle that arrives first is placed in the innermost part of the parking lot). Then, if the parking lot is full of N vehicles, the subsequent vehicles can only park on the sidewalk outside the gate of the parking lot. Once there is a vehicle driving away from the parking lot, the vehicles entering the parking lot after it must exit the parking lot to make way for it. After they leave the parking lot, these vehicles will enter the parking lot in the original order. When leaving the parking lot, each vehicle shall pay the fee according to the length of time it stays in the parking lot. If those who stay on the sidewalk leave without entering the parking lot, they are allowed to leave without charging parking fees, and the order of waiting vehicles on the sidewalk is still maintained. Compile a program to simulate the management of parking lot.

2.[implementation requirements]
The program is required to output the parking position (on the parking lot or sidewalk) after each vehicle arrives, as well as the fees payable when a vehicle leaves the parking lot and the time it stays in the parking lot;

3.[implementation prompt]
The format of vehicle analog input information can be: (arrival / departure, vehicle license plate number, arrival / departure time). For example, (A, 1, 5) means that No. 1 license plate vehicle arrives at the time of 5, while (D, 5, 20) means that No. 5 vehicle leaves at the time of 20. The whole program can end when the input information is (E, 0, 0). This problem can be realized by stack and queue.

You need two choices, one is to enter the station, the other is to leave the station. There are two operations to enter the station. If the parking lot is not full, enter the parking lot, or enter the sidewalk. When you enter the parking lot, you have to start counting until you leave the parking lot. The money you pay is the time you stopped. You don't have to pay when you leave on the sidewalk. If the parking lot is not full and there is a car on the access road, the car must be parked in the parking lot.

Code:

#include<stdio.h>  
const int n=10;

typedef struct
{
	int num;
	int time;
}node;

struct  
{
	node arr[500];
	int base=0;
	int top=0;
	int cap=0;
}stack;

struct 
{
	node arr[500];
	int front=0;
	int rear=0;
}quene;

void quenepush(int num,int time)
{
	quene.arr[quene.rear].num=num;
	quene.arr[quene.rear].time=time;
	quene.rear++;
}

void quenepop(int num,int time)
{
	bool fl=false;
	for(int i=quene.front;i<quene.rear;i++)
	{
		if(fl)
		{
			quene.arr[i-1]=quene.arr[i];
		}
		if(quene.arr[i].num==num)
		{
			fl=true;
		}
	}
	quene.rear--;
}

void stackpush(int num,int time)
{
	if(stack.cap == 3) quenepush(num,time);
	else
	{
	  stack.arr[stack.top].num=num;
	  stack.arr[stack.top].time=time;
	  stack.top++;
	  stack.cap++;	
	  printf("Successfully enter the parking lot\n");
	} 
}

int stackpop(int num,int time)
{
	node b;bool fl=false;
	//printf("%d",stack.top);
	for(int i=0;i<stack.top;i++)
	{
		if(fl)
		{
			stack.arr[i-1]=stack.arr[i];
		}
		printf("%d In the parking lot\n",stack.arr[i].num);
		if(stack.arr[i].num == num)
		{
			b=stack.arr[i];fl=true;;
		}
	}
	
	if(stack.arr[stack.top-2].num == stack.arr[stack.top-1].num || stack.arr[stack.top-1].num == num )
	{
		stack.top--;
	    if(quene.front != quene.rear)
	    {
	    	stack.arr[stack.top].num = quene.arr[quene.front].num;
	        stack.arr[stack.top].time = time;
	        quene.front++;
	    	stack.top++;
		}
		return b.time;
	}
    else return -1;
}


int main()
{	
	char c; int num,time;
	while(true)
	{
		printf("Your choice, license plate number, arrival time:");
	
		scanf("%c %d %d",&c,&num,&time);//printf("%c",c);
	    getchar();
		if(c=='A')
		{
			stackpush(num,time);
		}
		else if(c=='D')
		{
			int tt = stackpop(num,time);
			if(tt > 0) 
			{
				printf("Charge in the parking lot:%d\n",time-tt);
			}
			else 
			{
				printf("In the aisle, no charge\n");
				quenepop(num,time);
			}
		}
		else  break;
	}
	return 0;
}

Demo code:

 

Problem summary: in the output of c, we need to understand the input buffer. Every time we type the characters of the keyboard into the input buffer, and carriage return represents the end. However, a carriage return character is also stored in the input buffer, so we can use the getchar() function. Getchar() takes one from the buffer, and this method can eliminate carriage return.

Tags: C programming language

Posted by Zanne on Thu, 02 Jun 2022 02:33:08 +0530