思路:
车辆停车功能:
车辆离开功能:
系统退出:
无内存泄漏:
封装成库:
容错性:
创新点:
Parking.h
#ifndef __PLOT_H__
#define __PLOT_H__
#define Price 1 // 单价可以自己定义n
#define MAX_STOP 5
#define MAX_PAVE 10
#include <stdlib.h>
#include <stdio.h>
#include <time.h> // 包含时间函数的头文件
#include <string.h>
// 汽车信息
typedef struct
{
int timeIn;// 进入停车场时间
int timeOut;// 离开停车场时间
char plate[10];// 汽车牌照号码,定义一个字符指针类型
} Car;
// 停放栈(用于停放车辆)
typedef struct
{
Car Stop[MAX_STOP];// 用于停放车辆的栈
int top;// 标记栈顶位置
} Stopping;
// 等候队列
typedef struct
{
int count;// 用来指示队中的数据个数
Car Pave[MAX_PAVE];// 等候停车的队列
int front, rear,front1;// 标记队头和队尾位置
} Pavement;
// 让路栈
typedef struct
{
Car Help[MAX_STOP];// 用于让路的队列
int top;// 标记站定位置
} Buffer;
Stopping s;
Pavement p;
Buffer b;
Car c;
char C[50];
void stop_to_pave(); // 车停入便道
void car_come (); // 车停入停车位
void stop_to_buff(); // 车进入让路栈
void car_leave (); // 车离开
void welcome (); // 主界面函数
void Display (); // 显示车辆信息
void Choose ();//tigongxuanze
#endif //__PLOT_H__
Parking.c
#include "Parking.h"
/************************************************
function:车停入便道
判断 1:队满
输出:便道已满,请下次再来
判断 2:不满
输出:牌照为%s的汽车停入便道上的%d的位置
*************************************************/
void stop_to_pave() // 车停入便道
{
// 判断队满
if (p.count > 0 && (p.front == (p.rear) % (MAX_PAVE)))
{
printf ("便道已满,请下次再来\n");
}
else
{
strcpy(p.Pave[p.rear].plate, C); // 车进入便道
p.rear = (p.rear+1) % (MAX_PAVE); // 队尾指示器加1
p.count++; // 计数器加1
printf ("牌照为%s的汽车停入便道上的%d的位置\n", C, p.count);
}
}
/************************************************
function:车停入停车位
1:输入车牌号
2:判断
(1)如果停车位已满,停入便道
(2)如果停车位已满,停入便道
3:输出目前停车场状况
停车场共有几个车位,当前停车场共有几辆车,等候区共有几辆车
*************************************************/
void car_come() // 车停入停车位
{
printf ("请输入即将停车的车牌号:"); // 输入车牌号
scanf ("%s", C);
if (s.top >= MAX_STOP - 1) // 如果停车位已满,停入便道
{
printf("停车场已无空车位!\n\n");
printf ("\t*******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear - p.front)% (MAX_PAVE+1));
printf ("\t********************************************************\n");
stop_to_pave(); // 停入便道
}
else
{s.top++; // 停车位栈顶指针加1
time_t t1;
long int t = time(&t1); // 记录进入停车场的时间
char* t2;
t2 = ctime (&t1); // 将当前时间转换为字符串
s.Stop[s.top].timeIn = t;
strcpy(s.Stop[s.top].plate, C); // 将车牌号登记
printf ("牌照为%s的汽车停入停车位的%d车位\n当前时间:%s\n", C, s.top + 1, t2);
}
}
/************************************************
function:车进入让路栈
1:停车位栈压入临时栈,为需要出栈的车辆让出道
2:让出的车进入让路栈
3:如果停车位中的车都让了道,说明停车位中无车辆需要出
输出:停车位上无此车消息
否则
(1)输出:牌照为的汽车从停车场开走
(2)标记离开停车场的时间
(3)获取当前时间
(4)离开时间需付的钱
4:将让路栈中的车辆信息压入停车位栈
牌照为**的汽车停回停车位**车位
5: 从便道中 -> 停车位
判断队列是否为空
(1) 不为空,将便道中优先级高的车停入停车位
(2) 为空,输出牌照为%s的汽车从便道中进入停车位的%d车位
*************************************************/
void stop_to_buff() // 车进入让路栈
{
// 停车位栈压入临时栈,为需要出栈的车辆让出道
while (s.top >= 0)
{
if (0 == strcmp(s.Stop[s.top].plate, C))
{
break;
}
// 让出的车进入让路栈
strcpy(b.Help[b.top++].plate, s.Stop[s.top].plate);
printf ("牌照为%s的汽车暂时退出停车场\n", s.Stop[s.top--].plate);
}
// 如果停车位中的车都让了道,说明停车位中无车辆需要出行
if (s.top < 0)
{
printf ("停车位上无此车消息\n");
}
else
{
printf ("牌照为%s的汽车从停车场开走\n", s.Stop[s.top].plate);
time_t t1;
long int t = time (&t1);
c.timeOut = t; // 标记离开停车场的时间
char* t2;
t2 = ctime (&t1); // 获取当前时间
printf ("离开时间%s\n需付%ld元\n\n", t2, Price * (c.timeOut - s.Stop[s.top].timeIn)/10);
s.top--;
}
// 将让路栈中的车辆信息压入停车位栈
while (b.top > 0)
{
strcpy(s.Stop[++s.top].plate, b.Help[--b.top].plate);
printf ("牌照为%s的汽车停回停车位%d车位\n", b.Help[b.top].plate, s.top+1);
}
// 从便道中 -> 停车位
while (s.top < MAX_STOP-1)
{
if (0 == p.count) // 判断队列是否为空
{
break;
} // 不为空,将便道中优先级高的车停入停车位
else
{
strcpy(s.Stop[++s.top].plate, p.Pave[p.front].plate);
time_t t1;
long int t = time(&t1);
s.Stop[s.top].timeIn = t;
printf ("牌照为%s的汽车从便道中进入停车位的%d车位\n\n", p.Pave[p.front].plate, s.top+1);
p.front = (p.front + 1) % MAX_PAVE;
p.count--;
}
}
}
/************************************************
function:车离开
1:输出:请输入即将离开的车牌号
2:判断书否队首值
(1)是:输出牌照为**的汽车从便道中离开
(2)否:则找是否在队列中
是:输出牌照为%s的汽车请继续等待,不能离开,退出
否:继续执行下面的
3:判断停车位是否有车辆信息
(1)如果车站没有车输出:车位已空,无车辆信息
(2)否则执行stop_to_buff();
*************************************************/
void car_leave() // 车离开
{
printf ("请输入即将离开的车牌号:\n");
scanf ("%s", &C);
int duilie=0;
if(strcmp(p.Pave[p.front].plate,C)==0)
{
printf ("牌照为%s的汽车从便道中离开\n", p.Pave[p.front].plate);
p.front = (p.front + 1) % MAX_PAVE;
p.count--;
duilie=1;
}
else
{
p.front1=p.front;
while(p.front1 != p.rear)
{
if(strcmp(p.Pave[p.front1].plate,C)==0)
{
{
printf ("牌照为%s的汽车请继续等待,不能离开\n", p.Pave [p.front1].plate);
duilie=1;
break;
}
}
p.front1 = (p.front1 + 1) % MAX_PAVE;
}
}
if(duilie==0)
{
if (s.top < 0)
// 判断停车位是否有车辆信息
{
printf ("车位已空,无车辆信息!\n");
}
else
{
stop_to_buff();
}
}
}
/************************************************
function:显示停车场信息
判断有没有车
1:没车输出:停车场为空
2:有车
输出:(1)*车牌号***停放时间**当前所需支付金额
(2)***秒***元
*************************************************/
void Display()
{
int i = s.top;
if (-1 == i)
{
printf ("停车场为空\n");
}
time_t t1;
long int t = time(&t1); // 标记显示时的时间
printf ("\t车牌号\t\t\t停放时间\t\t当前所需支付金额\n");
while (i != -1)
{
printf ("\t%s\t\t\t%d秒\t\t\t\t%d元\n", s.Stop[i].plate, t - s.Stop[i].timeIn, Price * (t - s.Stop[i].timeIn) / 10);
i--;
}
}
/************************************************
function:目前停车场状况
输出:
(1)停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车
(2)Welcome to our Car Parking
(3)1.停车
(4)2.取车
(5)3.退出系统
*************************************************/
void welcome()
{
printf ("\t*******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, p.count);
printf ("\t********************************************************\n");
printf ("\t---------------Welcome to our Car Parking---------------\n");
printf ("\t* 1.停车 *\n");
printf ("\t* 2.取车 *\n");
printf ("\t* 3.退出系统 *\n");
printf ("\t--------------------------------------------------------\n");
}
/************************************************
function:你的选择
输出:
(1)1.查询当前停车场信息
(2)2.退出系统
(3)3.重新进入系统
*************************************************/
void Choose()
{
printf ("\t--------------------You choose who!----------------------\n");
printf ("\t* 1.查询当前停车场信息 *\n");
printf ("\t* 2.退出系统 *\n");
printf ("\t* 3.重新进入系统 *\n");
printf ("\t---------------------------------------------------------\n");
}
makefile
main:main.o Parking.o
gcc -g main.o Parking.o -o main
main.o:main.c Parking.h
gcc -g -c main.c -o main.o
Parking.o:Parking.c Parking.h
gcc -g -c Parking.c -o Parking.o
clean:
rm -fr *.o
rm -fr main
main.c
/**********************************************************
* 问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,
* 汽车按到达的先后次序停放。若车场满了,车要在门外的便道上等候
* ,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由
* 于通道窄,在它后面的车要先退出,待它走后依次进入。汽车离开
* 时按停放时间收费。
* 基本功能要求:
* 1)建立三个数据结构分别是:停放队列,让路栈,等候队列
* 2)输入数据模拟管理过程,数据(入或出,车号)。
************************************************************/
#include "Parking.h"
int main()
{
s.top = -1;
b.top = 0;
p.rear = 0;
p.count = 0;
p.front = 0;
while(1)
{
//system("clear");
welcome();
char i[50], cho[50];
printf("请输入序号:\n");
scanf ("%s", i);
if (strcmp(i, "1")==0) car_come(); else if (strcmp(i,"2")==0) car_leave(); else if (strcmp(i,"3")==0) break;
else
{
while(strcmp(i, "1")!=0 && strcmp(i, "2")!=0 && strcmp(i, "3")!=0)
{
printf("不按套路出牌鸭!\n重新输入!\n");
welcome();
printf("请输入序号:\n");
scanf ("%s", i);
if (strcmp(i, "1")==0) car_come(); else if (strcmp(i,"2")==0) car_leave(); else if (strcmp(i,"3")==0) break;
}
}
if (strcmp(i,"3")==0) break;
printf("您有以下几个选择:\n");
Choose();
printf("请输入您的选择:\n");
scanf ("%s", cho);
if (strcmp(cho, "1")==0)
{
Display();
}
else if(strcmp(cho, "2")==0)
{
break;
}
else if(strcmp(cho, "3")==0)continue;
else
{
while(strcmp(cho, "1")!=0 && strcmp(cho, "2")!=0 && strcmp(cho, "3")!=0)
{
printf("不按套路出牌鸭!\n重新输入!\n");
Choose();
printf("请输入序号:\n");
scanf("%s", cho);
if(strcmp(cho, "1")==0)
{Display();break;}
else if(strcmp(cho, "2")==0||strcmp(cho, "3")==0)break;
}
}
}
printf("感谢您的使用!\n");
return 0;
}