1、头文件
#ifndef _HEAD_H_
#define _HEAD_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define MAX 5
#define PRICE 16.8
enum m
{
STACK_FULL,
STACK_NOFULL,
STACK_EMPTY,
STACK_NOEMPTY,
PUSH_OK,
QUEUE_EMPTY,
QUEUE_NOEMPTY,
IN_OK,
POP_NO,
POP_OK,
OUT_OK,
OUT_NO
};
typedef struct car
{
char ID[10]; //车排号
time_t intime; //入库时间
time_t outtime; //出库时间
int length; //停车时长
float expense; //停车费用
int place; //停车位置
}CAR;
struct Stack
{
CAR num[MAX]; //栈所存放的最大的车辆数
int top; //栈顶指针
};
struct node
{
CAR data;
struct node *next;
};
typedef struct Queue
{
struct node *front; //队头指针
struct node *rear; //队尾指针
}Queue; //队列
typedef struct temporary //临时栈
{
CAR num1[MAX-1];
int top;
}tem;
int menu(); //菜单函数
void init_stack(struct Stack *s); //初始化栈
int empty_stack(struct Stack *s); //判断栈空
int full_stack(struct Stack *s); //判断栈满
int push_stack(struct Stack *s,CAR *car,Queue *q); //车进入停车场
int push1_stack(struct Stack *s,CAR *car,Queue *q); //车进入停车场
int POP_stack(struct Stack *s,CAR *car); //车离开停车场
void init_stack1(tem *s1); //初始化临时栈
int empty_stack1(tem *s1); //判断临时栈空
int full_stack1(tem *s1); //判断临时栈满
int push_stack1(tem *s1,CAR *car); //车驶入临时栈
CAR *pop_stack1(tem *s1,CAR *car); //车离开临时栈
void init_queue(Queue *q); //初始化队列
int empty_queue(Queue *q); //判断队列是否为空
int in_queue(Queue *q,CAR *car); //进入等待车道
int out_queue(struct Stack *s,CAR *car,Queue *q); //从车道取车进入停车场
int display_stack(struct Stack *s); //查看停车场内停车信息
int display_queue(Queue *p); //查看等待车道的信息
int printf_check(CAR *car); //打印发票
int look_check(); //查看发票
#endif
2、停车
#include "head.h"
int push_stack(struct Stack *s,CAR *car,Queue *q)
{
char relay;
Queue *p = q;
if(full_stack(s) == STACK_FULL)
{
printf("停车场车位已停满,是否进入等待车道(y/n):\n");
scanf(" %c",&relay);
while(!(relay == 'y'||relay == 'Y'||relay == 'N'|| relay == 'n'))
{
while(getchar() != '\n');
printf("输入有误,请选择(y/n):");
scanf(" %c",&relay);
}
if(relay == 'y' ||relay == 'Y') //选择y则进入队列等待
{
in_queue(p,car);
}
if(relay == 'n'|| relay == 'N')
{
printf("您已经放弃停车,请返回主界面退出本停车场管理系统,欢迎您的光临!\n");
return 0;
}
}
else
{
s->top++;
s->num[s->top] = *car;
s->num[s->top].place = s->top+1;
time(&s->num[s->top].intime);
return PUSH_OK;
}
}
int push1_stack(struct Stack *s,CAR *car,Queue *q)
{
if(full_stack(s) != STACK_FULL)
{
s->top++;
s->num[s->top] = *car;
s->num[s->top].place = s->top+1;
return PUSH_OK;
}
}
3、取车
void init_stack1(tem *s1) //初始化临时栈
{
s1->top = -1;
}
int empty_stack1(tem *s1) //判断临时栈空
{
if(s1->top == -1)
{
return STACK_EMPTY;
}
return STACK_NOEMPTY;
}
int full_stack1(tem *s1) //判断临时栈是否为满
{
if(s1->top = MAX-2)
{
return STACK_FULL;
}
return STACK_NOFULL;
}
int push_stack1(tem *s1,CAR *car) //进入临时栈
{
s1->top++;
s1->num1[s1->top] = *car;
}
CAR *pop_stack1(tem *s1,CAR *car) //从临时栈内退出
{
if(empty_stack1(s1) == STACK_EMPTY)
{
printf("临时栈为空,无法出栈!\n");
}
else
{
*car = s1->num1[s1->top];
s1->top--;
return car;
}
}
int pop_stack(struct Stack *s,CAR *car,Queue *q) //出停车场
{
char ID[10]; //输入你所要取的车牌号信息
int i = 0; //遍历栈
int flag = 0; //判断取车状态
int j = 0;
int temp = 0;
if(empty_stack(s) == STACK_EMPTY)
{
printf("停车场内无车,取车失败!\n");
return POP_NO;
}
printf("请输入你要取的车牌号:\n");
scanf("%s",ID);
while(i <= s->top)
{
temp = s->top;
if((strcmp(ID,s->num[i].ID) == 0)&&s->num[i].place == i+1)
{
if(i == s->top) //如果所取的车正好在栈顶,则直接取出
{
time(&s->num[s->top].outtime);
s->num[s->top].length = s->num[s->top].outtime - s->num[s->top].intime; //停车时间
s->num[s->top].expense = ((int)((s->num[s->top].length)/3600+1))*16.8;
*car = s->num[s->top];
s->top--;
}
else //否则将它之前的车先放入临时栈内
{
tem *s1 = (tem*)malloc(sizeof(tem));
init_stack1(s1);
j = i;
while(j < temp) //将所取车辆前的车压入临时栈内
{
CAR *car1 = (CAR*)malloc(sizeof(CAR));
*car1 = s->num[s->top];
s->top--;
push_stack1(s1,car1);
j++;
}
time(&s->num[s->top].outtime);
s->num[s->top].length = s->num[s->top].outtime - s->num[s->top].intime; //停车时间
s->num[s->top].expense = ((int)((s->num[s->top].length)/3600+1))*16.8;
*car = s->num[s->top];
s->top--;
j = s->top;
while(j < temp-1) //将放入临时栈内的车重新压入栈内
{
CAR *newcar = (CAR *)malloc(sizeof(CAR));
CAR *car2 = (CAR *)malloc(sizeof(CAR));
newcar = pop_stack1(s1,car2);
push1_stack(s,newcar,q);
j++;
}
}
flag = 1;
}
i++;
}
if(flag == 1)
{
printf("您的车已从本停车场离开,欢迎使用本停车场,祝您一路顺风!\n");
return POP_OK;
}
else
{
printf("本停车场没有您的爱车,请确认车牌信息!\n");
}
}
4、查看停车场信息和等待车道信息
#include "head.h"
int display_stack(struct Stack *s)
{
int i = 0;
time_t time_current; //计算当前时间
int length; //计算当前时间与进入停车场的时间差
if(s->top == -1)
{
printf("当前停车场内无车辆,无法查看到车辆信息!\n");
return -1;
}
while(i <= s->top)
{
printf("车牌号:%s\n",s->num[i].ID);
printf("入库时间 :%s",ctime(&s->num[i].intime));
printf("停车状态:使用中...\n");
printf("停车位: %d号车位\n",s->num[i].place);
time(&time_current);
length = time_current - s->num[i].intime;
printf("当前使用时间:%d分钟\n",(int)(length/60)+1);
printf("当前停车计费: %.1f元",((int)(length/3600)+1)*16.8);
printf("\n\n==================================================================\n\n");
i++;
}
}
#include "head.h"
int display_queue(Queue *q)
{
struct node *p = q->front;
int i = 0;
if(p == NULL)
{
printf("目前等待车道内没有车辆!\n");
return 0;
}
while(p != NULL)
{
printf("车牌:%s\n",p->data.ID);
printf("等待排号 :%d\n",++i);
printf("\n======================================================\n");
p = p->next;
}
}
5、打印发票以及查看发票
#include "head.h"
int printf_check(CAR *car)
{
printf("\n");
printf("车牌号:%s\n",car->ID);
printf("停车时间:%s",ctime(&car->intime));
printf("取车时间:%s",ctime(&car->outtime));
printf("停车时间:%d分钟\n",(int)(car->length/60+1));
printf("停车费用:%.1f元\n",car->expense);
printf("正在打印发票,请稍后...\n");
FILE *fp;
time_t print_time;
if((fp = fopen("check.txt","w+")) == NULL)
{
printf("打印失败!\n");
return -1;
}
fprintf(fp,"\n566停车场发票\n");
fprintf(fp,"车牌号:%s\n",car->ID);
fprintf(fp,"停车时间:%s",ctime(&car->intime));
fprintf(fp,"取车时间:%s",ctime(&car->outtime));
fprintf(fp,"停车时间:%d分钟\n",(int)(car->length/60+1));
fprintf(fp,"停车费用:%.1f元\n",car->expense);
sleep(4);
time(&print_time);
fprintf(fp,"打印时间:%s",ctime(&print_time));
printf("\n打印完成!\n");
fclose(fp);
return 0;
}
#include "head.h"
int look_check()
{
FILE *fp;
char ch;
if((fp = fopen("check.txt","r")) == NULL)
{
printf("查看失败!\n");
return -1;
}
while((ch = fgetc(fp)) != EOF) //从文件中读取字符直到文件末尾
{
putchar(ch); //输出字符
}
fclose(fp);
return 0;
}
6、主菜单以及主函数
#include "head.h"
void init_stack(struct Stack *s) //初始化栈
{
s->top = -1;
}
int empty_stack(struct Stack *s) //判断栈空
{
if(s->top == -1)
{
return STACK_FULL;
}
return STACK_NOFULL;
}
int full_stack(struct Stack *s) //判断栈满
{
if(s->top == MAX-1)
{
return STACK_FULL;
}
return STACK_NOFULL;
}
#include "head.h"
void init_queue(Queue *q) //初始化队列
{
q->front = NULL;
q->rear = NULL;
}
int empty_queue(Queue *q) //判断队列是否为空
{
if(q->rear == NULL)
{
return QUEUE_EMPTY;
}
else
{
return QUEUE_NOEMPTY;
}
}
int in_queue(Queue *q,CAR *car)
{
struct node *p = (struct node*)malloc(sizeof(struct node)); //申请队列空间
if(p == NULL)
{
printf("new p malloc error!\n");
return -1;
}
p->data = *car;
p->next = NULL;
if(q->rear == NULL)
{
q->rear = p;
q->front = p;
printf("您的车已经成功进去等待车位");
}
else
{
q->rear->next = p;
q->rear = p;
printf("您的车已经成功进入等待车位");
}
return IN_OK;
}
int out_queue(struct Stack *s,CAR *car,Queue *q) //将等待车道的车放入停车场内
{
if(empty_queue(q) == QUEUE_EMPTY) //如果等待队列为空,则不出队列
{
return OUT_NO;
}
struct node *p = q->front; //将p指向队列的第一个
if(empty_stack(s) != STACK_FULL)
{
*car = p->data;
q->front = p->next;
if(q->front == NULL)
{
q->rear = NULL;
}
free(p);
p = NULL;
push_stack(s,car,q); //将取出来的车放入停车场内
return OUT_OK;
}
}
#include "head.h"
int menu()
{
int choice = 0;
int ret = 0; //判断输入是否有误
printf(" ====================================================== \n");
printf(" | |\n");
printf(" | 欢迎来到566停车场管理系统 |\n");
printf(" | |\n");
printf(" | 版本:1.0 |\n");
printf(" |----------------------------------------------------|\n");
printf(" | 1、我要停车 | 2、我要取车 |\n");
printf(" |----------------------------------------------------|\n");
printf(" | 3、查看当前停车信息 | 4、查看等待车道信息 |\n");
printf(" |----------------------------------------------------|\n");
printf(" | 5、退出系统 | 6、查看发票信息 |\n");
printf(" |----------------------------------------------------|\n");
printf(" |相关停车说明:本停车场共有%d个车位,如果已经停满您的 |\n",MAX);
printf(" |爱车将进入等待车道等待,直到等到他人车从停车场出来并|\n");
printf(" |且前方无车辆等待时方可停车! |\n");
printf(" |----------------------------------------------------|\n");
printf(" |收费标准:停车场按照%.1f元/小时计费,如果未满一小时则|\n",PRICE);
printf(" |按照一小时计费,停在等待车道的车辆不计费! |\n");
printf(" |____________________________________________________|\n");
printf(" 请输入你要选择的功能(1/2/3/4/5/6):");
ret = scanf("%d",&choice);
while(ret != 1||(choice != 1&&choice != 2&&choice != 3&&choice != 4&&choice != 5&&choice != 6)) //输入错误处理
{
while(getchar() != '\n');
printf(" 输入有误,请选择正确的功能编号:");
ret = scanf("%d",&choice);
}
if(choice == 1||choice == 2||choice == 3||choice == 4 ||choice == 5 ||choice == 6)
{
return choice;
}
}
#include "head.h"
int main()
{
int choice = 0;
int push_ok = 0;
int out_ok = 0;
int pop_ok = 0;
struct Stack *s = (struct Stack *)malloc(sizeof(struct Stack));
Queue *q = (Queue *)malloc(sizeof(Queue));
if(s == NULL) //判断栈是否创建成功
{
printf("stack malloc error!\n");
return 0;
}
init_stack(s);
if(q == NULL) //判断队列是否创建成功
{
printf("queue malloc error!\n");
return 0;
}
init_queue(q);
while(1)
{
system("reset");
choice = menu();
switch(choice)
{
case 1:
{
system("reset");
CAR *car = (CAR *)malloc(sizeof(CAR)); //car将申请的车辆停入停车场
printf("请录入车辆相关信息\n");
printf("车牌:");
scanf("%s",car->ID);
push_ok = push_stack(s,car,q);
if(push_ok == PUSH_OK)
{
printf("入库成功!您的车停在:%d号车位上,当前时间为%s",s->num[s->top].place,ctime(&s->num[s->top].intime));
}
break;
}
case 2:
{
system("reset");
CAR *car1 = (CAR *)malloc(sizeof(CAR)); //car1存放从停车场出来的车辆信息
pop_ok = pop_stack(s,car1);
if(pop_ok == POP_OK)
{
printf("\n发票信息:\n");
printf_check(car1);
CAR *car2 = (CAR *)malloc(sizeof(CAR)); //每取一辆车将停车位置在上的1号车取出放入栈内
out_ok = out_queue(s,car2,q);
if(out_ok == OUT_OK)
{
printf("等待车道1号车已经进入停车场!\n");
}
}
break;
}
case 3:
{
system("reset");
display_stack(s);
break;
}
case 4:
{
system("reset");
display_queue(q);
break;
}
case 5:
{
printf("\t\t欢迎使用!^V^\n");
exit(0);
}
case 6:
{
system("reset");
look_check();
break;
}
}
printf("\n\n按回车键返回主菜单.........");
getchar();
getchar();
}
return 0;
}