第7周项目6 -停车场模拟
生活随笔
收集整理的這篇文章主要介紹了
第7周项目6 -停车场模拟
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題及代碼
/* *Copyright (c)2016 煙臺(tái)大學(xué)計(jì)算機(jī)與控制工程學(xué)院 *All rights reserved. *文件名稱:6.cpp *作 者:王修文 *完成日期:2016年10月13日 *版 本 號(hào):v1.0 *問題描述:停車場(chǎng)是一個(gè)可停放n輛汽車的狹長(zhǎng)死胡同,南邊封口, 汽車只能從北邊進(jìn)出(這樣的停車場(chǎng)世間少有)。汽車在 停車場(chǎng)內(nèi)按車輛到達(dá)時(shí)間的先后順序,最先到達(dá)的第一輛車 停放在車場(chǎng)的最南端,依次向北排開。若車場(chǎng)內(nèi)已停滿n輛汽車, 則后來的汽車只能在門外的候車場(chǎng)上等候,一旦有車開走,則排 在候車場(chǎng)上的第一輛車即可開入。當(dāng)停車場(chǎng)內(nèi)某輛車要離開時(shí), 在它之后進(jìn)入的車輛必須先退出車場(chǎng)為它讓路(假定停車場(chǎng)內(nèi)設(shè) 有供車輛進(jìn)出的便道,所有的司機(jī)也必須在車內(nèi)隨時(shí)待命), 待該輛車開出大門外,其他車輛再按原次序進(jìn)入車場(chǎng)。每輛停放在 車場(chǎng)的車在它離開停車場(chǎng)時(shí),要按停留的時(shí)間長(zhǎng)短交納費(fèi)用。試為 停車場(chǎng)編制按上述要求進(jìn)行管理的模擬程序。 輸入描述:根據(jù)菜單提示選擇 程序輸出:各指令結(jié)果 */代碼 #include <stdio.h> #include <malloc.h> #define N 10 /*停車場(chǎng)內(nèi)最多的停車數(shù)*/ #define M 10 /*候車場(chǎng)內(nèi)最多的停車數(shù)*/ #define Price 2 /*每單位時(shí)間停車費(fèi)用*/ typedef struct { int CarNo[N]; /*車牌號(hào)*/ int CarTime[N]; /*進(jìn)場(chǎng)時(shí)間*/ int top; /*棧指針*/ } SqStack; /*定義順序棧類型,用于描述停車場(chǎng)*/ typedef struct { int CarNo[M]; /*車牌號(hào)*/ int front,rear; /*隊(duì)首和隊(duì)尾指針*/ } SqQueue; /*定義循環(huán)隊(duì)類型,用于描述候車場(chǎng)*/ /*以下為順序棧的基本運(yùn)算算法*/ void InitStack(SqStack *&s) { s=(SqStack *)malloc(sizeof(SqStack)); s->top=-1; } int StackEmpty(SqStack *s) { return(s->top==-1); } int StackFull(SqStack *s) { return(s->top==N-1); } int Push(SqStack *&s,int e1,int e2) { if (s->top==N-1) return 0; s->top++; s->CarNo[s->top]=e1; s->CarTime[s->top]=e2; return 1; } int Pop(SqStack *&s,int &e1,int &e2) { if (s->top==-1) return 0; e1=s->CarNo[s->top]; e2=s->CarTime[s->top]; s->top--; return 1; } void DispStack(SqStack *s) { int i; for (i=s->top; i>=0; i--) printf("%d ",s->CarNo[i]); printf("\n"); } /*以下為循環(huán)隊(duì)列的基本運(yùn)算算法*/ void InitQueue(SqQueue *&q) { q=(SqQueue *)malloc (sizeof(SqQueue)); q->front=q->rear=0; } int QueueEmpty(SqQueue *q) { return(q->front==q->rear); } int QueueFull(SqQueue *q) /*判斷隊(duì)滿*/ { return ((q->rear+1)%M==q->front); } int enQueue(SqQueue *&q,int e) /*進(jìn)隊(duì)*/ { if ((q->rear+1)%M==q->front) /*隊(duì)滿*/ return 0; q->rear=(q->rear+1)%M; q->CarNo[q->rear]=e; return 1; } int deQueue(SqQueue *&q,int &e) /*出隊(duì)*/ { if (q->front==q->rear) /*隊(duì)空的情況*/ return 0; q->front=(q->front+1)%M; e=q->CarNo[q->front]; return 1; } void DispQueue(SqQueue *q) /*輸出隊(duì)中元素*/ { int i; i=(q->front+1)%M; printf("%d ",q->CarNo[i]); while ((q->rear-i+M)%M>0) { i=(i+1)%M; printf("%d ",q->CarNo[i]); } printf("\n"); } //main函數(shù)用于模擬停車場(chǎng)的工作 int main() { int comm; int no,e1,time,e2; int i,j,t; SqStack *St,*St1; //St是停車場(chǎng),St1是在有車離開時(shí),記錄為該車移開位置的車輛 SqQueue *Qu; //Qu是候車場(chǎng) InitStack(St); InitStack(St1); InitQueue(Qu); do { printf("輸入指令(1:到達(dá) 2:離開 3:顯示停車場(chǎng) 4:顯示候車場(chǎng) 0:退出):"); scanf("%d",&comm); switch(comm) { case 1: /*汽車到達(dá)*/ printf("輸入車號(hào)和時(shí)間(設(shè)車號(hào)和時(shí)間均為整數(shù)): "); scanf("%d%d",&no,&time); if (!StackFull(St)) /*停車場(chǎng)不滿*/ { Push(St,no,time); printf(" >>停車場(chǎng)位置:%d\n",St->top+1); } else /*停車場(chǎng)滿*/ { if (!QueueFull(Qu)) /*候車場(chǎng)不滿*/ { enQueue(Qu,no); printf(" >>候車場(chǎng)位置:%d\n",Qu->rear); } else printf(" >>候車場(chǎng)已滿,不能停車\n"); } break; case 2: /*汽車離開*/ printf("輸入車號(hào)和時(shí)間(設(shè)車號(hào)和時(shí)間均為整數(shù)): "); scanf("%d%d",&no,&time); for (i=0; i<=St->top && St->CarNo[i]!=no; i++); //在棧中找 if (i>St->top) printf(" >>未找到該編號(hào)的汽車\n"); else { t = St->top - i; //需要出棧的車輛數(shù)目 for (j=0; j<t; j++) //for (j=i; j<=St->top; j++)1樓評(píng)論講的原錯(cuò)誤寫法 { Pop(St,e1,e2); Push(St1,e1,e2); /*倒車到臨時(shí)棧St1中*/ } Pop(St,e1,e2); /*該汽車離開*/ printf(" >>%d汽車停車費(fèi)用:%d\n",no,(time-e2)*Price); while (!StackEmpty(St1)) /*將臨時(shí)棧St1重新回到St中*/ { Pop(St1,e1,e2); Push(St,e1,e2); } if (!QueueEmpty(Qu)) /*隊(duì)不空時(shí),將隊(duì)頭進(jìn)棧St*/ { deQueue(Qu,e1); Push(St,e1,time); /*以當(dāng)前時(shí)間開始計(jì)費(fèi)*/ } } break; case 3: /*顯示停車場(chǎng)情況*/ if (!StackEmpty(St)) { printf(" >>停車場(chǎng)中的車輛:"); /*輸出停車場(chǎng)中的車輛*/ DispStack(St); } else printf(" >>停車場(chǎng)中無車輛\n"); break; case 4: /*顯示候車場(chǎng)情況*/ if (!QueueEmpty(Qu)) { printf(" >>候車場(chǎng)中的車輛:"); /*輸出候車場(chǎng)中的車輛*/ DispQueue(Qu); } else printf(" >>候車場(chǎng)中無車輛\n"); break; case 0: /*結(jié)束*/ if (!StackEmpty(St)) { printf(" >>停車場(chǎng)中的車輛:"); /*輸出停車場(chǎng)中的車輛*/ DispStack(St); } if (!QueueEmpty(Qu)) { printf(" >>候車場(chǎng)中的車輛:"); /*輸出候車場(chǎng)中的車輛*/ DispQueue(Qu); } break; default: /*其他情況*/ printf(" >>輸入的命令錯(cuò)誤\n"); break; } } while(comm!=0); return 0; }運(yùn)行結(jié)果
知識(shí)點(diǎn)總結(jié)
利用所學(xué)知識(shí)解決實(shí)際問題
學(xué)習(xí)心得
依舊6
總結(jié)
以上是生活随笔為你收集整理的第7周项目6 -停车场模拟的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xml源文件的文档生成工具--DITA
- 下一篇: DITA与DocBook对比分析