LinuxC练习:停车场项目:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门 外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中

基本功能要求:

          (1)建立三个数据结构分别是:停放栈、让路栈、等候队列。

          (2)输入数据模拟管理过程,数据(入或出,车号)

功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、

          查询场内车辆信息、查询等候车辆信息、退出系统。

          (1)linux系统编写(链表、栈、队列);

          (2)进车登记:登记车牌号以及入场时间;

          (3)出车登记:计算出停车时间,记录车辆车牌;

  (4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场

          (5)查询出入记录:所有车辆,包括已经离开的

          (6)查询场内车辆信息:列出所有场内车辆信息

  (7)查询等候车辆信息:显示等候车辆数量以及所有车牌号

  (8)退出系统。

Car.h

#ifndef _CAR_H_
#define _CAR_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
enum
{
	SUCCESS = 10000,
	FAILURE,
	TRUE,
	FALSE,
};

struct carinfo
{
	char *carnum;

	char *entertime;
	char *outtime;

	long int enterseconds;
	long int outseconds;

	int enterflag;
	int outflag; 
	int waitflag;

	int fee;

	struct carinfo *next;

};

typedef struct carinfo CarInfo;

struct carstack
{
	CarInfo *top;
	int length;
};
typedef struct carstack CarStack;

struct carqueue
{
	CarInfo *front;
	CarInfo *rear;
};
typedef struct carqueue CarQueue;

int Welcome();
int Login();
int CarMenue();
int CheckCarnumStack(CarStack *cs1, char *carnum);
int CheckCarnumQueue(CarQueue *cq, char *carnum);
int ShowByCarnum(CarStack *s, CarStack *s1, CarQueue *q, void(p)(CarInfo *));
int InitCarStack(CarStack **s);
int Push(CarStack *s, CarInfo *c);
CarInfo *Pop(CarStack *s);
int EnterCarPark(CarStack *s, CarQueue *q);
int LeaveCarPark(CarStack *s, CarStack *s1,CarStack *s2, CarQueue *q);
int TraverseCarStack(CarStack *s, void (*p)(CarInfo *));
int EmptyCarStack(CarStack *s);
int InitCarQueue(CarQueue **q);
int EnterCarQueue(CarQueue *q, CarStack *s);
int EmptyCarQueue(CarQueue *q);
int LengthCarQueue(CarQueue *q);
CarInfo *DeleteCarQueue(CarQueue *q);
int TraverseCarQueue(CarQueue *q, void (*p)(CarInfo *));

#endif
Car.c
#include "Car.h"

int Welcome()
{
	system("clear");
	printf("\t\t\t************************************************************\n");
	printf("\t\t\t------------------------------------------------------------\n");
	printf("\t\t\t****************Welcome To  CarPark  System*****************\n");
	printf("\t\t\t------------------------------------------------------------\n");
	printf("\t\t\t*******************Made By Autumn Sun***********************\n");
	printf("\t\t\t------------------------------------------------------------\n");
	printf("\t\t\t************************************************************\n");
	sleep(2);
	return 0;
}

int Login()
{
	system("clear");
	char *LoginName = (char *)malloc(sizeof(char) * 20);
	char *LoginPassword = (char *)malloc(sizeof(char) * 20);
	if(NULL == LoginName || NULL == LoginPassword)
	{
		printf("NULL ERROR\n");
		exit(1);
	}
	int ret;
	printf("请输入登录名\n");
	scanf("%s", LoginName);
	printf("请输入登录密码\n");
	scanf("%s", LoginPassword);
	ret = strcmp(LoginName, LoginPassword);
	if(ret != 0)
	{
		return FAILURE;
	}
	else
	{
		return SUCCESS;
	}

}



int CarMenue()
{
	printf("\t\t\t1、显示车库             2、显示等候\n");
	printf("\t\t\t3、进入车库             4、离开车库\n");
	printf("\t\t\t5、查询所有             6、推出系统\n");
	return 0;
}

int CheckCarnumStack(CarStack *cs1, char *carnum)
{
	int flag = 0;
	if(NULL == cs1)
	{
		return FAILURE;
	}

	CarInfo *q = cs1 -> top;
	while(q)
	{
		if(0 == strcmp(q -> carnum, carnum))
		{
			flag = 1;
		}
		q = q ->next;
	}
	

	if(flag == 0)
	{
		return FALSE;
	}
	else
	{
		return TRUE;
	}


}

int CheckCarnumQueue(CarQueue *cq, char *carnum)
{
	if(NULL == cq)
 	{
     	return FAILURE;
 	}
	int flag = 0;
	CarInfo *q1 = cq -> front -> next;
	while(q1)
	{
		if(0 == strcmp(q1 -> carnum, carnum))
		{
			flag = 1;
		}
		q1 = q1 -> next;
	}
	if(flag == 0)
	{
		return FALSE;
	}
	else
	{
		return TRUE;
	}


}



int ShowByCarnum(CarStack *s, CarStack *s1, CarQueue *q, void(p)(CarInfo *))
{
	CarInfo *tmp1 = s -> top;
	CarInfo *tmp2 = s1 -> top;
	CarInfo *tmp3 = q -> front -> next;
	char *carnum = (char *)malloc(sizeof(char) * 20);
	if(NULL == carnum)
	{
		printf("NULL ERROR\n");
		return FAILURE;
	}
	printf("请输入要离开的车牌号\n");
	scanf("%s", carnum);
	while(tmp1)
	{
		if(strcmp(tmp1 -> carnum, carnum) == 0)
		{
			printf(" 状态:在停车场\n");
			p(tmp1);

		}
		tmp1 = tmp1 -> next;
	}

	while(tmp2)
	{
		if(strcmp(tmp2 -> carnum, carnum) == 0)
		{
			printf("状态:已经离开\n");
			p(tmp2);
		}
		tmp2 = tmp2 -> next;
	}

	while(tmp3)
	{
		if(strcmp(tmp3 -> carnum, carnum) == 0)
		{
			printf("状态:正在等候\n");
			p(tmp3);
		}
		tmp3 = tmp3 -> next;
	}
	return SUCCESS;
}

CarQueue.c
#include "Car.h"

int InitCarQueue(CarQueue **q)//初始化等候区
{
	if (NULL == q)
	{
		return FAILURE;
	}

	(*q) = (CarQueue *)malloc(sizeof(CarQueue));
	if ((*q) == NULL)
	{
		return FAILURE;
	}

	CarInfo *n = (CarInfo *)malloc(sizeof(CarInfo));
	if (NULL == n)
	{
		return FAILURE;
	}

	n->next = NULL;

	(*q)->rear = (*q)->front = n;

	return SUCCESS;
		
}

int EnterCarQueue(CarQueue *q, CarStack *s)// 将车辆进入等候区
{
	if (NULL == q)
	{
		return FAILURE;
	}
	
	int ret, ret1;
	CarInfo *n = (CarInfo *)malloc(sizeof(CarInfo));
	n -> carnum = (char *)malloc(sizeof(char) * 10);
	n -> entertime = (char *)malloc(sizeof(char) * 20);
	n -> outtime = (char *)malloc(sizeof(char) * 20);
	n -> waitflag = 1;
	char *carnum = (char *)malloc(sizeof(char) * 10);
	if (NULL == n || NULL == n -> carnum || NULL == n -> entertime || NULL == n -> outtime || NULL == carnum)
	{
		return FAILURE;
	}

	printf("请输入车牌号\n");
	scanf("%s", carnum);
	ret = CheckCarnumStack(s, carnum);
	ret1 = CheckCarnumQueue(q, carnum);
	while(TRUE == ret || TRUE == ret1)
	{
		printf("请重新输入车牌号\n");
		memset(carnum, 0, 10);
		scanf("%s", carnum);
		ret = CheckCarnumStack(s, carnum);
		ret1 = CheckCarnumQueue(q, carnum);

	}
	strcpy(n ->carnum, carnum);
	strcpy(n -> entertime, " ");
	strcpy(n -> outtime, " ");
	n -> fee = 0;
	n -> enterflag = 0;
	n -> outflag = 0;
	n->next = NULL;
	q->rear->next = n;
	q->rear = n;

	return SUCCESS;
}

int EmptyCarQueue(CarQueue *q)//判断等候区是否有车
{
	if (NULL == q)
	{
		return FAILURE;
	}

	return (q->rear == q->front) ? TRUE : FALSE; 
}

int LengthCarQueue(CarQueue *q)//求得等候区车辆数
{
	if (NULL == q)
	{
		return FAILURE;
	}

	CarInfo *p = q->front;
	int length = 0;

	while (p != q->rear)
	{
		p = p->next;
		length++;
	}

	return length;
}
//将车从等候区出去,返回车辆信息
CarInfo *DeleteCarQueue(CarQueue *q)
{
	if (NULL == q || q->rear == q->front)
	{	
		return NULL;
	}

	CarInfo *p = q->front->next;
	q->front->next = p->next;
		
	if (p == q->rear)
	{
		q->rear = q->front;
	}
	
	return p;
}

int TraverseCarQueue(CarQueue *q, void (*p)(CarInfo *))//现实等候区所有车辆
{
	if (NULL == q || p == NULL)
	{
		return FAILURE;
	}
	
	int length = LengthCarQueue(q);
	printf("目前有%d在等候区\n", length);
	
	CarInfo *n = q->front->next;
	while (n)
	{
		p(n);
		n = n->next;
	}
	
	return SUCCESS;
}

CarStack.c
#include "Car.h"

int InitCarStack(CarStack **s)//初始化车库
{
	*s = (CarStack *)malloc(sizeof(CarStack));
	if (NULL == (*s))
	{
		return FAILURE;
	}

	(*s)->top = NULL;
	(*s)->length = 0;	

	return SUCCESS;
}

int Push(CarStack *s, CarInfo *c)
{
	if(NULL == s)
	{
		return FAILURE;
	}

	c -> next = s -> top;
	s -> top = c;
	s -> length ++;
	return SUCCESS;
}

CarInfo *Pop(CarStack *s)
{
	if(NULL == s || NULL == s -> top)
	{
		return NULL;
	}
	
	CarInfo *c = s -> top;
	s -> top = c -> next;
	s -> length --;
	
	//tmp = c;
	//tmp -> next = NULL; 
	return c;
}


int EnterCarPark(CarStack *s, CarQueue *q)//将车牌号添加进车库
{
	if (NULL == s || NULL == q)
	{
		return FAILURE;
	}

	CarInfo *p = (CarInfo *)malloc(sizeof(CarInfo));
	p -> carnum = (char *)malloc(sizeof(char) * 10);
	p -> entertime = (char *)malloc(sizeof(char) * 20);
	p -> outtime = (char *)malloc(sizeof(char) * 20);
	char *carnum = (char *)malloc(sizeof(char) * 10);
	if (NULL == p|| NULL == p -> carnum || NULL == p -> entertime || NULL == p -> outtime || NULL == carnum)
	{
		return FAILURE;
	}
	int ret, ret1;
	//获取时间
	time_t timep;

	if(s -> length > 5)//如果车库满了则将车转到等候区
	{
		printf("停车场已经满了,请进入等候区\n");
		ret = EnterCarQueue(q, s);
		if(SUCCESS == ret)
		{
			printf("进入等候区成功\n");
		}
		else
		{
			printf("进入等候区失败\n");
		}
	}
	else if(s -> length <= 5 && EmptyCarQueue(q) == TRUE)//如果等候区没有车且车库没有满
	{
		printf("请输入车牌号\n");
		scanf("%s", carnum);
		
		ret = CheckCarnumStack(s, carnum);
		ret1 = CheckCarnumQueue(q, carnum);
		while(TRUE == ret || TRUE == ret1)
		{
			system("clear");
			printf("请重新输入车牌号\n");
			memset(carnum, 0, 10);
			scanf("%s", carnum);
			ret = CheckCarnumStack(s, carnum);
			ret1 = CheckCarnumQueue(q, carnum);

		}
		strcpy(p -> carnum, carnum);
		p -> outseconds = 0;
		strcpy(p -> outtime, " ");
		p -> fee = 0;
		p -> enterseconds = time(&timep);//获取描述
		strcpy(p -> entertime, ctime(&timep));
		p -> enterflag = 1;
		p -> outflag = 0;
		p -> waitflag  =1;
		printf("入场时间:%s\n", p -> entertime);
		Push(s, p);		
	}
	return SUCCESS;
}

						                     			 //出车时如果等候区有车则将等候区的第一辆车进入到车库中、length不变
  						                       //如果等候区没有车,则length --;
int LeaveCarPark(CarStack *s, CarStack *s1,CarStack *s2, CarQueue *q)//s为停车场,s1为方便停车的区域, s2为记录离开的车子
{
	char *carnum = (char *)malloc(sizeof(char) * 10);
	CarInfo *tmp ;
	CarInfo *tmp1;
	CarInfo *tmp2;
	CarInfo *tmp3;
	CarInfo *tmp4;
	int ret, ret2;

	if (NULL == s || NULL == s->top || NULL == carnum)
	{
		return FAILURE;
	}
	printf("请输入要离开的车牌号\n");
	scanf("%s", carnum);
	ret = CheckCarnumStack(s, carnum);
	while(FALSE == ret)
	{
		printf("请重新输入\n");
		memset(carnum, 0, 10);
		scanf("%s", carnum);
		ret = CheckCarnumStack(s, carnum);
		 
	}
	tmp = s -> top;
	while(tmp)
	{
		 
		 if(strcmp(tmp -> carnum, carnum) == 0)
		 {
		 	tmp1 = tmp;
			break;
		 }
		 tmp4 = Pop(s);
		 if(NULL == tmp4)
		 break;
		 Push(s1, tmp4);
		 
		 tmp = s -> top;
	}
	
	time_t timep;
	time(&timep);
	tmp1 -> outseconds = time(&timep);
	strcpy(tmp1 -> outtime, ctime(&timep));
	tmp1 -> fee = (tmp1 -> outseconds - tmp1 -> enterseconds) * 1;//一分钟一元
	tmp1 -> outflag = 1;
	tmp1 -> enterflag = 0;
	printf("应缴费用:%d\n", tmp1 -> fee);
	printf("离开时间:%s\n", tmp1 -> outtime);
	Pop(s);
	Push(s2, tmp1);
	tmp2 = s1 -> top;
	while(tmp2)
	{
		tmp4 = Pop(s1);
		if(NULL == tmp4)
		break;
		Push(s, tmp4);
		tmp2 = s1 -> top;
	}
	//如果等候区有车将第一辆车开进停车场
	if(FALSE == EmptyCarQueue(q))
	{
		tmp3 = DeleteCarQueue(q);
		
		if(s -> length <= 5)
		{
			
			tmp3 -> enterseconds = time(&timep);
			strcpy(tmp3 -> entertime, ctime(&timep));
			tmp3 -> enterflag = 1;
			tmp3 -> waitflag = 0;
			
			ret2 = Push(s, tmp3);
			if(SUCCESS == ret2)
			{
				printf("等候区车牌%s正在进入停车场\n", tmp3 -> carnum);
				printf("入场时间:%s\n", tmp3 -> entertime);
				printf("从等候区成功进入停车场\n");
				return SUCCESS;
			}
			else
			{
				return FAILURE;
			}
		}	
	}
	return SUCCESS;
}


int TraverseCarStack(CarStack *s, void (*p)(CarInfo *))//显示所有停车场车辆的信息
{
	if (NULL == s)
	{
		printf("1\n");
		return FAILURE;
		
	}
	//printf("2\n");
	printf("目前停车场有%d正在使用\n", s -> length);
	//printf("1\n");
	CarInfo *q = s->top;
	while (q)
	{
		p(q);
		q = q->next;
	}
	while(getchar() != '\n');
	return SUCCESS;

}


int EmptyCarStack(CarStack *s)//判断停车场是否为空
{
	if (NULL == s)
	{
		return FAILURE;
	}

	return (NULL == s->top) ? TRUE : FALSE;
}

test.c
#include"Car.h"

void visit(CarInfo *c)
{
	printf("\t\t\t车牌号:%s进库时间:%s出库时间:%s应缴费用:%d\n", c -> carnum, c -> entertime, c -> outtime, c -> fee);
	printf("\t\t\t--------------------------------------------------------------------\n");
	printf("\t\t\t--------------------------------------------------------------------\n");
}

int main()
{
	int ret, choice;
	CarStack *cs1;//车库
	CarStack *cs2;//用于临时存放
	CarStack *cs3;//用于存放离开的车辆信息
	CarQueue *cq;
	if(FAILURE == InitCarStack(&cs1) || FAILURE == InitCarStack(&cs2) || FAILURE == InitCarStack(&cs3) || FAILURE == InitCarQueue(&cq))
	{
		printf("Init Failure\n");
		exit(1);
	}
	else
	{
		Welcome();
		ret = Login();
		while( FAILURE == ret)
		{
			system("clear");
			printf("请重新登录\n");
			ret = Login();
		}
		
		while(1)
		{
			CarMenue();
			printf("\n");
			printf("请输入选项\n");
			scanf("%d", &choice);
			system("clear");
			switch(choice)
			{
				case 1: TraverseCarStack(cs1, visit);
						break;
				case 2: TraverseCarQueue(cq, visit);
						break;
				case 3: EnterCarPark(cs1, cq);
						break;
				case 4:	LeaveCarPark(cs1, cs2, cs3, cq);
						break;
				case 5: ShowByCarnum(cs1, cs3, cq, visit);
						break;
				case 6:	exit(1);
						break;
				default: printf("1111\n");
						return 0;

			}
		}
	}


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