C語言實現簡單停車場管理系統
阿新 • • 發佈:2020-01-07
本文例項為大家分享了C語言停車場管理系統的具體程式碼,供大家參考,具體內容如下
/*************************************************************************** 專案要求 停車場管理 問題描述:停車場是一個能放n輛車的狹長通道, 只有一個大門,汽車按到達的先後次序停放。若 車場滿了,車要停在門外的便道上等候,一旦有 車走,則便道上第一輛車進入。當停車場中的車 離開時,由於通道窄,在它後面的車要先退出, 待它走後再依次進入。汽車離開時按停放時間收費。 基本功能要求: (1) 建立三個資料結構分別是:停放棧、讓路 棧、等候佇列。 (2) 輸入資料模擬管理過程,資料(入或出,車號)。 ***************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<time.h> #define D (24*60*60) #define H (60*60) #define M (60) #define OK 1 #define ERROR 0 #define MAX_STACK_SIZE 10 /* 棧向量大小 */ typedef int StackData; typedef int QueueData; typedef int ElemType; typedef struct Node { int No; /* 車號 */ int Timeinit; /* 進入停車場的時間*/ }Node; typedef struct QueueNode /* 佇列結點*/ { struct Node data; struct QueueNode* next; } QueueNode; typedef struct LinkQueue /* 鏈式佇列結構體 */ { struct QueueNode *rear,*front; } LinkQueue; typedef struct SqStackNode /* 鏈式棧結構體 */ { int top; int bottom; struct Node stack_array[MAX_STACK_SIZE+1] ; }SqStackNode ; //*************************************************************** SqStackNode* InitStack() /* 初始化棧*/ { SqStackNode *S=(SqStackNode *)malloc(sizeof(SqStackNode)); S->bottom=S->top=0; return (S); } int FullStack(SqStackNode *S) /* 滿棧 */ { return S->top==MAX_STACK_SIZE; } int pushStack(SqStackNode *S,Node data) /* 入棧 */ { if(FullStack(S)) { return ERROR; /* 棧滿,返回錯誤標誌 */ } S->top++ ; (S->stack_array[S->top]).No=data.No ; (S->stack_array[S->top]).Timeinit=data.Timeinit; return OK; /* 壓棧成功 */ } int popStack(SqStackNode *S,Node *data) /*彈出棧頂元素*/ { if(S->top==0) { return ERROR; /* 棧空,返回錯誤標誌 */ } (*data).No=(S->stack_array[S->top]).No; (*data).Timeinit=(S->stack_array[S->top]).Timeinit; S->top--; return OK; } int FinfStack(SqStackNode *S,Node data) /* 搜尋棧內元素data*/ { int i; if(S->top==0) { return ERROR; /* 棧空,返回錯誤標誌 */ } for(i=1;i<=S->top;i++) { if(S->stack_array[i].No == data.No) { return OK; } } return ERROR; } //**************************************************** LinkQueue* InitQueue (void) /* 初始化佇列 */ { LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) ); Q->rear=Q->front=NULL; return Q; } int QueueEmpty ( LinkQueue *Q ) /* 空佇列*/ { return Q->front == NULL; } int GetFrontQueue ( LinkQueue *Q,Node *data ) /* 取隊首 */ { if ( QueueEmpty (Q) ) return 0; (*data).No = (Q->front->data).Timeinit; return 1; } int EnQueue ( LinkQueue **Q,Node data) /* 入隊*/ { QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) ); (p->data).No = data.No; (p->data).Timeinit = data.Timeinit; p->next = NULL; if ( (*Q)->front == NULL ) { (*Q)->front = (*Q)->rear = p; } else { (*Q)->rear = (*Q)->rear->next = p; } return 1; } int DeQueue ( LinkQueue **Q,Node *data) /* 出對*/ { if ( QueueEmpty (*Q) ) { return 0; } QueueNode *p = (*Q)->front; (*data).No = p->data.No; (*data).Timeinit = p->data.Timeinit; (*Q)->front = (*Q)->front->next; if ((*Q)->front == NULL) (*Q)->rear = NULL; free (p); return 1; } /*********************************************************/ int now_time(void) /* 獲取當日時間,單位秒*/ { time_t t1; time(&t1); int time=t1%D; return time; } Parking(LinkQueue **Q,SqStackNode *S) /* 停車*/ { int i,time_now; Node data; printf("Input the Car No:\n"); scanf(" %d",&data.No); for(i=1;i<=S->top;i++) { if(S->stack_array[i].No == data.No)/* 車號已存在*/ { printf("The Car is existed\n"); return ; } } EnQueue(Q,data);/* 進去等待佇列*/ while(!QueueEmpty(*Q)) { if(FullStack(S)) /* 停放棧滿*/ { printf("Please Wait...\n"); break; } else /* 停放棧未滿 */ { DeQueue(Q,&data);/* 等待佇列車出對 */ data.Timeinit=now_time();/* 記錄當前時間*/ pushStack(S,data);/* 進入停放棧*/ printf("Park Success\n"); } } return ; } leaving(SqStackNode *S,SqStackNode *B,LinkQueue **Q)/* 離開*/ { if(S->bottom == S->top)/* 停放棧空*/ { printf("Parking is Empty:\n"); } else { Node data; int i,h,m,s; float charge; int time_now,parking_time; printf("Leaving No:\n"); scanf(" %d",&i); data.No=i; if(!FinfStack(S,data))/* 停放棧內無此車*/ { printf("Do not find the car\n"); return ; } else/* 停放棧內有此車*/ { while(S->stack_array[S->top].No != i)/* 此車後的車依次出棧入讓路棧*/ { popStack(S,&data); pushStack(B,data); } popStack(S,&data);/* 此車出停放棧*/ time_now=now_time(); parking_time=time_now-data.Timeinit;/* 計算停車時間*/ h = parking_time/H; parking_time = parking_time%H; m = parking_time/M; s = parking_time%M; charge = 6*h+0.1*(m+1);/* 計算停車收費*/ printf("The leaving car:%d Parking time:%d:%d:%d Charge($6/h):$%g\n",data.No,s,charge); while(B->bottom != B->top)/* 讓路棧內的車依次出棧入停放棧*/ { popStack(B,&data); pushStack(S,data); } while(!FullStack(S)&&(!QueueEmpty(*Q)))/* 停放棧未滿且等待佇列未空*/ { DeQueue(Q,&data); /* 等待佇列車出隊*/ data.Timeinit=now_time(); pushStack(S,data);/* 出隊的車入停放棧*/ } } } } situation(SqStackNode *S,LinkQueue **Q)/* 檢視停車場當前情況*/ { Node data; int i; int time_now,parking_time; int h,s; struct QueueNode *p; int wait_count=0; p=(*Q)->front; if(p == NULL)/* 等待佇列空*/ { printf("Waiting car :0\n"); } else/* 等待佇列未空*/ { do { wait_count++; p=p->next; }while(p!=NULL);/* 計算等待佇列內車數*/ printf("Waiting car :%d\n",wait_count); } printf("Car No: "); for(i=1;i<=S->top;i++) { printf("%-10d",S->stack_array[i].No); if(S->stack_array[i].No == data.No) { return OK; } } printf("\nPark time:"); for(i=1;i<=S->top;i++) { time_now = now_time(); parking_time = time_now - S->stack_array[i].Timeinit;/* 計算截止當前停車時間*/ h = parking_time/H; parking_time = parking_time%H; m = parking_time/M; s = parking_time%M; printf("%02d:%02d:%02d ",s); } printf("\n"); } int main() { int i; Node data; SqStackNode *park;/* 停放棧*/ SqStackNode *back;/* 讓路棧*/ LinkQueue *wait; /* 等待佇列*/ park=InitStack(); back=InitStack(); wait=InitQueue(); while(1) { system("clear\n"); printf("----------Welcome to our Car Parking----------\n"); printf(" 1.Parking \n"); printf(" 2.leaving \n"); printf(" 3.situation \n"); printf(" 4.exit \n"); scanf(" %d",&i); switch(i) { case 1:/* 停車*/ { system("clear\n"); Parking(&wait,park); setbuf(stdin,NULL); getchar(); break; } case 2:/* 離開 */ { leaving(park,back,&wait); setbuf(stdin,NULL); getchar(); break; } case 3:/* 檢視停車情況*/ { system("clear\n"); situation(park,NULL); getchar(); break; } case 4:/* 退出*/ { return 0; } default: { break; } } } return 0; }
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。