您好,登錄后才能下訂單哦!
問題描述:停車場是一個能放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,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);//停車函數 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);//離開函數 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,&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("停車場已滿,進入等待區!\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量車,等待區共有%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"); }
更多學習資料請關注專題《管理系統開發》。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。