用C编写的一个小型停车场项目

项目要求:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后依次进入。汽车离开时按停放时间收费。 基本功能要求: 1)建立三个数据结构分别是:停放队列,让路栈,等候队列 2)输入数据模拟管理过程,数据(入或出,车号)。
#ifndef __PARK_H__
#define __PARK_H__

#define FALSE 0
#define TRUE  1

#define SIZE   20
#define CAR_SIZE  10

//创建三个结构体 停放栈  让路栈  等候队列

//车辆信息
typedef  struct _car 
{
	char ID[SIZE];     //存放车牌号
	time_t intime;     //进栈时间
	time_t outtime;    //出栈时间 
 	float  money;      //停车费用 (1秒0.1元)
	
}car;

//停放栈
typedef  struct _park
{
	car num1[CAR_SIZE];    //存放车辆信息
	int top1;          //栈头元素下标
}park;

//让路栈
typedef  struct _wait
{
	car num2[SIZE];
	int top2;
}wait;

//等候队列
typedef  struct _queue
{
	car data;
	struct _queue *next;
}Queue;

typedef  struct _linkqueue
{     
	Queue *front;    //对头指针
	Queue *rear;     //队尾指针
}LinkQueue;
//停车场的状态
int func(park *s, LinkQueue *q); 
// 置空栈
int InitStack (park *s);
//判断是否空栈
int EmptyStack(park *s);
//判断是否满栈
int FullStack(park *s);
//置空让路栈
int  initstack(wait *s);
//判断让路栈是否满栈
int fullstack(wait *s);
//判断让路栈是否空栈
int  emptystack(wait *s);
//创建队列
LinkQueue *Creat_Queue();
//判断队空
int EmptyQueue(LinkQueue *q);
//进队
int InQueue(LinkQueue *q,car *x);
//出队进停车栈
int OutQueue(LinkQueue *q, car *x, park *s);
//有车进停车栈
int Push(park *s, car *x, LinkQueue *p);
//有队列的车进停车栈
//int  queuepush(park *s , LinkQueue *q);
//有车出栈
int popstack(park *s, car *x);
//让路栈出栈
int pop1stack(wait *p ,park *s);
//让路栈进栈
int  push1stack(wait *p ,park *s);
void menu(park *s, LinkQueue *q);
//查看停车场情况
int display(park *s);
//参看等候队列情况
int  voidplay(LinkQueue *q);
//出停放栈
int pop(park *s,car *x, wait *p ,LinkQueue *q);
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "Park.h"
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int func(park *s, LinkQueue *q)
{
	int i = 0;
	if(s == NULL)
		return FALSE;
	if(q == NULL)
		return FALSE;
	Queue *p = q->front;
	while(p)
	{
		i++;
		p = p->next;
	}
	printf("\t停车场共有6个车位,目前停车场共有%d辆车,等候区共有%d辆\n",s->top1+1, i);
	return TRUE;
}
void menu(park *s, LinkQueue *q)
{
	printf("\t******************停车场目前的状态*****************~\n");
	func(s, q);
	printf("\t****************************************************\n");
	printf("\t~               1 >>>>>>>>停车                     ~\n");
	printf("\t~               2 >>>>>>>>离开                     ~\n");
	printf("\t~               3 >>>>>>>>查看停车场情况           ~\n");
	printf("\t~               4 >>>>>>>>查看等候车道情况         ~\n");
	printf("\t~               5 >>>>>>>>退出                     ~\n");
	printf("\t~                                                  ~\n");
	printf("\t~                                     作者:倪鹏程 ~\n");
	printf("\t***************************************************~\n");
	printf("\t请按对应数字提示完成对应操作                       ~\n");
	printf("\t                                   收费标准:0.1元/秒\n");
}


//查看停车场情况
int display(park *s)
{
	if(s == NULL)
		return FALSE;
	
	int i = 0;
	if(s->top1 == -1)
	{
		printf("停车场中没有车\n");
	}
	time_t curtime;
	time(&curtime);
	
	while(i <= s->top1)
	{
		int len = curtime - s->num1[i].intime;
		s->num1[s->top1].money = 0.1 * len; 
		printf("%d号车位 车牌号为%s 停车时间%s",
		i+1,s->num1[i].ID,ctime(&s->num1[i].intime));
		printf("停车时长%d秒 需支付%.1f元\n", len,s->num1[s->top1].money);
		printf("*****************************************\n");
		i++;
	}
	return TRUE;	
}
//参看等候队列情况
int  voidplay(LinkQueue *q)
{
	int i = 1;
	Queue *p = q->front;
	
	if(q == NULL)
	{
		return FALSE;
	}
	if(EmptyQueue(q))
	{
		printf("等候队列中没有车辆信息\n");
		return FALSE;
		
	}
	time_t pretime;
	time(&pretime);
	while(p != NULL)
	{
		int len = pretime - p->data.intime;
		printf("———————————————————————————————————————————————————\n");
		printf("车牌号%s 停在了等候车道的%d号车位\n", p->data.ID, i++);
		printf("停车时间%s\n",ctime(&p->data.intime));
		printf("已经等候了 %d秒\n",len);
		printf("———————————————————————————————————————————————————\n");
		p = p->next;
	}
	
	return 0;
}
//置空停放栈
int InitStack(park *s)
{
	if(s == NULL)
		return FALSE;
	
	s->top1 = -1;
	return  TRUE;
}
//判断是否空栈
int EmptyStack(park *s)
{
	if(s == NULL)
		return FALSE;
	
	return s->top1 == -1;
}
//判断是否满栈
int FullStack(park *s)
{
	if(s == NULL)
		return FALSE;
	
	return s->top1 == (CAR_SIZE - 1);
}
//置空让路栈
int  initstack(wait *s)
{
	if(s == NULL)
		return FALSE;
	s->top2 = -1;
	return TRUE;
}      
//判断让路栈是否满栈
int fullstack(wait *s)
{
	if(s == NULL)
		return FALSE;
	
	return s->top2 == SIZE - 1;
}
//判断让路栈是否空栈
int  emptystack(wait *s)
{
	if(s == NULL)
		return FALSE;
	
	return s->top2 == -1;
}
//创建队列
LinkQueue *Creat_Queue()
{
	LinkQueue *q = (LinkQueue*)malloc(sizeof(LinkQueue));
	if(q == NULL)
		return FALSE;
	//置空队
	q->front = NULL;
	q->rear = NULL;
	
	return q;
}
//判断队空
int EmptyQueue(LinkQueue *q)
{
	if(q == NULL)
		return FALSE;
	
	return q->front == NULL; 
}
//进队
int InQueue(LinkQueue *q ,car *x)
{
	if(q == NULL)
		return FALSE;
	Queue *node =(Queue *)malloc(sizeof(Queue));
    if(node == NULL)
		return FALSE;
	
	time(&x->intime);
	node->next = NULL;
	node->data = *x;
	
	if(q->front == NULL)
	{
		q->front = node;
		q->rear  = node;
		printf("您的车已进入等候车道\n");
	}
	else
	{
		q->rear->next = node;
		q->rear = node;
		printf("您的车已进入等候车道\n");
	}
	
	return TRUE;
}
//有车进停车栈
int Push(park *s, car *x, LinkQueue *p)
{
	if(s == NULL)
		return FALSE;
	char a;
	if(FullStack(s))
	{
		printf("停车位已满,是否先转到等候队列\n");
		printf("Y/y   or    N/n\n");
		printf("请输入:");
		getchar();
		scanf("%c",&a);
		
		if(a == 'Y' || a == 'y')
		{
			InQueue(p, x);	
			return TRUE;
		}
		else if(a == 'N' || a == 'n')
		{
			printf("您已放弃本次停车。\n");
			return TRUE;
		}	
	}
	else
	{
		s->top1++;
		
	    //x->intime = time(NULL);
		
		s->num1[s->top1] = *x;	
		time(&s->num1[s->top1].intime);
		printf("车牌号:%s     成功停入第%d个停车位。\n", s->num1[s->top1].ID,s->top1+1);
		printf("当前时间为 %s\n",ctime(&s->num1[s->top1].intime));
	}
	
	return TRUE;
}

//出队进停车栈	
int OutQueue(LinkQueue *q, car *x,park *s)
{
	if(q == NULL)
		return FALSE;
	
	if(EmptyQueue(q))
		return FALSE;
	
	if(s == NULL)
		return FALSE;
	
	
	Queue *p = q->front;
	*x = p->data;
	
	q->front = p->next;
	free(p);
	p = NULL;
	if(q->front == NULL)
	{
		q->rear = NULL;
	}
	Push(s, x, q);
	printf("等候车道的车已成功进入停入到停车栈中!\n");
	return TRUE;
}

//出栈
int popstack(park *s, car *x)
{
	if(s == NULL)
		return FALSE;
	if(EmptyStack(s))
	{
		printf("停车场中没有车!\n");
		return FALSE;
	}
	time(&s->num1[s->top1].outtime);
	*x = s->num1[s->top1];
	
	
	printf("车牌号为%s的车,在%s成功从第%d个停车位离开停车场!\n",
	s->num1[s->top1].ID,ctime(&s->num1[s->top1].outtime),s->top1+1);
	printf("欢迎下次再来我的停车场,祝您一路顺风!\n");
	s->top1--;
	return TRUE;
}

//让路栈进栈
int  push1stack(wait *p ,park *s)
{
	if(p == NULL)
		return FALSE;
	if(fullstack(p))
		return FALSE;
	if(EmptyStack(s))
		return FALSE;
	p->top2++;
	p->num2[p->top2] = s->num1[s->top1];
	s->top1--;
	
	return TRUE;
}
//让路栈出栈
int pop1stack(wait *p ,park *s)
{
	if(p == NULL)
		return FALSE;
	if(emptystack(p))
		return FALSE;
	s->top1++;
	s->num1[s->top1] = p->num2[p->top2];
	p->top2--;
	return TRUE;
}
//出停放栈
int pop(park *s,car *x, wait *p ,LinkQueue *q)
{
	if(s == NULL)
		return FALSE;
	if(p == NULL)
		return FALSE;
	if(EmptyStack(s))
	{
		printf("停车场中没有车\n");
		return FALSE;
	} 
	if(FullStack(s))
	{
		int flag = 0;
		char a[20];
		int i = s->top1;
		printf("请输入你要离开的车牌号:");
		scanf("%s", a); 
		printf("\n");		
		while(s->top1 >= 0)
		{
			if(strcmp(s->num1[s->top1].ID, a) == 0)
			{
				popstack(s , x);
				flag = 1;
				break;
			}
			else
			{
				push1stack(p , s);
			}
		}
		if(flag != 1)
		{
			printf("停车场中没有此车牌号车的信息!\n");
		}
		if(flag == 1)
		{
			printf("——————————————————————————————\n");
			if(EmptyQueue(q))
			{
				return;
			}
			else
			{
				OutQueue(q, x, s);
			}				
			printf("——————————————————————————————\n");
			while(s->top1 < i)      //把摆放在让路栈的车回到停放栈中
			{            
				pop1stack(p, s);
			}
		}	
	}
	else
	{
		popstack(s , x);
	}
	return TRUE;
}

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



int main()
{
	int num;    //用于case
	int num2;  //用车输入要离开的车牌号

	car *x = (car *)malloc(sizeof(car));
	if(x == NULL)
		return FALSE;
	park *p = (park *)malloc(sizeof(park));
	if(p == NULL)
		return FALSE;
	wait *w = (wait*)malloc(sizeof(wait));
	if(w == NULL)
		return FALSE;
	LinkQueue *q = Creat_Queue();   //创建等候队列
	if(q == NULL)
		return FALSE;
	InitStack (p);    //置空停车栈
	initstack (w);    //置空让路栈
	while(1)
	{
		system("clear");
		menu(p, q);
		printf("请按功能输入数字:");
		scanf("%d", &num);
		switch(num)
		{
			case 1:
			{
				system("clear");			
				printf("请输出您的车牌号:");
				scanf("%s", x->ID);
				printf("\n");
				Push(p, x, q);
				
				getchar();
				printf("请按回车键返回主菜单!");
				getchar();
				break;
			}
			case 2:
			{
				system("clear");
				pop(p, x, w ,q);
				getchar();
				printf("请按回车键返回主菜单!");
				getchar();
				break;
			}
			case 3:
			{	
				system("clear");
				display(p);
				getchar();
				printf("请按回车键返回主菜单!");
				getchar();
				break;
			}
			case 4:
			{
				system("clear");
				voidplay(q);
				getchar();
				printf("请按回车键返回主菜单!");
				getchar();
				break;
			}
			case 5:
			{
				system("clear");
				exit(0);
				break;
			}
			default :
			{
				system("clear");
				getchar();
				printf("请严格按照数字选择对应操作\n");
				printf("请按回车键返回主菜单!");
				getchar();
				break;
			}
		}	
	}
	return 0;
}



  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个简单的停车场管理系统的C语言程序,供您参考。 程序包含4个主要模块: 1. 定义结构体表示车辆信息 ``` struct car { char license[10]; // 车牌号 int parktime; // 停车时间 int spot; // 车位号 }; ``` 2. 定义全局变量和数组表示停车场信息 ``` int empty = 100; // 停车场空余车位数 struct car park[100]; // 停车场车辆信息数组 ``` 3. 定义函数实现停车场管理操作 ``` // 分配车位 void assign_spot(char *license) { if (empty == 0) { printf("对不起,没有空余车位!\n"); return; } for (int i = 0; i < 100; i++) { if (park[i].license[0] == '\0') { park[i].spot = i + 1; strcpy(park[i].license, license); park[i].parktime = time(NULL); empty--; printf("车辆已成功停放在车位 %d!\n", i + 1); return; } } } // 查询车辆信息 void get_car_info(int spot) { if (park[spot - 1].license[0] == '\0') { printf("该车位没有车辆停放!\n"); return; } printf("车牌号:%s\n", park[spot - 1].license); printf("停车时间:%s\n", ctime(&park[spot - 1].parktime)); } // 释放车位 void release_spot(int spot) { if (park[spot - 1].license[0] == '\0') { printf("该车位没有车辆停放!\n"); return; } printf("车辆已成功离开车位 %d!\n", spot); park[spot - 1].license[0] = '\0'; empty++; } ``` 4. 主函数调用以上函数实现用户交互 ``` int main() { int choice, spot; char license[10]; while (1) { printf("请选择要执行的操作:\n"); printf("1. 分配车位\n"); printf("2. 查询车辆信息\n"); printf("3. 释放车位\n"); printf("4. 退出程序\n"); scanf("%d", &choice); switch (choice) { case 1: printf("请输入车牌号:"); scanf("%s", license); assign_spot(license); break; case 2: printf("请输入要查询的车位号:"); scanf("%d", &spot); get_car_info(spot); break; case 3: printf("请输入要释放的车位号:"); scanf("%d", &spot); release_spot(spot); break; case 4: printf("程序已退出!\n"); exit(0); default: printf("输入有误,请重新输入!\n"); break; } } return 0; } ``` 以上是一个简单的停车场管理系统的C语言程序,您可以根据实际需求进行修改和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值