Linux c 停车场管理系统

 功能:1 车辆进出登记 2 出入记录 3 候车场 4 查看 停车场及候车场车辆信息及总数 5  查看停留时间最长的车辆 。。。

代码如下

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

#define SIZE   5
#define SUCCESS   10001
#define FAILURE   10002
#define TURE      10003
#define FAISE     10004
#define price       100



//出入记录
struct record
{
	
	int num;
	long int time1;
	long int time2;
	float time;
	struct record *next;
	
};

typedef struct record srd;


struct stack  //停车场
{
	
	//记录车牌号码
	//记录进场时间
	//记录出场时间
    int *num;
	long int *time1;
	long int *time2;
	int top;
	
};

typedef struct stack ssk;

struct wait   //候车场
{
	
	//顺序队列
	int *num;
	long int *time1;
	int front;
	int rear;
};

typedef struct wait ssw;

struct exchange //换车场
{
	//换车用的
	int *num;
	long int *time1;
	long int *time2;
	int top;
};

typedef struct exchange sse;


int ret;
ssk *car;
ssw *wait;
ssk *exchange;
srd *record;
int x = 0;


int initrecord(srd **r)
{
	
	(*r) = (srd *)malloc(sizeof(srd) * 1);
	
	if(NULL == (*r))
	{
		return FAILURE;
	}
	
	(*r)->next = NULL;
	
	return SUCCESS;
	
}

//给候车场进行初始化
int initwait(ssw ** w)
{
	
	//分配空间
	(*w) = (ssw *)malloc(sizeof(ssw));
	
	if(NULL == (*w))
	{
		return FAILURE;
	}
	
	(*w)->num = (int *)malloc(sizeof(int)*100);
	
	if(NULL == (*w)->num)
	{
		return FAILURE;
	}
	
	(*w)->time1 = (long int *)malloc(sizeof(long int)*100);
	
	if(NULL == (*w)->time1)
	{
		return FAILURE;
	}
	
	(*w)->front = (*w)->rear = 0;
	
	return SUCCESS;
	
}



//主界面
void welcome()
{
	
	printf("**************欢迎来到德莱联盟****************\n");
	printf("**********************************************\n");
	printf("*****************请选择功能*******************\n");
	printf("**********************************************\n");
	printf("*************1 录入进入停车场*****************\n");
	printf("**********************************************\n");
	printf("*************2 登记出车***********************\n");
	printf("**********************************************\n");
	printf("*************3 查看停车场信息*****************\n");
	printf("**********************************************\n");
	printf("*************4 查看候车场信息*****************\n");
	printf("**********************************************\n");
	printf("*************5 查看候车场一共有多少辆车*******\n");
	printf("**********************************************\n");
	printf("*************6 查看停留时间最长的车辆*********\n");
	printf("**********************************************\n");
	printf("*************7 查看出入记录*******************\n");
	printf("**********************************************\n");	
	printf("*************8 退出系统**********************\n");
}



int initpark(ssk **s)//初始化
{
	
	//给s分配空间
	(*s) = (ssk *)malloc(sizeof(ssk) * 1);
	
	if(NULL == (*s))
	{
		return FAILURE;
	}
	(*s)->top = -1;
	
	
	//给num分配空间
	(*s)->num = (int *)malloc(sizeof(int) * SIZE);
	
	if((*s)->num == NULL)
	{
		return FAILURE;
	}
	
	//给time1分配空间
	(*s)->time1 = (long int *)malloc(sizeof(long int) * SIZE);
	
	if((*s)->time1 == NULL)
	{
		return FAILURE;
	}
	
	//给time2分配空间
	(*s)->time2 = (long int *)malloc(sizeof(long int) * SIZE);
	
	if(NULL == (*s)->time2)
	{
		return FAILURE;
	}
	
	return SUCCESS;

}



//停车场遍历
int cartraver(ssk *s)
{
	
	//入参判断
	if(s == NULL)
	{
		return FAILURE;
	}
	
	//判断停车场是否有车
	if(s->top == -1)
	{
		printf("停车场里没有车!\n");
	}
	
	int i;
	
	for(i = 0;i <s->top + 1;i++)
	{
		printf("车牌号码%d  进场时间%s\n",s->num[i],ctime(&(s->time1[i])));
	}
	
	return SUCCESS;
	
}



//候车场遍历
int waittraver(ssw *w)
{
	
	//判断候车场是否有车
	if(w->rear == w->front)
	{
		printf("候车场没有车\n");
	}
	
	// 入参判断
	if( w == NULL)
	{
		return FAILURE;
	}
	
	int a;
	a = (w->rear - w->front + 100) % 100;
	
	int i;
	
	for(i =w->front;i < w->rear;i++)
	{
		
		printf("候车场的车辆信息为:\n");
		printf("车牌号码:%d 进场时间:%s\n",w->num[i],ctime(&(w->time1[i])));
		
	}
	
	return SUCCESS;
	
}


	

//往候车场里停车
int waiting(ssw *w,ssk *s)
{
	
	while(s->top >= SIZE - 1)
	{
		
		//入参判断
		if(NULL == w)
		{
			return FAILURE;
		}
		
		printf("请输入进入候车区车牌号码:\n");
		scanf("%d",&w->num[w->rear]);
		
		time_t t1;
		time (&t1);
		long int t = t1;            // 标记进入候车场的时间
		char* t2;
		t2 = ctime (&t1); 		// 获取当前时间
		w->time1[w->rear] = t;
		
		printf("车牌号为%d进入候车区,时间为%s\n",w->num[w->rear],t2);
		w->rear ++;
		
		int a;
		printf("是否结束输入? 1 是 2 否\n");
		scanf("%d",&a);
		
		if( a == 1)
		{
			break;
		}
		
		else if(a == 2)
		{
			continue;
		}
		
		else
		{
			printf("输入错误!请重新输入:\n");
		}
		return SUCCESS;
		
	}
}
	
	
//往停车场里 停车
int push(ssk *s)
{
	
	while(s->top < SIZE)
	{
		
		//入参判断
		if(NULL == s)
		{
			return FAILURE;
		}
		
		else if(s->top == SIZE - 1)
		{
		
			int h;
			printf("停车场已满,请等候!\n");
			printf("是否要登记车辆进入候车场?1 是 2 否\n");
			scanf("%d",&h);
			
			if(h == 1)
			{
				waiting(wait,car);
			}
			
			else 
			{
				break;
			}
			
		}
		
		else
		{
			printf("请输入进入停车场的车牌号码:\n");
			scanf("%d",&s->num[s->top + 1]);
			
			time_t t1;
			time (&t1);            // 标记进入停车场的时间
			char* t2;
			t2 = ctime (&t1); 		// 获取当前时间
			s->time1[s->top + 1] = t1;
			
			printf("牌照为%d的车进入,当前时间为%s\n",s->num[s->top + 1],t2);
			s->top++;
			
			int a;
			
			printf("是否继续输入进入停车场的车辆信息?\n");
			printf("   1 继续输入       2 返回主界面\n");
			
			scanf("%d",&a);
			
			if(a == 1)
			{
				continue;
			}
			
			else if (a == 2)
			{
				break;
			}
			
			else
			{
				printf("输入错误!\n");
			}
			
		
		}
		
	}
	
	//返会成功
	return SUCCESS;
}



int pop(ssk *s,ssk *e,ssw *w,srd *r)
{
	//出车
	int a;
	int p = 0;// 用来判断是否找到查找的车辆
	
	//将要离开停车场的车牌号
	printf("请输入出停车场的车牌号:\n");
	scanf("%d",&a);

	int k = 0;

	//当停车场内有车时进行循环
	while(s->top > -1)
	{
		
		// 入参判断
		if(NULL == s)
		{
			return FAILURE;
		}
		
		//入参判断
		if(e == NULL)
		{
			printf("候车场出现错误!");
		}
		
		// 找到要出去的车
		if(a == s->num[s->top])
		{
				
			if(s->top == -1)
			{
					return FAILURE;
			}

			time_t t1;      
			time (&t1); 
			
			// 标记离开停车场的时间
			s->time2[0] = t1;             
			char* t2;
			// 获取当前时间
			t2 = ctime (&t1);	
			
			srd *pp = r;
			srd *qq = (srd *)malloc(sizeof(srd)*1);
			
			if(qq == NULL)
			{
				return FAILURE;
				break;
			}
			
			qq->num = s->num[s->top];
			qq->time1 = s->time1[s->top];
			qq->time2 = s->time2[0];
			qq->time = (difftime(s->time2[0],s->time1[s->top]))/60;
			qq->next = pp->next;
			pp->next = qq;
			
			
			
			//计费
			int money;
			money = ((difftime(s->time2[0],s->time1[s->top]))/60 - 0.5) * price;
			
			//如果时间超过了30秒
			if ((difftime(s->time2[0],s->time1[s->top]))/60 > 0.5)
			{
				printf("车牌号:%d于 %s出场停留时间%.1f分钟\n",s->num[s->top],t2,(difftime(s->time2[0],s->time1[s->top]))/60);
				printf("时间超过了30秒;应收$%d\n",money);
			}
			
			//如果时间没有超过30秒
			else 
			{
				printf("车牌号:%d于 %s出场停留时间%.1f分钟\n",s->num[s->top],t2,(difftime(s->time2[0],s->time1[s->top]))/60);
				printf("时间没有超过30秒 不收费!!\n");
			}
			
			
			//top减1
			s->top --;
			p++;    //找到车辆 p值发生改变
			
			break;
			
		} 
		
		
		//把其余的车停到交换车场
		e->num[e->top + 1] = s->num[s->top];
		e->time1[e->top + 1] = s->time1[s->top];
		e->time2[e->top + 1] = s->time2[s->top];
		
		
		s->top --;
		e->top ++;
		k++;

		
	}
	
	
	if(p == 0)
	{
		printf("没有这辆车的信息!\n");
		
	}
		
		
	// 把交换车场里的车换回去
	while(e->top > -1) 
	{
		
		if(e == NULL)
		{
			return FAILURE;
		}
		
		s->num[s->top + 1] = e->num[e->top];
		s->time1[s->top + 1] = e->time1[e->top];
		s->time2[s->top + 1] = e->time2[e->top];
		
		e->top --;
		s->top ++;
		
		
	}
	
	//当停车场内未满时 从候车场停入车辆
	while(s->top <= 3)
	{
		
		//当停车场车辆已满
		if(s->top > 3)
		{
			return FAILURE;
			break;
		}
		
		//如果候车场没有车辆
		if(w->front == w->rear)
		{
			break;
		}
		
		//入参判断
		if(NULL == w)
		{
			return FAILURE;
		}
		
		//停车场进车
		int aa;
		printf("是否有车辆进入停车场? 1 是 2 否\n");
		scanf("%d",&aa);
		
		if( aa == 1)
		{
			//如果输入1 则开始录入进入停车场的车辆信息
			push(car);
		}
		
		else 
		{
			//如果没有车辆进入停车场,则继续进行循环,继续向下执行
			continue;
		}
			
		int c;
		
		//候车场车辆数量减 1
		c = w->num[w->front];
		w->front = (w->front + 1) % 100;
		
		x++;
		return SUCCESS;
	}
}



int waitlength(ssw *w)
{
	
	//入参判断
	if(NULL == w)
	{
		return FAILURE;
	}
	
	//如果候车场没有车辆
	if(w->front == w->rear)
	{
		printf("候车场没有车辆!\n");
	}
	
	int a;
	
	//查看候车场有多少车辆
	a = (w->rear - w->front + 100) % 100;
	printf("候车场一共有%d辆车!\n",a);
	
	return SUCCESS;
	
}


//出入记录 没有数量限制
int traverrecord(srd *r)
{
	
	if(r == NULL)
	{
		return FAILURE;
	}
	
	srd *qq = r;
	
	
	while(qq->next)
	{
		qq =qq->next;
		printf("车牌号码:%d\n",qq->num);
		printf("进场时间:%s\n",ctime(&(qq->time1)));
		printf("出场时间: %s\n",ctime(&(qq->time2)));
		printf("停留时间:%.2f分钟\n",qq->time);
		printf("**********************************\n");
	}
	
	return SUCCESS;
}

int longestcar(ssk *s)
{
	if(NULL == s)
	{
		return FAILURE;
	}
	if(s->top == -1)
	{
		return FAILURE;
	}
	time_t t1;      
	time (&t1); 
			
	// 标记离开停车场的时间
	s->time2[0] = t1;             
	char* t2;
	// 获取当前时间
	t2 = ctime (&t1);
	
	int i, j,k;
	float temp = 0;
	int flag = 0;
	float a[] = {0};

	
	if(s->top == 0)
	{
		temp = (difftime(s->time2[0],s->time1[s->top]))/60;
		
		printf("目前停留时间最长的是%d,停留时间为%.2f\n",s->num[s->top],temp);
		return SUCCESS;
	}
	
	if(s->top == 1)
	{
		
		float aa= (difftime(s->time2[0],s->time1[0]))/60;
		float bb= (difftime(s->time2[0],s->time1[1]))/60;
		
		if( aa > bb)
		{
			
			printf("目前停留时间最长的是%d,停留时间为%.2f\n",s->num[0],aa);
			
		}
		
		else
		{
			
			printf("目前停留时间最长的是%d,停留时间为%.2f\n",s->num[1],bb);
			
		}
		
		return SUCCESS;
	}
	if(s->top >= 2)
	{
		int c = s->top;
		
		for(i = 0;i <= c;i++)
		{
			a[i] = (difftime(s->time2[0],s->time1[i]))/60;
		}
		
		temp = a[0];
		
			for (j = 1; j <= c; j++)//从第二个开始依次进行比较,
			{                     //选出最小值得下标
									//把较小值的
				if (a[j] > temp)
				{
					temp = a[j];  //把较da值的值放到temp里
					flag = j;  //把较da值的下标放到flag里
				}
			}
		
		
	
		printf("目前停留时间最长的是%d,停留时间为%.2f\n",s->num[flag],temp);
		
		return SUCCESS;
		
	}
}

int main()
{

	//主函数
	
	//初始化是否成功
	ret = initpark(&car);
	if(ret == FAILURE)
	{
		printf("初始化停车场失败\n");
	}
	
	//初始化是否成功
	ret = initpark(&exchange);
	if(ret == FAILURE)
	{
		printf("初始化换车场失败\n");
	}
	
	
	//初始化执行是否成功
	ret = initwait(&wait);
	
	if(ret == FAILURE)
	{
		printf("初始化候车区失败\n");
	}
	
	ret = initrecord(&record);
	
	if(ret == FAILURE)
	{
		printf("初始化记录失败!\n");
	}
	
	else
	{
		printf("初始化记录成功!\n");
	}
	int a;
	
	// 清屏
	system("clear");
	
	//进行循环
	while(1)
	{
		welcome();
		
		printf("请选择功能:\n");
		scanf("%d",&a);
		
		switch(a)
		{
			
			//进车是否成功
			case 1:
			system("clear");
			ret = push(car);
			
			if(ret == FAILURE)
			{
				printf("录入信息失败!\n");
			}
			
			else
			{
			printf("录入信息成功!\n\n\n");
			}
			break;
			
			//出车是否成功
			case 2:
			system("clear");
			
			ret = pop(car,exchange,wait,record);
			if(ret == FAILURE)
			{
				printf("出车失败!\n");
			}
			
			else
			{
				printf("出车成功!\n\n\n");
			}
			break;
			
			//查看停车场车辆信息
			case 3:
			
			system("clear");
			ret = cartraver(car);
			
			if(ret == FAILURE)
			{
				printf("查看停车场信息失败!\n");
			}
			else
			{
				printf("查看停车场信息成功!\n\n\n");
			}
			break;
			
			//查看候车场车辆信息
			case 4:
			
			system("clear");
			ret = waittraver(wait);
			
			if(ret == FAILURE)
			{
				printf("查看候车场信息失败\n");
			}
			
			else
			{
				printf("查看候车场信息成功\n\n\n");
			}
			break;
			
			//退出系统
			case 5:
			
			system("clear");
			ret = waitlength(wait);
			
			if(ret == FAILURE)
			{
				printf("查看失败!\n");
			}
			
			else
			{
				printf("查看成功\n\n\n");
			}
			break;
			
			case 6:
			system("clear");
			ret = longestcar(car);
			
			if(ret == FAILURE)
			{
				printf("获取时间最长的车辆失败!\n");
			}
			
			else
			{
				printf("获取时间最长的车辆成功!\n");
			}
				
			break;
			case 7:
			
			ret = traverrecord(record);
			if(ret == SUCCESS)
			{
				printf("查看记录成功\n");
			}
			else
			{
				printf("查看记录失败\n");
			}
				break;
			case 8:
				system("clear");
				printf("退出系统成功!\n");
				sleep(1);
				exit(0);
				break;
		}
	}
	
		return 0;

}


int initwait (ssw **w);
void welcome();
int initpark (ssk **s);
int waittraver (ssw *w);
int waiting (ssw *w,ssk *s);
int push (ssk *s);
int pop (ssk *s,ssk *e,ssw *w,srd *r);
int waitlength(ssw *w);
int initrecord(srd **r);
int traverrecord(srd *r);







 

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/chenxinntu/article/details/81671250
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞