几天没发了,想起有个停车场可以发一下;就发了
停车场管理系统主要(要建立俩个栈和一个队列)难点就在于要出来的时候,车子要移出来,让等待车辆进入该系统,在移回去,这个过程很烦。其他的还行不是很难。
停车场.h文件
#ifndef __TINGCHECHANG_H__
#define __TINGCHECHANG_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
#define PAYSIZE 1
#define OUT_OK 2
#define TRUE 1
#define FALSE 0
//**********车辆基本信息**********
typedef struct car
{
char ID[10]; //车排号
time_t intime; //入库时间
time_t outtime; //出库时间
int length; //停车时长
float expense; //停车费用
int place; //停车位置
}Car;
//**********停车栈**********
typedef struct _stack
{
Car num[SIZE]; //栈所存放的最大的车辆数
int top; //栈顶元素下表
}Stack;
//**********等待停车队**********
typedef struct _node
{
Car data;
struct _node *next;
}Node;
typedef struct _Queue
{
struct _node *front; //对头指针
struct _node *rear; //队尾指针
}Queue;
//**********便道让车栈**********
typedef struct _temstack //临时停车栈
{
Car num1[SIZE-1];
int top;
}TemStack;
//菜单函数
int menu();
//初始化栈
int Init_Stack (Stack *s);
//判断栈空
int Empty_Stack (Stack *s);
//判断栈满
int Full_Stack (Stack *s);
//车进入停车车场
int push_Stack (Stack *s, Car *car, Queue *q);
//车进入停车道
int push1_Stack (Stack *s, Car *car, Queue *q);
//车离开停车场
int pop_Stack (Stack *s, Car *car,Queue *q);
//初始化临时栈
int Init_Stack1 (TemStack *s1);
//判断临时栈空
int Empty_Stack1 (TemStack *s1);
//判断临时栈满
int Full_Stack1 (TemStack *s1);
//车驶入临时栈
int push_Stack1 (TemStack *s1,Car *car);
//车离开临时栈
Car *pop_Stack1 (TemStack *s1, Car *car);
//初始化队列
Queue* Create_Queue();
//判断队列是否为空
int Empty_Queue (Queue *q);
//进入等待车道
int In_Queue (Queue *q, Car *car);
//从车道取车进入停车场
int Out_Queue (Stack *s, Car *car, Queue *q);
//查看停车场内停车信息
int Display_Stack (Stack *s);
//查看等待车道的信息
int Display_Queue (Queue *q);
//打印发票
int printf_check (Car *car);
//查看发票
int look_check ();
#endif //__TINGCHECHANG_H__
停车场.c文件
#include "tingchechang.h"
//初始化栈
int Init_Stack (Stack *s)
{
if (s == NULL)
return FALSE;
s->top = -1;
}
//空返回真,否则返回假
int Empty_Stack (Stack *s)
{
if (s == NULL)
return FALSE;
return s->top == -1;
}
//满返回真,否则返回假
int Full_Stack (Stack *s)
{
if (s == NULL)
return FALSE;
return s->top == (SIZE-1);
}
//车进入停车车场
int push_Stack (Stack *s, Car *car, Queue *q)
{
char relay;
Queue *p = q;
if (Full_Stack(s))
{
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')
{
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 TRUE;
}
}
//车离开停车场
int pop_Stack (Stack *s, Car *car, Queue *q)
{
char ID[10];
int i = 0;
int flag = 0;
int j = 0;
int tmp = 0;
if (Empty_Stack(s) == TRUE)
{
printf ("停车场内无车,取车失败!\n");
return FALSE;
}
printf ("请输入你要取得车排号:\n");
scanf("%s", ID);
while(i <= 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)*PAYSIZE;
*car = s->num[s->top];
s->top--;
}
else
{
TemStack *s1 = (TemStack*)malloc(sizeof(TemStack));
Init_Stack1(s1);
j = i;
while(j < tmp)
{
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)*PAYSIZE;
*car = s->num[s->top];
s->top--;
j = s->top;
while(j < tmp - 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 TRUE;
}
else
{
printf("本停车场没有您的车,请确认车牌信息!\n");
}
}
//车进入停车场
int push1_Stack (Stack *s, Car *car, Queue *q)
{
if (Full_Stack(s))
{
s->top++;
s->num[s->top] = *car;
s->num[s->top].place = s->top+1;
return TRUE;
}
}
//初始化临时栈
int Init_Stack1 (TemStack *s1)
{
if (s1 == NULL)
return FALSE;
s1->top = -1;
}
//判断临时栈空
int Empty_Stack1 (TemStack *s1)
{
if (s1 == NULL)
return FALSE;
return s1->top == -1;
}
//判断临时栈满
int Full_Stack1 (TemStack *s1)
{
if (s1 == NULL)
return FALSE;
return s1->top == (SIZE-2);
}
//车驶入临时栈
int push_Stack1 (TemStack *s1,Car *car)
{
if (s1 == NULL)
return FALSE;
s1->top++;
s1->num1[s1->top] = *car;
return TRUE;
}
//车离开临时栈
Car *pop_Stack1 (TemStack *s1, Car *car)
{
if (s1 == NULL)
return FALSE;
if (Empty_Stack1(s1))
return FALSE;
*car = s1->num1[s1->top];
s1->top--;
return car;
}
//创建队列并初始化
Queue* Create_Queue()
{
Queue * q = (Queue*)malloc(sizeof(Queue)/sizeof(char));
if (q == NULL)
return NULL;
//置空队
q->front = NULL;
q->rear = NULL;
return q;
}
//判断队列是否为空
int Empty_Queue (Queue *q)
{
if (q == NULL)
return FALSE;
return q->front == NULL;
}
//进入等待车道
int In_Queue (Queue *q, Car *car)
{
if (q == NULL)
return FALSE;
Node * node = (Node*)malloc(sizeof(Node)/sizeof(char));
if(node == NULL)
{
printf("new node malloc error!\n");
return -1;
}
node->data = *car;
node->next = NULL;
if (q->rear == NULL )
{
q->front = node;
q->rear = node;
printf("您的车已经成功进去等待车位");
}
else
{
q->rear->next = node;
q->rear = node;
printf("您的车已经成功进去等待车位");
}
return TRUE;
}
//从车道取车进入停车场
int Out_Queue (Stack *s, Car *car, Queue *q) //将等待车道的车放入停车场内
{
if (s == NULL)
return FALSE;
if (q == NULL)
return FALSE;
if (Empty_Queue(q)) //如果等待队列为空,则不出队列
return FALSE;
Node *p = q->front; //将p指向队列的第一个
if(Empty_Stack (s) != FALSE)
{
*car = p->data;
q->front = p->next;
if (q->front == NULL)
{
q->rear = NULL;
}
free(p);
p = NULL;
push_Stack (s, car, q); //将取出来的车放入停车场内
return TRUE;
}
}
//查看停车场情况
int Display_Stack (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].place;
printf("当前使用时间:%d\n",(int)(length/60)+1);
printf("当前停车计费:%.1f元",((int)(length/3600)+1)*PAYSIZE);
printf("\n\n====================================\n\n");
i++;
}
}
//查看等待车道情况
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;
}
}
//查看发票情况
int look_check ()
{
FILE *fp;
char ch;
if ((fp = fopen("check.txt","r")) == NULL)
{
printf("查看失败!\n");
return -1;
}
while (ch = fgetc(fp) != EOF) //从文件中读取字符
{
fputc(ch, fp); //输出字符
}
fclose(fp);
return 0;
}
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","a+")) == 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));
fprintf(fp,"\n=======================================================\n");
printf("\n打印完成!\n");
fclose(fp);
return 0;
}
停车场 main.c文件
#include <stdio.h>
#include "tingchechang.h"
int menu()
{
int choice = 0;
int ret = 0; //判断输入是否有误
printf(" ====================================================== \n");
printf(" | |\n");
printf(" | 欢迎来到泽伟停车场管理系统 |\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");
printf(" |爱车将进入等待车道等待,直到等到他人车从停车场出来并|\n");
printf(" |且前方无车辆等待时方可停车! |\n");
printf(" |----------------------------------------------------|\n");
printf(" |收费标准:停车场按照%.1f元/小时计费,如果未满一小时则|\n");
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;
}
}
int main()
{
int choice = 0;
int push_ok = 0;
int out_ok = 0;
int pop_ok = 0;
Stack *s = (Stack *)malloc(sizeof(Stack));
Queue *q = (Queue *)malloc(sizeof(Queue));
if(s == NULL) //判断栈是否创建成功
{
printf("创建空间失败\n");
return 0;
}
Init_Stack(s);
if(q == NULL) //判断队列是否创建成功
{
printf("创建空间失败\n");
return 0;
}
Queue* Create_Queue();
while(1)
{
system("clear");
choice = menu();
switch(choice)
{
case 1:
{
system("clear");
Car *car = (Car *)malloc(sizeof(Car)); //car将申请的车辆停入停车场
printf("请录入车辆相关信息\n");
printf("车牌:");
scanf("%s",car->ID);
push_ok = push_Stack(s,car,q);
if(push_ok == TRUE)
{
printf("入库成功!您的车停在:%d号车位上,当前时间为%s",s->num[s->top].place,ctime(&s->num[s->top].intime));
}
break;
}
case 2:
{
system("clear");
Car *car1 = (Car *)malloc(sizeof(Car)); //car1存放从停车场出来的车辆信息
pop_ok = pop_Stack(s,car1,q);
if(pop_ok == TRUE)
{
printf("\n发票信息:\n");
printf_check(car1);
Car *car2 = (Car *)malloc(sizeof(Car)); //每取一辆车将停车位置在上的1号车取出放入栈内
out_ok = Out_Queue(s,car2,q);
if(out_ok == TRUE)
{
printf("等待车道1号车已经进入停车场!\n");
}
}
break;
}
case 3:
{
system("clear");
Display_Stack(s);
break;
}
case 4:
{
system("clear");
Display_Queue(q);
break;
}
case 5:
{
printf("\t\t欢迎使用!^V^\n");
exit(0);
}
case 6:
{
system("clear");
look_check();
break;
}
}
printf("\n\n按回车键返回主菜单.........");
getchar();
getchar();
}
return 0;
}