您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用C語言實現(xiàn)停車場管理系統(tǒng)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
問題描述:停車場是一個能放n輛車的狹長通道,只有一個大門,汽車按到達(dá)的先后次序停放。若車場滿了,車要停在門外的便道上等候,一旦有車走,則便道上第一輛車進(jìn)入。當(dāng)停車場中的車離開時,由于通道窄,在它后面呢的車要先退出,待它走后再依次進(jìn)入。汽車離開時按停放時間收費。
基本功能要求:
(1)建立三個數(shù)據(jù)結(jié)構(gòu)分別是:停放隊列、讓路棧、等候隊列。 (2)輸入數(shù)據(jù)模擬管理過程,數(shù)據(jù)(入或出,車號)。
停車管理系統(tǒng)是C語言中隊列和棧比較簡單的應(yīng)用,需要注意的是停車隊列、等候隊列、讓路棧結(jié)構(gòu)體的構(gòu)建。在寫代碼時,出隊列入棧和出棧入隊列時,指針容易出錯而造成段錯誤,應(yīng)當(dāng)注意。我所寫的代碼如下:
//定義結(jié)構(gòu)體
#include <stdio.h>#include <stdlib.h>#define F 0#define T 1#define MAX 3typedef struct Node //數(shù)據(jù){ int number; int time;}Node;typedef struct QueueNode //隊列結(jié)點{ struct Node infom; struct QueueNode * next;}*QueueNode;typedef struct LinkQueue //鏈隊列{ struct QueueNode * front; struct QueueNode * rear;}LinkQueue;typedef struct stack //棧結(jié)點{ struct Node data; struct stack *next;}*StackNode;typedef struct LinkStack //鏈棧{ StackNode top; int count;}LinkStack;
//函數(shù)實現(xiàn)
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//菜單int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway);//初始化int linklength(LinkQueue q);//查看長度int enqueue(LinkQueue *q,int num,int t);//入隊列int dequeue(LinkQueue *q,int *num,int *t);//出隊列void park1(LinkQueue *wait,LinkQueue *park);//停車函數(shù)int push(LinkStack *s,int num,int t);//入棧int pop(LinkStack *s,int *num,int *t);//出棧void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//離開函數(shù)void view3(LinkQueue wait,LinkQueue park);//查看停車場狀態(tài)int main(){ LinkQueue wait; LinkQueue park; LinkStack giveway; int num = 0; int t = 0; init(&wait,&park,&giveway); menu(&wait,&park,&giveway,num,t); return 0;}int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway){ QueueNode newnode1 = (QueueNode)malloc(sizeof(struct QueueNode)); if(NULL == newnode1) { return F; } newnode1->next = NULL; wait->front = newnode1; wait->rear = newnode1; QueueNode newnode2 = (QueueNode)malloc(sizeof(struct QueueNode)); if(NULL == newnode2) { return F; } newnode2->next = NULL; park->front = newnode2; park->rear = newnode2; giveway->top = NULL; giveway->count = 0;}void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t){ printf("**********Welcome to our Car Parking !**********\n"); printf("********** Please choose function **********\n"); printf("********** 1 : park. **********\n"); printf("********** 2 : leave. **********\n"); printf("********** 3 : view. **********\n"); printf("********** 4 : exit. **********\n"); int option; scanf("%d",&option); switch(option) { case 1:{ park1(wait,park); printf("停車完成!\n"); menu(wait,park,giveway,num,t); break; } case 2:{ leave2(wait,park,giveway,num,t); menu(wait,park,giveway,num,t); break; } case 3:{ view3(*wait,*park); menu(wait,park,giveway,num,t); break; } case 4:{ printf("********** 歡迎再次使用,謝謝! **********\n"); break; } default:{ printf("********** 請輸入正確的指令! **********\n"); menu(wait,park,giveway,num,t); break; } }}int linklength(LinkQueue q){ int i = 0; while(q.front != q.rear) { i++; q.front = q.front->next; } return i;}int enqueue(LinkQueue *q,int num,int t){ QueueNode newnode = (QueueNode)malloc(sizeof(struct QueueNode)); if(NULL == newnode) { return F; } newnode->infom.number = num; newnode->infom.time = t; newnode->next = NULL; q->rear->next = newnode; q->rear = newnode; return T;}int dequeue(LinkQueue *q,int *num,int *t){ if(q->front == q->rear) { printf("the queue is empty!\n"); return F; } *num = q->front->next->infom.number; *t = q->front->next->infom.time; QueueNode temp = q->front->next; q->front->next = temp->next; if(temp->next == NULL) { q->rear = q->front; } free(temp); return T;}void park1(LinkQueue *wait,LinkQueue *park){ printf("請輸入車號和停車時間\n"); int num,t; scanf("%d,%d",&num,&t); if(linklength(*park) >= MAX) { printf("停車場已滿,進(jìn)入等待區(qū)!\n"); enqueue(wait,num,t); } else { enqueue(park,num,t); }}int push(LinkStack *s,int num,int t){ StackNode newnode = (StackNode)malloc(sizeof(struct stack)); if(NULL == newnode) { return F; } newnode->data.number = num; newnode->data.time = t; newnode->next = s->top; s->top = newnode; s->count++; return T;}int pop(LinkStack *s,int *num,int *t){ if(0 == s->count) { printf("the stack is empty !\n"); return F; } *num = s->top->data.number; *t = s->top->data.time; StackNode temp = s->top; s->top = s->top->next; free(temp); s->count--; return T;}void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t){ printf("請輸入要離開車的車號\n"); int leavenumber; scanf("%d",&leavenumber); int i = 0; QueueNode head = park->front; while(head != park->rear) { if(head->next->infom.number != leavenumber) { head = head->next; i++; } else break; } int j = 0; if(i <= MAX-1) { while(j != i) { dequeue(park,&num,&t); push(giveway,num,t); j++; } dequeue(park,&num,&t); } else { printf("查無此車!\n"); } while(giveway->top != NULL) { pop(giveway,&num,&t); enqueue(park,num,t); } if(linklength(*wait) != 0) { dequeue(wait,&num,&t); enqueue(park,num,t); }}void view3(LinkQueue wait,LinkQueue park){ printf("******************** 目前停車場狀況 ********************\n"); printf("停車場共%d個車位,當(dāng)前停車場共有%d量車,等待區(qū)共有%d量車\n", MAX,linklength(park),linklength(wait)); printf("**************************************************************\n"); printf("車 號:"); QueueNode head1 = park.front; QueueNode head2 = park.front; while(head1 != park.rear) { printf("%d ",head1->next->infom.number); head1 = head1->next; } printf("\n"); printf("停車時間:"); while(head2 != park.rear) { printf("%d ",head2->next->infom.time); head2 = head2->next; } printf("\n");}
“怎么用C語言實現(xiàn)停車場管理系統(tǒng)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。