代码段如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define Success 10000
#define Failure 10001
#define SIZE 5
/*
* 顺序栈:top为栈顶下标
* number用来存放车牌号
* timestart、timeend 用来存放汽车进、出时间(long)
* timer、poptime 用来存放汽车进、出时间(char *)
*/
struct SequenceStack
{
int top;
int *number;
long timestart[5];
long timeend[5];
char *timer[5];
char *poptime[5];
};
typedef struct SequenceStack SS;
/*
* 链栈节点:num存放车牌
*/
struct node
{
int num;
struct node *next;
};
typedef struct node SN;
/*
* 链栈:topper 为栈顶指针, count 为栈中元素个数
*/
struct LinkStack
{
SN *topper;
int count;
};
typedef struct LinkStack SL;
/*
* 初始化顺序栈,此处传地址过来,因要给顺序栈分配空间
*/
int SSInit(SS **s1)
{
if(NULL == s1)
{
return Failure;
}
(*s1) = (SS *)malloc(sizeof(SS));
if(*s1 == NULL)
{
return Failure;
}
(*s1)->number = (int *)malloc(sizeof(int) * SIZE);
if(*s1 == NULL)
{
return Failure;
}
int i;
for(i = 0; i < 5; i++)
{
(*s1)->timer[i] = (char *)malloc(sizeof(char) * 64);
(*s1)->poptime[i] = (char *)malloc(sizeof(char) * 64);
if((*s1)->timer[i] == NULL || (*s1)->poptime[i] == NULL)
{
return Failure;
}
}
(*s1)->top = -1;
return Success;
}
/*
* 初始化链栈,传地址,因要给链栈分配空间
*/
int SLInit(SL **s2)
{
if(s2 == NULL)
{
return Failure;
}
(*s2) = (SL *)malloc(sizeof(SL));
if(*s2 == NULL)
{
return Failure;
}
(*s2)->count = 0;
(*s2)->topper = NULL;
return Success;
}
void Hello()
{
system("clear");
sleep(1);
printf(" \033[1m\033[47;31m这\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;32m是\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;33m设\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;34m计\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;35m师\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;36m张\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;31m鹏\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;32m宇\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;33m设\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;34m计\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;35m的\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;36m停\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;31m车\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;32m场\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;33m系\033[0m \n");
sleep(1);
printf(" \033[1m\033[47;34m统\033[0m \n");
sleep(3);
}
/*
* 欢迎界面
* 作者:ZPY
* 时间:2018/8/14
*/
void Welcome()
{
system("clear");
printf("*************************************************************\n");
printf("*************************************************************\n");
printf("**** ****\n");
printf("******* 1、进车登记 ********\n");
printf("******* 2、出车登记 ********\n");
printf("******* 3、查询车辆信息 ********\n");
printf("******* 4、查询出车信息 ********\n");
printf("******* 5、查询场内车辆信息 ********\n");
printf("******* 6、查询等候车辆信息 ********\n");
printf("******* 7、退出系统 ********\n");
printf("**** ****\n");
printf("*************************************************************\n");
printf("*************************************************************\n");
}
/*
*功能函数:停车
* 车先停入停车区s1,若s1停满,则转入等候区s2等候
* 输入车牌即刻停车
* 记录停车时间
*作者:ZPY
*时间:2018/8/14
*/
int Parking(SS *s1, SL *s2)
{
system("clear");
int tmp;
time_t t; //定义时间变量t
if(s1 == NULL || s2 == NULL)
{
return Failure;
}
rp: printf("请输入进车车牌号:\n");
scanf("%d", &tmp);
if(s1->top <= 3) //若停车区s1中车未停满则停入
{
t = time(NULL); //记录当前时间到t中
s1->number[s1->top + 1] = tmp;
s1->top++;
s1->timestart[s1->top] = time(&t); //把当前时间转化为长整型变量储存以备后面计算停车时长
strcpy(s1->timer[s1->top], ctime(&t)); //把当前时间转化为char *型储存以在后面输出
}
else
{
printf("车库已满!该车辆已被安排进入等候区!\n"); //若s1已停满则自动安排进入等候区s2
SN *q = (SN *)malloc(sizeof(SN));
q->num = tmp;
q->next = s2->topper;
s2->topper = q;
s2->count++;
}
int i;
printf("如果您还想继续进车,输入1,否则输入0:\n");
scanf("%d", &i);
if(i == 1)
{
goto rp; //跳转至标志rp处继续执行
}
return 0;
}
/*
* 功能函数:查看停车区中的车辆信息
* 信息如车牌,停车起始时间,停车时长
* 作者:ZPY
* 时间:2018/8/14
*/
int CheckParking(SS *s1)
{
if(s1 == NULL)
{
printf("停车失败!\n");
return -1;
}
system("clear");
int i;
long timetmp; // 定义一个临时变量以备下面使用
time_t t; // 定义一个时间变量
getchar();
for(i = 0; i < s1->top + 1; i++)
{
t = time(NULL); //记录当前时间
timetmp = time(&t); //把当前时间转化为长整型
printf("%d\n%s%ld秒\n\n", s1->number[i], s1->timer[i], timetmp - s1->timestart[i]);
}
printf("按下Enter键结束查看\n");
getchar();
return 0;
}
/*
* 功能函数:查看等候区车辆信息
* 信息只有车牌号
* 作者:ZPY
* 时间:2018/8/14
*/
int CheckWaiting(SL *s2)
{
if(s2 == NULL)
{
printf("查询失败!\n");
return -1;
}
system("clear");
int tmp = s2->count;
getchar();
if(s2->count == 0)
{
printf("等候区无车辆!\n");
sleep(3);
return -1;
}
SN *p = s2->topper;
while(tmp != 0)
{
printf("%d\n", p->num);
p = p->next;
tmp--;
}
printf("按下ENTER键继续\n");
getchar();
return 0;
}
/*
* 功能函数:用于记录停车区车辆的离开
* 需记录车辆离开时间,一共停留时长,并记录
* 若等候区有车,则需要在有车离开时再停入停车区
* 作者:ZPY
* 时间:2018/8/14
*/
int Pushing(SS *s1, SL *s2, SS *s3, SS *s4)
{
if(s1 == NULL || s2 == NULL || s3 == NULL || s4 == NULL)
{
return Failure;
}
system("clear");
if(s1->top == -1)
{
printf("车库中没有车辆!\n");
return -1;
}
int n;
jp: printf("请输入你想出的车的车牌号:\n");
scanf("%d", &n);
int tmp = s1->top;
time_t t;
while(tmp != -1) //既然要出车,那停车区车辆不能为空
{
if(s1->number[tmp] == n) //若车牌号为要出的车的车牌号则执行,要分为下面两种情况
{
if(s1->top == tmp) //此处为第一类,若要出的车为停车区最后进来的一辆车,则执行
{
t = time(NULL);
s4->number[s4->top + 1] = s1->number[s1->top];
s4->top++;
strcpy(s4->timer[s4->top], s1->timer[s1->top]);
strcpy(s4->poptime[s4->top], ctime(&t));
s4->timestart[s4->top] = s1->timestart[s1->top];
s4->timeend[s4->top] = time(&t);
s1->top--;
if(s2->count != 0) //若等候区有车,则执行,需从等候车出一辆车到停车区
{
t = time(NULL);
s1->top++;
s1->number[s1->top] = s2->topper->num;
SN *l = s2->topper;
s2->topper = l->next;
s2->count--;
strcpy(s1->timer[s1->top], ctime(&t)); //记录停车起始时间(char *)
s1->timestart[s1->top] = time(&t); //记录停车起始时间(long)
}
break;
}
else //第二种情况,若出的车不是最后进来的那辆,则需要将外面的
{ //车先出再进
int k, tmp1;
tmp1 = s1->top;
for(k = 0; k < tmp1 - tmp; k++) //先将外面的车先出转到顺序栈s3
{
s3->number[s3->top + 1] = s1->number[s1->top];
s3->timestart[s3->top + 1] = s1->timestart[s1->top];
s3->top++;
s1->top--;
}
t = time(NULL); //将需要出的车出掉并记录
s4->number[s4->top + 1] = s1->number[s1->top];
s4->top++;
strcpy(s4->timer[s4->top], s1->timer[s1->top]);
strcpy(s4->poptime[s4->top], ctime(&t));
s4->timestart[s4->top] = s1->timestart[s1->top];
s4->timeend[s4->top] = time(&t);
s1->top--;
tmp1 = s3->top;
for(k = 0; k <= tmp1; k++) //将出掉的车再进停车区
{
s1->timer[s1->top + 1] = s1->timer[s1->top +2];
s1->number[s1->top + 1] = s3->number[s3->top];
s1->timestart[s1->top + 1] = s3->timestart[s3->top];
s1->top++;
s3->top--;
}
if(s2->count != 0) //若等候区有车,则停入停车区
{
s1->top++;
s1->number[s1->top] = s2->topper->num;
t = time(NULL);
strcpy(s1->timer[s1->top], ctime(&t));
s1->timestart[s1->top] = time(&t);
s2->topper = s2->topper->next;
s2->count--;
}
}
break;
}
tmp--;
}
if(tmp == -1)
{
printf("车库里没有您要找的车!\n");
sleep(2);
return -1;
}
int m;
printf("请问您还想出其他的车吗,若想输入1,不想输入2!\n");
scanf("%d", &m);
if(m == 1)
{
goto jp;
}
else
{
return -1;
}
return 0;
}
/*
* 功能函数:查看车辆信息
* 信息有车牌号,停车起始时间,已停时长
* 作者:ZPY
* 时间:2018/8/14
*/
int CheckCar(SS *s1, SL *s2)
{
system("clear");
if(s1 == NULL || s2 == NULL)
{
return Failure;
}
int n;
op: printf("请输入您要查找的车辆车牌号:\n");
scanf("%d", &n);
getchar();
int tmp;
long timetmp;
time_t t;
tmp = s1->top;
while(tmp >= 0) //在停车区进行查找
{
if(s1->number[tmp] == n)
{
t = time(NULL);
timetmp = time(&t);
printf("此车在停车区内\n车牌号:%d\n停车起始时间:%s已停时间:%ld\n\n", s1->number[tmp], s1->timer[tmp],
timetmp - s1->timestart[tmp]);
printf("按下任意键继续!\n");
getchar();
return -1;
}
tmp--;
}
SN *q = s2->topper;
int temp = s2->count;
while(temp != 0) //在等候区进行查找
{
if(q->num == n)
{
printf("此车在等候区内\n车牌:%d\n\n", n);
printf("按下任意键继续\n");
getchar();
return -1;
}
q = q->next;
temp--;
}
printf("没有您要查找的车辆!\n按下任意键继续\n");
getchar();
return 0;
}
/*
*功能函数:查看出车情况
* 显示已离开的车的车牌,停留起始和离开时间,停留时长
*作者:ZPY
*时间:2018/8/14
*/
CheckPop(SS *s4)
{
if(s4 == NULL)
{
return Failure;
}
system("clear");
getchar();
if(s4->top == -1)
{
printf("暂无车辆出行!\n");
return -1;
}
int i;
for(i = 0; i < s4->top + 1; i++)
{
printf("车牌号:%d\n停车起始时间:%s离开时间:%s停车时间:%ld秒\n", s4->number[i], s4->timer[i], s4->poptime[i], s4->timeend[i] - s4->timestart[i]);
}
printf("按下Enter键结束查看\n");
getchar();
return 0;
}
/*
* 主函数:定义1个顺序栈s1用作停车场
* 1个链栈s2用于等候区
* 1个顺序栈s3用于让路区
* 1个顺序栈s4用于存放离开的车辆
* 1个switch循环用来选择功能
* 时间:2018/8/14
* 作者:ZPY
*/
int main()
{
int n, ret;
SS *s1 = NULL;
SL *s2 = NULL;
SS *s3 = NULL;
SS *s4 = NULL;
if(SSInit(&s1) == Failure || SLInit(&s2) == Failure || SSInit(&s3) == Failure || SSInit(&s4) == Failure)
{
printf("Init failure!\n");
return -1;
}
Hello();
while(1)
{
Welcome();
lp: printf("请输入你想实现功能的序号:\n");
scanf("%d", &n);
switch(n)
{
case 1:
ret = Parking(s1, s2);
if(ret == Failure)
{
printf("Parking failure!\n");
}
break;
case 2:
ret = Pushing(s1, s2, s3, s4);
if(ret == Failure)
{
printf("Pushing failure!\n");
}
break;
case 3:
ret = CheckCar(s1, s2);
if(ret == Failure)
{
printf("Checking Failure!\n");
}
break;
case 4:
ret = CheckPop(s4);
if(ret == Failure)
{
printf("Checking Failure!\n");
}
break;
case 5:
ret = CheckParking(s1);
if(ret == Failure)
{
printf("CheckParking failure!\n");
}
break;
case 6:
ret = CheckWaiting(s2);
if(ret == Failure)
{
printf("CheckWaiting failure!\n");
}
break;
case 7:
exit(1);
break;
default:
goto lp;
}
}
return 0;
}