本文实例为大家分享了C语言实现停车场项目的具体代码,供大家参考,具体内容如下
停车场项目需求
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
(2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、查询场内车辆信息、查询等候车辆信息、退出系统。
(1)linux系统编写(链表、栈、队列);
(2)进车登记:登记车牌号以及入场时间;
(3)出车登记:计算出停车时间,记录车辆车牌;
(4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
(5)查询出入记录:所有车辆,包括已经离开的
(6)查询场内车辆信息:列出所有场内车辆信息
(7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
(8)退出系统。
分析,底层需要写以下内容:
两个栈,停放栈(车牌号,出场时间,入场时间)[初始化,判断是否为空,出栈,入栈,查栈] 让路栈(车牌号)[初始化,出栈,入栈,判断是否为空]
一个队列,[初始化,出队列,入队列](车牌号)
一个链表,(车牌号,出入厂状态,入场时间,出场时间)[初始化,入链表,查找数据,遍历打印]
Windows下代码
工程分为三个文件,分别是main.c fun.c pack.h
main.c:
#include<stdio.h>
#include"park.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>
void main()
{
int ret=0;
char number[10];
park_init();
//所有数据结构初始化
//存放用户输入的字符
while(1)
{
printf("################## 停车场 v1.0 ##################\n");
printf("# #\n");
printf("#####################################################################\n");
printf("# #\n");
printf("# 1-----------------进车登记------------------- #\n");
printf("# #\n");
printf("# 2-----------------出车登记------------------- #\n");
printf("# #\n");
printf("# 3-----------------车辆查询------------------- #\n");
printf("# #\n");
printf("# 4-----------------出入记录------------------- #\n");
printf("# #\n");
printf("# 5-----------------场内车辆------------------- #\n");
printf("# #\n");
printf("# 6-----------------等候车辆------------------- #\n");
printf("# #\n");
printf("# 7-----------------退出系统------------------- #\n");
printf("# #\n");
printf("#####################################################################\n");
printf("# 201808 #\n");
printf("#####################################################################\n");
gets( number );
switch ( *number ) //选择需要什么功能
{
case '1': //进车登记
{
system("cls");
ret=car_in();
if(ret==FAILURE)
{
printf("输入错误!");
}
printf("入车成功!\n");
getchar();
system("cls");
}
break;
case '2': //出车登记
{
system("cls");
ret=car_out();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
printf("出车成功!\n");
getchar();
system("cls");
}
break;
case '3': //查找车辆
{
system("cls");
ret=find_car();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
//printf("-------------------------------------------------------------------------------------------------------------");
getchar();
system("cls");
}
break;
case '4': //出入记录
{
system("cls");
ret=record_all();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
getchar();
system("cls");
}
break;
case '5': //场内车辆
{
system("cls");
ret=car_park();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
getchar();
system("cls");
}
break;
case '6':
{
system("cls");
//等候车辆
ret=Car_wait();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
getchar();
system("cls");
}
break;
case '7':
printf("欢迎下次使用\n");
break;
default:
system("cls");
printf( "操作错误,此项不存在!\n" );
getchar();
system("cls");
break;
}
if ( strcmp( number, "7" ) == 0 )
break;
}
}
park.h
#ifndef _PARK_H
#define _PAEK_H
#include <time.h>
#include <stdio.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
#define SIZE 6 //车场大小
struct car_info
{
char name[10];
// 车牌
time_t time1;
// 入场时间
time_t time2;
// 出场时间
}
;
typedef struct car_info CAR_I;
struct sequencestack
{
int top;
CAR_I *date;
}
;
typedef struct sequencestack ST;
struct car_wait
{
char name[10];
struct car_wait *next;
}
;
typedef struct car_wait CAR_W;
struct linkqueue
{
CAR_W *front;
CAR_W *rear;
}
;
typedef struct linkqueue LQ;
struct all_info
{
char name[10];
char sit[10];
time_t time1;
// 入场时间
time_t time2;
// 出场时间
struct all_info *next;
}
;
typedef struct all_info A_INFO;
int car_in();
int car_out();
int find_car();
int record_all();
int car_park();
int Car_wait();
int STinit(ST **q);
int STempty(ST *q);
int STinsert(ST *q, char na[] ,time_t time);
int STout(ST *q,char *a, time_t *time);
int LQinit(LQ **q);
int LQinsert(LQ *q, char na[]);
int LQout(LQ *q, char *a);
int LLinit(A_INFO **q);
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB);
int LLfind(A_INFO *q, char na[]);
int LLget(A_INFO *q, int n, A_INFO **k);
int LLtra(A_INFO *q);
int parkadd(CAR_I car[], char na[], time_t time);
int parkdel(CAR_I car[] ,char na[],time_t time);
int print(CAR_I car[]);
int park_init();
struct tm * local_time(time_t *tmpcal_ptr);
int exchange(A_INFO *q, char na[], char s[]);
#endif
fun.c
#include<stdio.h>
#include"park.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>
int num = 0;
//场内车辆数
ST *stack_park;
//停放栈
ST *stack_exchange;
//交换栈
LQ *queue_wait;
//等候队列
A_INFO *all_car;
//存放所有信息的链表
int STinit(ST **q)
{
if(q == NULL)
{
return FAILURE;
}
*q = (ST *)malloc(sizeof(ST)*1);
//为结构体指针分配空间
(*q)->top = -1;
(*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE);
//为内容指针分配空间
return SUCCESS;
}
int STempty(ST *q)
{
if(q == NULL)
{
return FAILURE;
}
return(q->top == -1)? TRUE:FALSE;
//空返回true 不空返回false
}
int STinsert(ST *q, char na[] ,time_t time)
{
if(q == NULL || q->top >= SIZE-1)
{
return FAILURE;
}
strcpy( q->date[q->top+1].name, na );
q->date[q->top+1].time1 = time;
q->top++;
return SUCCESS;
}
int STout(ST *q,char *a, time_t *time)
{
if(q == NULL)
{
return FAILURE;
//错误返回failure
}
if(q->top == -1)
{
return FALSE;
//空返回false
}
strcpy(a, q->date[q->top].name);
//a被修改为出场的车牌
*time = q->date[q->top].time1;
//time被修改为入场时间
q->top--;
return SUCCESS;
}
int LQinit(LQ **q)
{
CAR_W (*p);
(*q) = (LQ *)malloc(sizeof(LQ));
if( (*q) == NULL )
{
return FAILURE;
}
p = (CAR_W *)malloc(sizeof(CAR_W));
if(p == NULL)
{
return FAILURE;
}
p->next = NULL;
(*q)->front = (*q)->rear =p;
return SUCCESS;
}
int LQinsert(LQ *q, char na[])
{
CAR_W *p;
if (NULL == q)
{
return FAILURE;
}
p = (CAR_W *)malloc(sizeof(CAR_W));
if (NULL == p)
{
return FAILURE;
}
strcpy(p->name, na);
p->next = NULL;
q->rear->next = p;
q->rear = p;
return SUCCESS;
}
int LQout(LQ *q, char *a)
{
// char na[10];
CAR_W *p = q->front->next;
if (NULL == q )
{
return FAILURE;
}
if(q->rear == q->front)
{
return FALSE;
}
strcpy(a, p->name);
q->front->next = p->next;
if (NULL == p->next)
{
q->rear = q->front;
//用参数a保存出去的车牌
}
free(p);
return SUCCESS;
}
int LLinit(A_INFO **q)
{
(*q) = (A_INFO *)malloc(sizeof(A_INFO));
if( (*q) == NULL)
{
return FAILURE;
}
(*q)->next = NULL;
return SUCCESS;
}
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB)
{
A_INFO *l;
A_INFO *p = q;
int k = 1;
if (NULL == q)
{
return FAILURE;
}
while(k < n && p != NULL)
{
p=p->next;
k++;
}
if(k > n || p == NULL)
{
return FAILURE;
}
l = (A_INFO *)malloc(sizeof(A_INFO)*1);
if (NULL == l)
{
return FAILURE;
}
strcpy(l->name, na);
strcpy(l->sit ,s);
l->time1 = timeA;
l->time2 = timeB;
l->next = p->next;
p->next = l;
return SUCCESS;
}
int LLfind(A_INFO *q, char na[])
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
return len;
//找到返回位置
}
p = p->next;
len++;
}
return FALSE;
//未找到返回false
}
int LLget(A_INFO *q, int n, A_INFO **k)
{
int i;
A_INFO *p = q;
if (NULL == q)
{
return FAILURE;
}
for (i = 0; i < n && p != NULL ; i++)
{
p = p->next;
}
if(!p)
{
return FAILURE;
}
(*k) = p;
//用k指针保存找到的结构体地址
return SUCCESS;
}
int LLtra(A_INFO *q)
{
double cost;
struct tm *time1;
struct tm *time2;
time_t timenow;
A_INFO *p;
if (NULL == q)
{
return FAILURE;
}
p = q;
while (p->next)
{
p = p->next;
////////////////////记得改这里
time1 = local_time(&p->time1);
if(time1 != NULL)
{
printf("车牌:%s 状态:%s 入场时间:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
time2 = local_time(&p->time2);
if(time2 != NULL)
{
cost = difftime(p->time2,p->time1);
printf(" 出场时间:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec);
printf(" 停车时间:%f秒\n",cost);
printf("-------------------------------------------------------------------------------------------------------------\n");
} else
{
time(&timenow);
cost = difftime(timenow,p->time1);
printf(" 停车时间为:%f秒\n" ,cost);
printf("-------------------------------------------------------------------------------------------------------------\n");
//return 0;
}
} else
{
printf("车牌:%s 排队中\n",p->name);
printf("-------------------------------------------------------------------------------------------------------------\n");
// return 0;
}
}
return SUCCESS;
}
int parkadd(CAR_I car[], char na[], time_t time)
{
if(num>=6)
{
return FAILURE;
}
strcpy(car[num].name, na);
car[num].time1 = time;
num++;
return SUCCESS;
}
int parkdel(CAR_I car[] ,char na[],time_t time)
{
int i;
if(num == 0)
{
return FAILURE;
}
for (i = 0; i < num; i++)
{
if(strcmp(car[i].name, na)==0)
{
for (; i<num; i++)
{
car[i] = car[i+1];
}
break;
}
}
num--;
return SUCCESS;
}
int print(CAR_I car[])
{
int i;
for (i = 0; i<num; i++)
{
printf("场内车辆信息");
}
return SUCCESS;
}
int exchange(A_INFO *q, char na[], char s[])
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
strcpy( p->sit, s ) ;
return SUCCESS;
}
p = p->next;
len++;
}
return FALSE;
}
int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr)
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
p->time2 = tmpcal_ptr;
return SUCCESS;
}
p = p->next;
len++;
}
return FALSE;
}
int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr)
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
p->time1 = tmpcal_ptr;
return SUCCESS;
}
p = p->next;
len++;
}
return FALSE;
}
int car_in()
{
int i;
char a[12];
//记录车牌号
int ret;
time_t tmpcal_ptr;
time_t tmpcal_ptr1;
time_t tmpcal_ptr2;
//为了给出车时间赋空值
printf("请输入车牌号:\n");
gets(a);
time(&tmpcal_ptr);
ret=STinsert(stack_park, a ,tmpcal_ptr);
if(ret==FAILURE)
{
printf("停车场已满,排队中\n");
ret=LQinsert(queue_wait, a);
LLinsert(all_car, 1, a, "排队中",tmpcal_ptr1, tmpcal_ptr2);
// exchange(all_car, a, "排队中");
if(FAILURE==ret)
{
return FAILURE;
}
} else
{
LLinsert(all_car, 1, a, "在场中",tmpcal_ptr, tmpcal_ptr2);
}
for (i=0; i<stack_park->top+1; i++)
{
printf("车牌 :%s\n",stack_park->date[i].name);
}
return SUCCESS;
}
int car_out()
{
char a[12];
char b[12];
char c[12];
int ret;
time_t tmpcal_ptr3;
//出场时间
time_t tmpcal_ptr4;
//
printf("\n请输入出车车牌号:\n\n");
gets(a);
time(&tmpcal_ptr3);
// ret =STout(stack_park, b, &tmpcal_ptr4 );
// while(strcmp(b,a)!=0)
while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE)
{
if(strcmp(b,a)==0)
{
break;
}
STinsert(stack_exchange, b, tmpcal_ptr4);
// ret =STout(stack_park, b, &tmpcal_ptr4 );
}
if(ret == FALSE)
{
printf("未找到该车!\n\n");
while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)
{
STinsert(stack_park, b,tmpcal_ptr4 );
}
return FAILURE;
} else
{
ret = exchange(all_car , b ,"出场了");
ret = exchange1(all_car ,b ,tmpcal_ptr3);
while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)
{
STinsert(stack_park, b,tmpcal_ptr4 );
}
if((ret = LQout(queue_wait, c))!=FALSE)
{
time(&tmpcal_ptr4);
STinsert(stack_park, c,tmpcal_ptr4);
ret = exchange(all_car , c ,"在场中");
ret = exchange2(all_car ,c ,tmpcal_ptr3);
}
}
return SUCCESS;
}
int find_car()
{
int len;
char a[12];
double cost;
struct tm *time1;
struct tm *time2;
time_t timenow;
A_INFO *k;
int ret;
printf("请输入你要查询的车牌:\n");
gets(a);
len = LLfind(all_car, a);
if(len == FALSE)
{
printf("未来过\n\n");
return FAILURE;
}
ret = LLget(all_car, len, &k);
time1 = local_time(&k->time1);
if(time1 != NULL)
{
printf("车牌:%s\n状态:%s\n入场时间:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
time2 = local_time(&k->time2);
if(time2 != NULL)
{
cost = difftime(k->time2,k->time1);
printf("出场时间:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec);
printf("停车时间:%f\n秒",cost);
} else
{
time(&timenow);
cost = difftime(timenow,k->time1);
printf("出场时间:未出场\n停车时间:%f秒\n",cost);
return 0;
}
}
else
printf("等候中");
return SUCCESS;
}////////////////////////
查看出入登记函数
功能:遍历打印存放所有信息的链表
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
查看停车场内车辆信息函数
功能:遍历打印存放停车场内信息的数组
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
查看等候队列内车辆信息函数
功能:遍历打印等候队列中车辆
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
初始化函数
功能:初始化
入参:无
返回值:SUCCESS/FAILURE
//////////////////////////////////////////////////////////////////////
时间函数
功能:转换成当地时间
入参:time_t *tmpcal_ptr(等待转换的时间)
返回值:time_local(当地时间)
///////////////////////////////////
void main()
{
int ret=0;
char number[10];
park_init();
//所有数据结构初始化
//存放用户输入的字符
while(1)
{
printf("################## 停车场 v1.0 ##################\n");
printf("# #\n");
printf("#####################################################################\n");
printf("# #\n");
printf("# 1-----------------进车登记------------------- #\n");
printf("# #\n");
printf("# 2-----------------出车登记------------------- #\n");
printf("# #\n");
printf("# 3-----------------车辆查询------------------- #\n");
printf("# #\n");
printf("# 4-----------------出入记录------------------- #\n");
printf("# #\n");
printf("# 5-----------------场内车辆------------------- #\n");
printf("# #\n");
printf("# 6-----------------等候车辆------------------- #\n");
printf("# #\n");
printf("# 7-----------------退出系统------------------- #\n");
printf("# #\n");
printf("#####################################################################\n");
printf("# 201808 #\n");
printf("#####################################################################\n");
gets( number );
switch ( *number ) //选择需要什么功能
{
case '1': //进车登记
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
ret=car_in();
if(ret==FAILURE)
{
printf("输入错误!");
}
printf("入车成功!\n");
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
break;
case '2': //出车登记
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
ret=car_out();
if(ret!=FAILURE)
{
printf("出车成功!\n");
}
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
break;
case '3': //查找车辆
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
ret=find_car();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
//printf("-------------------------------------------------------------------------------------------------------------");
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
break;
case '4': //出入记录
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
ret=record_all();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
break;
case '5': //场内车辆
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
ret=car_park();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
break;
case '6':
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
//等候车辆
ret=Car_wait();
if(ret==FAILURE)
{
printf("输入错误!\n");
}
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
break;
case '7':
printf("欢迎下次使用\n");
break;
default:
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
printf( "操作错误,此项不存在!\n" );
getchar();
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
break;
}
if ( strcmp( number, "7" ) == 0 )
break;
}
}
fun_u.c
#include<stdio.h>
#include"park_u.h"
#include<stdlib.h>
#include<time.h>
#include<string.h>
int num = 0;
//场内车辆数
ST *stack_park;
//停放栈
ST *stack_exchange;
//交换栈
LQ *queue_wait;
//等候队列
A_INFO *all_car;
//存放所有信息的链表
int STinit(ST **q)
{
if(q == NULL)
{
return FAILURE;
}
*q = (ST *)malloc(sizeof(ST)*1);
//为结构体指针分配空间
(*q)->top = -1;
(*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE);
//为内容指针分配空间
return SUCCESS;
}
int STempty(ST *q)
{
if(q == NULL)
{
return FAILURE;
}
return(q->top == -1)? TRUE:FALSE;
//空返回true 不空返回false
}
int STinsert(ST *q, char na[] ,time_t time)
{
if(q == NULL || q->top >= SIZE-1)
{
return FAILURE;
}
strcpy( q->date[q->top+1].name, na );
q->date[q->top+1].time1 = time;
q->top++;
return SUCCESS;
}
int STout(ST *q,char *a, time_t *time)
{
if(q == NULL)
{
return FAILURE;
//错误返回failure
}
if(q->top == -1)
{
return FALSE;
//空返回false
}
strcpy(a, q->date[q->top].name);
//a被修改为出场的车牌
*time = q->date[q->top].time1;
//time被修改为入场时间
q->top--;
return SUCCESS;
}
int LQinit(LQ **q)
{
CAR_W (*p);
(*q) = (LQ *)malloc(sizeof(LQ));
if( (*q) == NULL )
{
return FAILURE;
}
p = (CAR_W *)malloc(sizeof(CAR_W));
if(p == NULL)
{
return FAILURE;
}
p->next = NULL;
(*q)->front = (*q)->rear =p;
return SUCCESS;
}
int LQinsert(LQ *q, char na[])
{
CAR_W *p;
if (NULL == q)
{
return FAILURE;
}
p = (CAR_W *)malloc(sizeof(CAR_W));
if (NULL == p)
{
return FAILURE;
}
strcpy(p->name, na);
p->next = NULL;
q->rear->next = p;
q->rear = p;
return SUCCESS;
}
int LQout(LQ *q, char *a)
{
// char na[10];
CAR_W *p = q->front->next;
if (NULL == q )
{
return FAILURE;
}
if(q->rear == q->front)
{
return FALSE;
}
strcpy(a, p->name);
q->front->next = p->next;
if (NULL == p->next)
{
q->rear = q->front;
//用参数a保存出去的车牌
}
free(p);
return SUCCESS;
}
int LLinit(A_INFO **q)
{
(*q) = (A_INFO *)malloc(sizeof(A_INFO));
if( (*q) == NULL)
{
return FAILURE;
}
(*q)->next = NULL;
return SUCCESS;
}
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB)
{
A_INFO *l;
A_INFO *p = q;
int k = 1;
if (NULL == q)
{
return FAILURE;
}
while(k < n && p != NULL)
{
p=p->next;
k++;
}
if(k > n || p == NULL)
{
return FAILURE;
}
l = (A_INFO *)malloc(sizeof(A_INFO)*1);
if (NULL == l)
{
return FAILURE;
}
strcpy(l->name, na);
strcpy(l->sit ,s);
l->time1 = timeA;
l->time2 = timeB;
l->next = p->next;
p->next = l;
return SUCCESS;
}
int LLfind(A_INFO *q, char na[])
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
return len;
//找到返回位置
}
p = p->next;
len++;
}
return FALSE;
//未找到返回false
}
int LLget(A_INFO *q, int n, A_INFO **k)
{
int i;
A_INFO *p = q;
if (NULL == q)
{
return FAILURE;
}
for (i = 0; i < n && p != NULL ; i++)
{
p = p->next;
}
if(!p)
{
return FAILURE;
}
(*k) = p;
//用k指针保存找到的结构体地址
return SUCCESS;
}
int LLtra(A_INFO *q)
{
double cost;
struct tm *time1;
struct tm *time2;
time_t timenow=0;
A_INFO *p;
if (NULL == q)
{
return FAILURE;
}
p = q;
while (p->next)
{
p = p->next;
time1 = local_time(&p->time1);
if(time1 != NULL)
{
printf("车牌:%s 状态:%s 入场时间:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
time2 = local_time(&p->time2);
if(!(time2->tm_hour==8&&time2->tm_min==0&&time2->tm_sec==0))
{
cost = difftime(p->time2,p->time1);
printf(" 出场时间:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec);
printf(" 停车时间:%f秒\n",cost);
printf("-------------------------------------------------------------------------------------------------------------\n");
} else
{
time(&timenow);
cost = difftime(timenow,p->time1);
printf(" 停车时间为:%f秒\n" ,cost);
printf("-------------------------------------------------------------------------------------------------------------\n");
//return 0;
}
} else
{
printf("车牌:%s 排队中\n",p->name);
printf("-------------------------------------------------------------------------------------------------------------\n");
// return 0;
}
}
return SUCCESS;
}
int parkadd(CAR_I car[], char na[], time_t time)
{
if(num>=6)
{
return FAILURE;
}
strcpy(car[num].name, na);
car[num].time1 = time;
num++;
return SUCCESS;
}
int parkdel(CAR_I car[] ,char na[],time_t time)
{
int i;
if(num == 0)
{
return FAILURE;
}
for (i = 0; i < num; i++)
{
if(strcmp(car[i].name, na)==0)
{
for (; i<num; i++)
{
car[i] = car[i+1];
}
break;
}
}
num--;
return SUCCESS;
}
int print(CAR_I car[])
{
int i;
for (i = 0; i<num; i++)
{
printf("场内车辆信息");
}
return SUCCESS;
}
int exchange(A_INFO *q, char na[], char s[])
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
strcpy( p->sit, s ) ;
return SUCCESS;
}
p = p->next;
len++;
}
return FALSE;
}
int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr)
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
p->time2 = tmpcal_ptr;
return SUCCESS;
}
p = p->next;
len++;
}
return FALSE;
}
int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr)
{
A_INFO *p;
int len;
if (NULL == q)
{
return FAILURE;
}
p = q->next;
len = 1;
while(p)
{
if(strcmp(p->name, na) == 0)
{
p->time1 = tmpcal_ptr;
return SUCCESS;
}
p = p->next;
len++;
}
return FALSE;
}
int car_in()
{
int i;
char a[12];
//记录车牌号
int ret;
time_t tmpcal_ptr;
time_t tmpcal_ptr1;
time_t tmpcal_ptr2;
//为了给出车时间赋空值
printf("请输入车牌号:\n");
gets(a);
time(&tmpcal_ptr);
ret=STinsert(stack_park, a ,tmpcal_ptr);
if(ret==FAILURE)
{
printf("停车场已满,排队中\n");
ret=LQinsert(queue_wait, a);
LLinsert(all_car, 1, a, "排队中",tmpcal_ptr1, tmpcal_ptr2);
// exchange(all_car, a, "排队中");
if(FAILURE==ret)
{
return FAILURE;
}
} else
{
LLinsert(all_car, 1, a, "在场中",tmpcal_ptr, tmpcal_ptr2);
}
for (i=0; i<stack_park->top+1; i++)
{
printf("车牌 :%s\n",stack_park->date[i].name);
}
return SUCCESS;
}
int car_out()
{
char a[12];
char b[12];
char c[12];
int ret;
time_t tmpcal_ptr3;
//出场时间
time_t tmpcal_ptr4;
//
printf("\n请输入出车车牌号:\n\n");
gets(a);
time(&tmpcal_ptr3);
// ret =STout(stack_park, b, &tmpcal_ptr4 );
// while(strcmp(b,a)!=0)
while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE)
{
if(strcmp(b,a)==0)
{
break;
}
STinsert(stack_exchange, b, tmpcal_ptr4);
// ret =STout(stack_park, b, &tmpcal_ptr4 );
}
if(ret == FALSE)
{
printf("未找到该车!\n\n");
while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)
{
STinsert(stack_park, b,tmpcal_ptr4 );
}
return FAILURE;
} else
{
ret = exchange(all_car , b ,"出场了");
ret = exchange1(all_car ,b ,tmpcal_ptr3);
while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)
{
STinsert(stack_park, b,tmpcal_ptr4 );
}
if((ret = LQout(queue_wait, c))!=FALSE)
{
time(&tmpcal_ptr4);
STinsert(stack_park, c,tmpcal_ptr4);
ret = exchange(all_car , c ,"在场中");
ret = exchange2(all_car ,c ,tmpcal_ptr3);
}
}
return SUCCESS;
}
int find_car()
{
int len;
char a[12];
double cost;
struct tm *time1;
struct tm *time2;
time_t timenow;
A_INFO *k;
int ret;
printf("请输入你要查询的车牌:\n");
gets(a);
len = LLfind(all_car, a);
if(len == FALSE)
{
printf("未来过\n\n");
return FAILURE;
}
ret = LLget(all_car, len, &k);
time1 = local_time(&k->time1);
if(time1 != NULL)
{
printf("车牌:%s\n状态:%s\n入场时间:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec);
time2 = local_time(&k->time2);
if(time2 != NULL)
{
cost = difftime(k->time2,k->time1);
printf("出场时间:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec);
printf("停车时间:%f\n秒",cost);
} else
{
time(&timenow);
cost = difftime(timenow,k->time1);
printf("出场时间:未出场\n停车时间:%f秒\n",cost);
return 0;
}
}
else
printf("等候中");
return SUCCESS;
}
int record_all()
{
LLtra(all_car);
return SUCCESS;
}
int car_park()
{
int i;
double cost;
struct tm *time1;
time_t timenow;
for (i=0; i<stack_park->top+1; i++)
{
time1 = local_time(&stack_park->date[i].time1);
printf("车牌:%s 入场时间:%d:%d:%d",stack_park->date[i].name, time1->tm_hour, time1->tm_min, time1->tm_sec);
time(&timenow);
cost = difftime(timenow,stack_park->date[i].time1);
printf(" 未出场,停车时间为:%f秒\n\n",cost);
printf("-------------------------------------------------------------------------------------------------------------\n");
}
return SUCCESS;
}
int Car_wait()
{
CAR_W *p;
p = queue_wait->front;
while(p->next)
{
p=p->next;
printf("车牌:%s\n\n",p->name);
}
return SUCCESS;
}
int park_init()
{
STinit(&stack_park);
STinit(&stack_exchange);
LQinit(&queue_wait);
LLinit(&all_car);
return SUCCESS;
}
struct tm * local_time(time_t *tmpcal_ptr)
{
struct tm *time_local = NULL;
time_local = localtime(tmpcal_ptr);
//转换成当地时间
return time_local;
}
park_u.h
#ifndef _PARK_H
#define _PAEK_H
#include <time.h>
#include <stdio.h>
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10002
#define FALSE 10003
#define SIZE 6 //车场大小
struct car_info
{
char name[10];
// 车牌
time_t time1;
// 入场时间
time_t time2;
// 出场时间
}
;
typedef struct car_info CAR_I;
struct sequencestack
{
int top;
CAR_I *date;
}
;
typedef struct sequencestack ST;
struct car_wait
{
char name[10];
struct car_wait *next;
}
;
typedef struct car_wait CAR_W;
struct linkqueue
{
CAR_W *front;
CAR_W *rear;
}
;
typedef struct linkqueue LQ;
struct all_info
{
char name[10];
char sit[10];
time_t time1;
// 入场时间
time_t time2;
// 出场时间
struct all_info *next;
}
;
typedef struct all_info A_INFO;
int car_in();
int car_out();
int find_car();
int record_all();
int car_park();
int Car_wait();
int STinit(ST **q);
int STempty(ST *q);
int STinsert(ST *q, char na[] ,time_t time);
int STout(ST *q,char *a, time_t *time);
int LQinit(LQ **q);
int LQinsert(LQ *q, char na[]);
int LQout(LQ *q, char *a);
int LLinit(A_INFO **q);
int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB);
int LLfind(A_INFO *q, char na[]);
int LLget(A_INFO *q, int n, A_INFO **k);
int LLtra(A_INFO *q);
int parkadd(CAR_I car[], char na[], time_t time);
int parkdel(CAR_I car[] ,char na[],time_t time);
int print(CAR_I car[]);
int park_init();
struct tm * local_time(time_t *tmpcal_ptr);
int exchange(A_INFO *q, char na[], char s[]);
#endif
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。