【嵌入式系统学习记录】小项目:停车场管理系统的体会

2019-07-12 17:42发布

停车场项目需求
问题描述:停车场是一个能放 n 辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门
          外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车
  要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
          (1)建立三个数据结构分别是:停放栈、让路栈、等候队列。
          (2)输入数据模拟管理过程,数据(入或出,车号)
功能描述:进车登记、出车登记、按车牌号查询停车车辆信息、查询出入车记录、
          查询场内车辆信息、查询等候车辆信息、退出系统。
          (1)linux系统编写(链表、栈、队列);
          (2)进车登记:登记车牌号以及入场时间;
          (3)出车登记:计算出停车时间,记录车辆车牌;
  (4)按车牌号查询车辆信息:停车时间,是否来过停车场,是否还在停车场
          (5)查询出入记录:所有车辆,包括已经离开的
          (6)查询场内车辆信息:列出所有场内车辆信息
  (7)查询等候车辆信息:显示等候车辆数量以及所有车牌号
  (8)退出系统。#ifndef _PARK_H #define _PARK_H #define TRUE 10000 #define FALSE 10001 #define P_MAX 3 #define W_MAX 3 #define PRICE 2 struct car { char *carnum; char *timein; char *timeout; int InsertSeconds; int total; struct car *next; }; typedef struct car Car; struct park { Car *top; int length; }; typedef struct park Park; struct waitcar { Car *front; Car *rear; int length; }; typedef struct waitcar Wait; void Menu(); void Init(Park **p, Park **r, Wait **w, Park **h); int EmptyStack(Park *p); int FullStack(Park *p); int EnterStack(Park *p, Car *c); Car *PopStack(Park *p); int TraverStackP(Park *p); int TraverStackH(Park *p); int EmptyQueue(Wait *w); int FullQueue(Wait *w); Car *PopQueue(Wait *w); int TraverQueue(Wait *w); char *now_time(void); int Seconds(); int InsertCar(Park *p, Wait *w); int PopCar(Park *p,Park *r, Wait *w, Park *h); int QueryCar(Park *p, Wait *w, Park *h); int ListAll(Park *p, Wait *w, Park *h); #endif #include #include #include #include #include"park.h" void Menu() { printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~欢迎使用停车管理系统~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~1、停入车辆 2、车辆离开~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~3、查询车辆信息 4、显示所有车辆~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~5、显示在场车辆 6、显示等待中的车辆~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~~~~7、退出系统~~~~~~~~~~~~~~~~~~~~~~~~ "); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ "); } void Init(Park **p, Park **r, Wait **w, Park **h) { (*p) = (Park *)malloc(sizeof(Park)); (*r) = (Park *)malloc(sizeof(Park)); (*h) = (Park *)malloc(sizeof(Park)); (*w) = (Wait *)malloc(sizeof(Wait)); if(NULL == (*p) || NULL == (*r) || NULL == (*w) || NULL == (*h)) { printf("malloc failure! "); } (*p) -> top = NULL; (*r) -> top = NULL; (*h) -> top = NULL; (*p) -> length = 0; (*r) -> length = 0; (*h) -> length = 0; Car *wn = (Car *)malloc(sizeof(Car)); if(NULL == wn) { printf("ERROR! "); } wn -> next = NULL; (*w) -> rear = (*w) -> front = wn; (*w) -> length = 0; } int EmptyStack(Park *p) { if(NULL == p) { return FALSE; } return(NULL == p -> top)? TRUE: FALSE; } int FullStack(Park *p) { if(NULL == p) { return FALSE; } if(p -> length == P_MAX) { return TRUE; } else { return FALSE; } } int EnterStack(Park *p, Car *c) { if(NULL == p) { return FALSE; } Car *t = (Car *)malloc(sizeof(Car)); if (NULL == t) { return FALSE; } t = c; t -> next = p -> top; p -> top = t; p -> length++; } Car *PopStack(Park *p) { if(NULL == p) { printf("ERROR "); } Car *tmp = p -> top; p -> top = tmp -> next; p -> length--; return tmp; } int TraverStackP(Park *p) { if(NULL == p) { return FALSE; } Car *c = p -> top; while(c) { printf("车牌号%s 入场时间为%s 正在计时 ", c -> carnum, c -> timein); c = c -> next; } printf(" "); } int TraverStackH(Park *p) { if(NULL == p) { return FALSE; } Car *c = p -> top; while(c) { printf("车牌号%s 入场时间为%s 离开时间为%s 费用为%d ", c -> carnum, c -> timein, c -> timeout, c -> total); c = c -> next; } printf(" "); } int EmptyQueue(Wait *w) { if(NULL == w) { return FALSE; } return(w -> length == 0)? TRUE: FALSE; } int FullQueue(Wait *w) { if(NULL == w) { return FALSE; } if(w -> length == W_MAX) { return TRUE; } else { return FALSE; } } Car *PopQueue(Wait *w) { if(NULL == w || w -> front == w -> rear) { printf("ERROR "); } Car *tmp = w -> front -> next; w -> front -> next = tmp -> next; if (tmp == w -> rear) { w -> rear = w -> front; } return tmp; } int TraverQueue(Wait *w) { if(NULL == w) { return FALSE; } Car *c = w -> front ->next; while(c) { printf("车牌%s正在等待入栈 ", c -> carnum); c = c -> next; } printf(" "); } char *now_time() { time_t timep; time(&timep); return ctime(&timep); } int Seconds() { time_t timep; return time(&timep); } int InsertCar(Park *p, Wait *w) { if(NULL == p || NULL == w) { printf("error! "); } if(FullStack(p) == TRUE) { if(FullQueue(w) == TRUE) { printf("等候区已满! "); } else { Car *c = (Car *)malloc(sizeof(Car)); c -> carnum = (char *)malloc(sizeof(char) * 8) ; if(NULL == c) { return FALSE; } printf("请输入车牌之后进入等待队列: "); scanf("%s", c -> carnum); c -> next = NULL; w -> rear -> next = c; w -> rear = c; w -> length++; printf("已进入等待队列! "); } printf("车库已满! "); } else { Car *c = (Car *)malloc(sizeof(Car)); c -> carnum = (char *)malloc(sizeof(char) * 8) ; c -> timein = (char *)malloc(sizeof(char) * 30) ; c -> timeout = (char *)malloc(sizeof(char) * 30) ; if(NULL == c) { return FALSE; } printf("请输入车牌号: "); scanf("%s", c -> carnum); strcpy(c -> timein, now_time()); c -> InsertSeconds = Seconds(); c -> total = 0; c -> next = p -> top; p -> top = c; p -> length++; printf("%s已经成功进入停车栈 ",c -> carnum); } } int PopCar(Park *p, Park *r, Wait *w, Park *h) { if(NULL == p || NULL == r || NULL == w || NULL == h) { return FALSE; } char *goalnum = (char *)malloc(sizeof(char) * 20); printf("请输入需要离开的车牌号: "); scanf("%s", goalnum); Car *n = p -> top; int count = 0; while(n != NULL && strcmp(goalnum, n -> carnum) != 0) { count++; n = n -> next; } n = p -> top; if(count == p -> length) { printf("该车辆不在停车场内! "); } else { while(n) { if(strcmp(goalnum, n -> carnum) == 0) { strcpy(n -> timeout, now_time()); n -> total = (Seconds() - n -> InsertSeconds) * PRICE; EnterStack(h, PopStack(p)); printf("%s已经退出停车栈,费用为%d元 ", n -> carnum, n -> total); break; } EnterStack(r, PopStack(p)); printf("%s已经退出停车栈,暂时进入让路栈 ",n -> carnum); n = p-> top;//关键问题!!!!!!! } while(FullStack(p) == FALSE && EmptyStack(r) == FALSE) { EnterStack(p, PopStack(r)); printf("%s已经回到停车栈 ", p -> top -> carnum); if(EmptyStack(r) == TRUE) { printf("让路栈已空! "); break; } } } while( EmptyQueue(w) == FALSE && FullStack(p) == FALSE) { EnterStack(p, PopQueue(w)); printf("%s已经从候车队列进入停车栈! ", p -> top -> carnum); p -> top -> timein = now_time(); } } int QueryCar(Park *p, Wait *w, Park *h) { int flag = 0; char *goal = (char *)malloc(sizeof(char)); if(NULL == p || NULL == h || NULL == w) { return FALSE; } printf("请输入想要查找的车牌号: "); scanf("%s",goal); Car *p1 = p -> top; Car *w1 = w -> front -> next; Car *h1 = h -> top; while(p1) { if(strcmp(goal, p1 -> carnum) == 0) { printf("车牌为%s的车辆正在计时停车,入场时间为%s ",p1 -> carnum, p1 -> timein); flag = 1; break; } p1 = p1 -> next; } while(w1) { if(strcmp(goal, w1 -> carnum) == 0) { printf("车牌为%s的车辆正在等候队列 ",w1 -> carnum); flag = 1; break; } } while(h1) { if(strcmp(goal, h1 -> carnum) == 0) { printf("车辆%s已离开,入场时间%s,离开时间%s,计费%d元 ",h1 -> carnum, h1 -> timein, h1 -> timeout, h1 -> total); flag = 1; break; } } if(flag == 0) { printf("没有找到这辆车! "); } } int ListAll(Park *p, Wait *w, Park *h) { if(NULL == p || NULL == w || NULL == h) { return FALSE; } TraverStackP(p); TraverStackH(h); TraverQueue(w); }
#include"park.h" #include #include int main() { system("clear"); Park *park; Park *road; Wait *waitcar; Park *history; Init(&park, &road, &waitcar, &history); int choice; while(1) { Menu(); printf("请选择:"); scanf("%d",&choice); getchar(); switch(choice) { case 1: InsertCar(park, waitcar); break; case 2: PopCar(park, road, waitcar, history); break; case 3: QueryCar(park, waitcar, history); break; case 4: ListAll(park, waitcar, history); break; /* case 5: ListCarIn(park); break; case 6: ListCarWait(wait); break;*/ case 7: printf("欢迎下次使用! "); sleep(1); exit(1); break; default: printf("请重新选择! "); break; } } return 0; }
跟通讯录相比,本人在该项目中主要通过对初始化、入栈、出栈、进队列、出队列等接口函数的调用实现了对整个停车场的管理操作。对比通讯录,在这个项目中,数据结构更加的多元化,不是单一的单向链表,而是结合了链式队列、链式栈、结构体指针等。在函数调用时,实参与形参的数量和类型都变得丰富,数据类型的一致是实现功能的关键。对于栈中top指针和队列中front和rear都有了更深入的理解。