C語言實現停車場管理系統
阿新 • • 發佈:2020-01-07
問題描述:停車場是一個能放n輛車的狹長通道,只有一個大門,汽車按到達的先後次序停放。若車場滿了,車要停在門外的便道上等候,一旦有車走,則便道上第一輛車進入。當停車場中的車離開時,由於通道窄,在它後面呢的車要先退出,待它走後再依次進入。汽車離開時按停放時間收費。
基本功能要求:
(1)建立三個資料結構分別是:停放佇列、讓路棧、等候佇列。
(2)輸入資料模擬管理過程,資料(入或出,車號)。
停車管理系統是C語言中佇列和棧比較簡單的應用,需要注意的是停車佇列、等候佇列、讓路棧結構體的構建。在寫程式碼時,出佇列入棧和出棧入佇列時,指標容易出錯而造成段錯誤,應當注意。我所寫的程式碼如下:
//定義結構體
#include <stdio.h> #include <stdlib.h> #define F 0 #define T 1 #define MAX 3 typedef struct Node //資料 { int number; int time; }Node; typedef struct QueueNode //佇列結點 { struct Node infom; struct QueueNode * next; }*QueueNode; typedef struct LinkQueue //鏈佇列 { struct QueueNode * front; struct QueueNode * rear; }LinkQueue; typedef struct stack //棧結點 { struct Node data; struct stack *next; }*StackNode; typedef struct LinkStack //鏈棧 { StackNode top; int count; }LinkStack;
//函式實現
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//選單 int init(LinkQueue *wait,LinkStack *giveway);//初始化 int linklength(LinkQueue q);//檢視長度 int enqueue(LinkQueue *q,int t);//入佇列 int dequeue(LinkQueue *q,int *num,int *t);//出佇列 void park1(LinkQueue *wait,LinkQueue *park);//停車函式 int push(LinkStack *s,int t);//入棧 int pop(LinkStack *s,int *t);//出棧 void leave2(LinkQueue *wait,int t);//離開函式 void view3(LinkQueue wait,LinkQueue park);//檢視停車場狀態 int main() { LinkQueue wait; LinkQueue park; LinkStack giveway; int num = 0; int t = 0; init(&wait,&park,&giveway); menu(&wait,&giveway,num,t); return 0; } int init(LinkQueue *wait,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,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,t); break; } case 2:{ leave2(wait,t); menu(wait,t); break; } case 3:{ view3(*wait,*park); menu(wait,t); break; } case 4:{ printf("********** 歡迎再次使用,謝謝! **********\n"); break; } default:{ printf("********** 請輸入正確的指令! **********\n"); menu(wait,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 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 *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("停車場已滿,進入等待區!\n"); enqueue(wait,t); } else { enqueue(park,t); } } int push(LinkStack *s,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 *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,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,&t); push(giveway,t); j++; } dequeue(park,&t); } else { printf("查無此車!\n"); } while(giveway->top != NULL) { pop(giveway,&t); enqueue(park,t); } if(linklength(*wait) != 0) { dequeue(wait,t); } } void view3(LinkQueue wait,LinkQueue park) { printf("******************** 目前停車場狀況 ********************\n"); printf("停車場共%d個車位,當前停車場共有%d量車,等待區共有%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"); }
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。