c语言实现的停车场管理系统
問題描述:
? ? ? ?設(shè)停車場內(nèi)只有一個可停放n輛汽車的狹長通道,且只有一個大門可供汽車進出。汽車在停車場內(nèi)按車輛到達時間的先后順序,依次由北向南排列(大門在最南端,最先到達的第一輛車停放在車場的最北端),若車場內(nèi)已停滿n輛汽車,則后來的汽車只能在門外的便道上等候,一旦有車開走,則排在便道上的第一輛車即可開入;當停車場內(nèi)某輛車要離開時,在它之后開入的車輛必須先退出車場為它讓路,待該輛車開出大門外,其它車輛再按原次序進入車場,每輛停放在車場的車在它離開停車場時必須按它停留的時間長短交納費用。
由題得,此系統(tǒng)要實現(xiàn)的功能為:
?(1)設(shè)計停車場內(nèi)的結(jié)構(gòu)。(由題分析為一個棧,因為只有一個門所以其就像數(shù)據(jù)結(jié)構(gòu)中學到的棧,但是因為既要出棧又要入棧,此處我就將這個棧簡化為了一個鏈表)
(2)當車庫滿時,在車庫外等待的結(jié)構(gòu)。(就像排隊一樣,先來先進后來后進,只能從一端進,另一端出)。
(3)車輛的結(jié)構(gòu),一輛車要有什么信息?首先要有這輛車的車牌號,可以用一個字符數(shù)組來存儲(因為車牌不一定全是數(shù)字,還可能有漢字,英文字母等),題中要求要用要計算收費,就要知道駛?cè)霑r間和駛出時間(怎樣獲取在下面會說到)。
算法描述:
? 1?剛開始定義結(jié)構(gòu)類型,如車的類型,車庫里的類型,車庫外等待的類型。
? 2聲明所要用到的函數(shù):
???? void menu(Stack *cheku,SequenQueue* paidui);//開始菜單//就是展示出菜單的函數(shù)//隊列的相關(guān)操作SequenQueue* InitQueue();//申請一個空隊int EmptyQueue(SequenQueue* Q);//判斷隊空int FullQueue(SequenQueue* Q);//判斷隊滿int EnQueue(SequenQueue* Q, ElemType *e);//入隊int DeQueue(SequenQueue* Q, ElemType *e); //出隊Stack* build();//建鏈表int fullstack(Stack *cheku);//判斷鏈表滿void tingche(Stack *cheku,SequenQueue* paidui);//停車的函數(shù)void likai(Stack *cheku,SequenQueue* paidui);//離開的函數(shù)void chakan(Stack *cheku,SequenQueue* paidui);//查看車庫停車情況的函數(shù)? 3? ?一些可能不理解的說明的說明。
(1). 獲取時間的函數(shù),寫頭文件 #include <time.h>,定義一個 time_t類型的變量starttime,starttime=time(NULL);就是獲取1970年1月1日到當前的秒數(shù),定義一個字符數(shù)組?tmp2[100],strftime(tmp2,sizeof(tmp2),"%Y-%m-%d? %H:%M:%S",localtime(&q->a.starttime));用這一句就能把當前時間的年月日時分秒存進字符數(shù)組中,puts(tmp2),就得到了當前的時間。
(2). 代碼中用到了Sleep函數(shù),這個函數(shù)的功能為延時,要用到一個頭文件,#include <time.h>,此函數(shù)是與(3)中的配合使用。
(3).(2)中提到的就是system("cls"),他的作用就是清屏,(2)的作用就是把結(jié)果讓用戶看到。故兩者配合,就能得到奇效。
程序代碼:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #include <string.h> #define TRUE 1 #define FALSE 0 #define MAXSIZE 1024 static int maxsize; typedef char ElemType; typedef struct {char num[10];//車牌time_t starttime,endtime;//進入推出時間 }car; //車 typedef struct Stack {int top;car a;struct Stack *next;}Stack; typedef struct {char a[10]; }dat; typedef struct {//隊列結(jié)構(gòu)定義dat data[MAXSIZE];int front;int rear;}SequenQueue; void menu(Stack *cheku,SequenQueue* paidui);//開始菜單 SequenQueue* InitQueue();//申請一個空隊 int EmptyQueue(SequenQueue* Q);//判斷隊空 int FullQueue(SequenQueue* Q);//判斷隊滿 int EnQueue(SequenQueue* Q, ElemType *e);//入隊 int DeQueue(SequenQueue* Q, ElemType *e); //出隊 Stack* build();//建鏈表 void tingche(Stack *cheku,SequenQueue* paidui); int fullstack(Stack *cheku); void likai(Stack *cheku,SequenQueue* paidui); void chakan(Stack *cheku,SequenQueue* paidui);int main() {Stack *cheku=build();SequenQueue* paidui=InitQueue();printf("請輸入車庫最大容量:");scanf("%d",&maxsize); system("cls");menu(cheku,paidui);return 0; } void menu(Stack *cheku,SequenQueue* paidui) {printf("********** 歡迎來停車 ! **********\n");printf("********** 請選擇一項 **********\n");printf("********** 1 : park. **********\n");printf("********** 2 : leave. **********\n");printf("********** 3 : view. **********\n");printf("********** 4 : exit. **********\n");int option;scanf("%d",&option);system("cls");switch(option){case 1:{tingche(cheku,paidui);menu(cheku,paidui);break;}case 2:{likai(cheku,paidui);menu(cheku,paidui);break;}case 3:{chakan(cheku, paidui);menu(cheku,paidui);break;}case 4:{printf("********** 歡迎再次使用,謝謝! **********\n");break;}default:{printf("********** 請輸入正確的指令! **********\n");Sleep(1000);menu(cheku,paidui);system("cls");break;}} } int fullstack(Stack *cheku) {if(cheku->top<maxsize-1)return 1;elsereturn 0; } SequenQueue* InitQueue() {SequenQueue* Q = NULL;Q = (SequenQueue*)malloc(sizeof(SequenQueue));Q->front = Q->rear = 0;return Q; } int DeQueue(SequenQueue* Q, ElemType *e) {if(EmptyQueue(Q))return FALSE;else{strcpy(e,Q->data[Q->front].a);Q->front=(Q->front+1)%MAXSIZE;return TRUE;} } int EnQueue(SequenQueue* Q, ElemType *e) {if(FullQueue(Q)){printf("等待的車輛太多,請下次再來");return FALSE;}strcpy(Q->data[Q->front].a,e);Q->rear = (Q->rear+1)%MAXSIZE;return TRUE; } int FullQueue(SequenQueue* Q) {if((Q->rear+1)%MAXSIZE==Q->front){return TRUE;}else{return FALSE;} } int EmptyQueue(SequenQueue* Q) {if(Q->front == Q->rear)return TRUE;elsereturn FALSE; } Stack* build(Stack *cheku,SequenQueue* paidui) {Stack* a;a=(Stack*)malloc(sizeof(Stack));a->top=-1;return a; } void tingche(Stack *cheku,SequenQueue* paidui) {Stack *p;p=(Stack *)malloc(sizeof(Stack));printf("請輸入車牌號\n");fflush(stdin);gets(p->a.num);if(fullstack(cheku)){p->next=cheku->next;cheku->next=p;p->a.starttime=time(NULL);cheku->top++;printf("停車成功\n");Sleep(1000);system("cls");}else{printf("車庫已滿請在門口等待\n");EnQueue(paidui,p->a.num);Sleep(1000);system("cls");} } void likai(Stack *cheku,SequenQueue* paidui) {char m[10];Stack *p,*q;char e[10];int n=0;p=cheku;if(cheku->top==-1){printf("車庫為空\n");Sleep(1000);system("cls"); }else{printf("請輸入離開的車牌:\n");fflush(stdin);gets(m);while(p->next!='\0'){ double money; if(strcmp(p->next->a.num,m)==0){q=p->next;p->next=q->next;q->a.endtime=time(NULL);money=(q->a.endtime-q->a.starttime)*0.00139;char tmp1[100],tmp2[100];strftime(tmp1,sizeof(tmp1),"%Y-%m-%d %H:%M:%S",localtime(&q->a.endtime));strftime(tmp2,sizeof(tmp2),"%Y-%m-%d %H:%M:%S",localtime(&q->a.starttime));printf("停車時間:%s\n",tmp2);printf("離開時間:%s\n",tmp1);printf("共停%ds\n",q->a.endtime-q->a.starttime); printf("收費%.5lf元(一小時五元)\n",money); Sleep(3000);free(q);system("cls");cheku->top--;n++;if(EmptyQueue(paidui)==0){DeQueue(paidui,e);Stack *d=(Stack *)malloc(sizeof(Stack));strcpy(d->a.num,e);d->a.starttime=time(NULL);d->next=cheku->next;cheku->next=d;cheku->top++;printf("已將等待的第一輛車進入停車場");Sleep(1000);system("cls"); } break;}p=p->next;}if(n==0){printf("未找到該車輛信息請重試");Sleep(1000);system("cls");} } } void chakan(Stack *cheku,SequenQueue* paidui) {if(cheku->top==maxsize-1){printf("車庫已滿,共有%d的車輛在等候",(paidui->rear-paidui->front+MAXSIZE)%MAXSIZE);Sleep(1000);system("cls");}else{printf("車庫還有%d個空位",maxsize-cheku->top-1);Sleep(1000);system("cls");} }代碼可能不是最佳,如有錯誤,敬請指正。
總結(jié)
以上是生活随笔為你收集整理的c语言实现的停车场管理系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html音乐播放器 频谱,HTML5 C
- 下一篇: 个人开源项目如何上传maven中央仓库