C语言停车场管理模拟系统
【問題描述】
某停車場是一個(gè)可停放n輛汽車的狹長通道,且只有 一個(gè)大門可供汽車進(jìn)出。汽車在停車場內(nèi)按車輛到達(dá)時(shí)間的先后順序,依次由北向南排列(大門在最南端,最先到達(dá)的第一輛車停放在車場的最北端),若車場內(nèi)已停滿n輛汽車,則后來的汽車只能在門外的便道上等候,一旦有車開走,則排在便道上的第一輛車即可開入;當(dāng)停車場內(nèi)某輛車要離開時(shí),在它之后進(jìn)入的車輛必須先退出車場為它讓路,待該輛車開出大門外,其他車輛再按原次序進(jìn)入車場,每輛停放在車場的車在它離開停車場時(shí)必須按它停留的時(shí)間長短交納費(fèi)用。按上述要求試為停車場編制車輛管理的模擬程序。
【基本要求】
以棧模擬停車場,以隊(duì)列模擬車場外的便道,按照從終端讀入的輸入數(shù)據(jù)序列進(jìn)行模擬管理。每一組輸入數(shù)據(jù)包括三個(gè)數(shù)據(jù)項(xiàng):汽車“到達(dá)”或“離去”信息、汽車牌照號(hào)以及到達(dá)或離去的時(shí)刻。對(duì)每一組輸入數(shù)據(jù)進(jìn)行操作后的輸出信息為:若是車輛到達(dá),則輸出汽車在停車場內(nèi)或便道上的停車位置;若是車輛離去,則輸出汽車在停車場內(nèi)停留的時(shí)間和應(yīng)交納的費(fèi)用(在便道上停留的時(shí)間不收費(fèi))。棧以順序結(jié)構(gòu)實(shí)現(xiàn),隊(duì)列以鏈表結(jié)構(gòu)實(shí)現(xiàn)。
【測試數(shù)據(jù)】
設(shè)n=2,輸入數(shù)據(jù)為:(`A`,1,5),(`A`,2,10), (`D`1,15),(`A`,3,20), (`A`,4,25), (`A`,5,30), (`D`,2,35), (`D`,4,40), (`E`,0,0).其中:`A`表示到達(dá)(Arrival);`D`表示離去(Departure);`E`表示輸入結(jié)束(End)。
【實(shí)現(xiàn)要求】
需另設(shè)一個(gè)棧,臨時(shí)停放為給要離去的汽車讓路而從停車場退出來的汽車,也用順序存儲(chǔ)結(jié)構(gòu)實(shí)現(xiàn)。輸入數(shù)據(jù)按到達(dá)或離去的時(shí)刻有序。棧中每個(gè)元素表示一輛汽車,包含兩個(gè)數(shù)據(jù)項(xiàng):汽車的牌照號(hào)碼和進(jìn)入停車場的時(shí)刻。
特別注意:
整個(gè)車輛的入場和出場必須嚴(yán)格的通過調(diào)用隊(duì)列和棧的相關(guān)函數(shù)實(shí)現(xiàn)。
【運(yùn)行結(jié)果】
(1)當(dāng)停車場內(nèi)車輛未滿時(shí):
?(2)當(dāng)下一輛車輛入棧,顯示當(dāng)前停車場內(nèi)車輛信息
?
?(3)當(dāng)停車場內(nèi)車輛容量已達(dá)上限,提示停車場內(nèi)車輛已滿,顯示棧道內(nèi)車輛信息:
?(4)當(dāng)車輛出停車場,顯示應(yīng)收費(fèi)信息:
?
源代碼如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <malloc.h> #define Max 2 //停車場容量 #define PRICE 2 //收費(fèi)標(biāo)準(zhǔn) :2元/小時(shí) typedef struct car{char id[10];//車牌 char sta; //狀態(tài):A,D,E int t; //入場或離場時(shí)間 }car; typedef struct S *SNode; struct S{//棧 car data[Max];int top; }; typedef SNode Stack; typedef struct Qd{car data;struct Qd *next; } *QNode; typedef struct Q{//隊(duì)列 QNode front,rear; }*Que; bool push(Stack s,car carp); bool pop(Stack s,car *carp); bool insertQ(Que q,car carp); bool DeleQ(Que q,car *carp); void printQ(Que q);//輸出隊(duì)列中所有車輛信息 void printS(Stack s);//輸出停車場中所有車輛信息 void menu(); int main(){int choice;car carp,*tp,*tp2;tp=(struct car*)malloc(sizeof(struct car));tp2=(struct car*)malloc(sizeof(struct car));Stack s1,s2;s1=(struct S*)malloc(sizeof(struct S));s2=(struct S*)malloc(sizeof(struct S));Que q;//初始化停車場 s1->top=-1;//停車場 s2->top=-1; // 中轉(zhuǎn) //初始化便道 q=(Que)malloc(sizeof(struct Q));q->front=q->rear=NULL;while(1){printf("\n\n\n\n\t\t\t\t........歡迎進(jìn)入停車場管理系統(tǒng)!.......\n ");printf("\t\t\t\t 請(qǐng)輸入操作信息(輸入E退出系統(tǒng))\n");printf("例:(A 蒙E 1 )表示車牌號(hào)為蒙E的車輛于1時(shí)進(jìn)入停車場\n (D 蒙E 5 )表示車牌號(hào)為蒙E的車輛于5時(shí)離開停車場\n"); scanf("%c",&carp.sta);if(carp.sta=='E'||carp.sta=='e'){printf("感謝使用停車場管理系統(tǒng)!\n");exit(0);break;}scanf("%s %d",carp.id ,&carp.t );switch (carp.sta){case 'A'://來車 case 'a': if(s1->top ==Max-1) //進(jìn)入便道,同時(shí)輸出便道上所有車輛信息 {printf("\n停車場已滿,該車將駛?cè)氡愕赖群?#xff01;\n\n");insertQ(q,carp);printS(s1); printQ(q); }else{//進(jìn)入停車場 ,同時(shí)輸出便道上所有車輛信息push(s1,carp);printS(s1);} break;case 'D'://走車case 'd':if(s1->top ==-1) //停車場為空 printf("Not found!\n"); else{//離開停車場 while(strcmp(s1->data [s1->top].id,carp.id )!=0 &&s1->top !=-1){//查找 push(s2,s1->data [s1->top ]);pop(s1,tp);}if(s1->top ==-1)printf("Not found!\n");else{pop(s1,tp);tp2->t=carp.t;printf("車牌號(hào)碼為 %s 的車輛停留了%d小時(shí),應(yīng)收費(fèi)用%d元\n", carp.id,carp.t -tp->t,(carp.t -tp->t)*PRICE );void printS(Stack s);while(s2->top !=-1){pop(s2,tp);push(s1,*tp); }if(q->front !=NULL){ //便道上有車,將頭車入庫 DeleQ(q,&carp);carp.t=tp2->t;push(s1,carp);} }} break;}system("pause");system("cls") ;getchar();}return 0; }void printS(Stack s) {int i;printf("停車場內(nèi)車輛信息:\n");for(i=0;i<=s->top;i++){printf("車牌號(hào) 到達(dá)時(shí)間\n%s\t %d時(shí)\n\n",s->data[i].id,s->data[i].t);} }void printQ(Que q) {struct Qd* p;p=(struct Qd*)malloc(sizeof(struct Qd));p=q->front;printf("---------------------------\n"); printf("便道內(nèi)車輛信息:\n");printf("車牌號(hào) 到達(dá)時(shí)間\n%s\t %d時(shí)\n",p->data.id,p->data.t); }bool insertQ(Que q,car carp) {QNode p;p=(QNode)malloc(sizeof(struct Qd));p->data=carp;p->next=NULL;if(q->front==NULL){q->front=p;q->rear=p;}else{q->rear->next=p;q->rear=p;} }bool push(Stack s,car carp) {s->top++;s->data[s->top]=carp; }bool pop(Stack s,car* carp) {*carp=s->data[s->top--]; }bool DeleQ(Que q,car* carp) {QNode p;*carp=q->front->data;p=q->front;q->front=q->front->next;free(p); }總結(jié)
以上是生活随笔為你收集整理的C语言停车场管理模拟系统的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Lubuntu下启用Compiz
- 下一篇: Android系统开发:短信的号码拦截