C語言課程設(shè)計之停車場管理問題
C語言課程設(shè)計之停車場管理問題,供大家參考,具體內(nèi)容如下
1.問題描述
停車場內(nèi)只有一個可停放n輛汽車的狹長通道,且只有一個大門可供汽車進出。汽車在停車場內(nèi)按車輛到達時間的先后順序,依次由北向南排列(大門在最南端,最先到達的第一輛車停放在停車場的最北端),若車場內(nèi)已停滿n輛汽車,則后來的汽車只能在門外的便道上等候,一旦有車開走,則排在便道上的第一輛車即可開入;當停車場內(nèi)某輛車要離開時,在它之后開入的車輛必須先退出車場為它讓路,待該輛車開出大門外,其它車輛再按原次序進入車場,每輛停放在車場的車在它離開停車場時必須按它停留的時間長短交納費用。試為停車場編制按上述要求進行管理的模擬程序。
2.基本要求
(1)以棧模擬停車場,以隊列模擬車場外的便道,按照從終端讀入的輸入數(shù)據(jù)序列進行模擬管理。
(2)每一組輸入數(shù)據(jù)包括三個數(shù)據(jù)項:汽車“到達”或“離去”信息、汽車牌照號碼及到達或離去的時刻,對每一組輸入數(shù)據(jù)進行操作后的輸出數(shù)據(jù)為:若是車輛到達,則輸出汽車在停車場內(nèi)或便道上的停車位置;若是車離去;則輸出汽車在停車場內(nèi)停留的時間和應交納的費用(在便道上停留的時間不收費)。
(3)棧以順序結(jié)構(gòu)實現(xiàn),隊列以鏈表結(jié)構(gòu)實現(xiàn)。
(4)按照題意要求獨立進行設(shè)計,設(shè)計結(jié)束后按要求寫出設(shè)計報告。
一、代碼塊
#include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef int ElemType; #define MaxSize 100 #define QNODE struct QNode typedef struct Node?? ??? ?//車輛信息 { ?? ?char AL; ?? ?int NO; ?? ?int time; }Node; typedef struct Stack?? ??? ?//棧定義 { ?? ?struct Node?? ?data[MaxSize]; ?? ?int top; ?? ?int num; }SqStack; QNODE ? ? ? ? ? ? ? //隊列節(jié)點 { ?? ?struct Node data; ?? ?QNODE *next; }; typedef struct linkqueue ? ? ? //隊列結(jié)構(gòu)體定義 { ?? ?QNODE *front,*rear; ?? ?int num; }LinkQueue; SqStack *Init_SeqStack()?? ??? ?//置空棧 { ?? ??? ?SqStack *s; ?? ??? ?s=(SqStack*)malloc(sizeof(SqStack)); ?? ? ? ?s->top=-1; ?? ??? ?s->num=0; ?? ??? ?return s; } LinkQueue *Init_LQueue() ? ? ? ?//創(chuàng)建空隊列 { ?? ??? ?LinkQueue *q; ?? ? ? ?QNODE *p; ? ? ??? ?q=(LinkQueue*)malloc(sizeof(LinkQueue)); ? ? ?? ?p=(QNODE*)malloc(sizeof(QNODE)); ?? ??? ?p->next=NULL; ?? ??? ?q->front=q->rear=p; ?? ??? ?q->num=0; ?? ??? ?return q; } int ISEmpty_SeqStack(SqStack *s)?? ??? ?//判斷棧是否為空,棧為空返回1 { ?? ?if(s->top ==-1) ?? ??? ?return 1; ?? ?else ?? ??? ?return 0; } int ?ISFULL_SeqStack(SqStack *s,int n)?? ?//判斷棧是否已滿,若棧滿返回1 { ?? ?if(s->top==n-1) ?? ??? ?return 1; ?? ?else ?? ??? ?return 0; } int ISEmpty_LQueue(LinkQueue *q)?? ??? ?//判斷隊列是否為空,隊列為空返回1 { ?? ?if(q->front==q->rear) ?? ??? ?return 1; ?? ?else ?? ??? ?return 0; } void IN_Lqueue(?? ?LinkQueue *q,struct Node s) ? //入隊 { ?? ??? ?QNODE *p; ?? ??? ?p=(QNODE*)malloc(sizeof(QNODE)); ?? ??? ?p->data=s; ?? ??? ?q->num++; ?? ??? ?p->next=NULL; ?? ??? ?q->rear->next =p; ?? ??? ?q->rear =p; } void Push_SeqStack(SqStack *p,struct Node s) ? //入棧 { ?? ?p->top ++; ?? ?p->data[p->top]=s; ?? ?p->num++; } int POP_SeqStack(SqStack *s,struct Node car)//出棧 { ?? ?SqStack *p; ?? ?int t; ? ? p=Init_SeqStack(); ?? ?while(s->data[s->top].NO !=car.NO)//找到車牌號為P.NO的車, ?? ?{ ?? ? ? ?Push_SeqStack(p,s->data[s->top]); ?? ??? ?s->top--; ?? ??? ?s->num--; ?? ?} ?? ?t=car.time-s->data[s->top].time; ?? ?s->top--; ?? ?s->num--; ?? ?while(ISEmpty_SeqStack(p)==0) ?? ?{ ?? ??? ?Push_SeqStack(s,p->data[p->top]); ?? ??? ?p->top--; ?? ??? ?p->num--; ?? ?} ?? ?return t; } struct Node Out_LQueue(LinkQueue *q) ? //出隊 { ?? ??? ?QNODE *p; ?? ??? ?p=q->front->next; ?? ??? ?q->front->next=p->next; ?? ??? ?q->num --; ?? ??? ?if(?? ?q->front->next==NULL) ?? ??? ??? ?q->rear=q->front; ?? ??? ?return p->data; ?? ??? ?free(p); } int main() { ?? ?SqStack *parkstack;?? ??? ??? ?//parkstack為表示停車場的棧 ?? ?LinkQueue *parkqueue; ? ? ? //parkqueue為表示便道的隊列 ?? ?struct Node car; ?? ?int n,a=0,t; ? ? ? ? ? ? ? ?//n為停車場棧的最大容量 ?? ?float f; ? ? ? ? ? ? ? ? ? ?//f為每小時收費 ?? ?parkstack=Init_SeqStack(); ?? ?parkqueue=Init_LQueue(); //初始界面 ? ? printf("***************停車場信息查詢***************\n"); ?? ?printf("請輸入停車場最大容量n="); ?? ?scanf("%d",&n); ?? ?printf("\n請輸入每分鐘收取費用f="); ?? ?scanf("%f",&f); ?? ?printf("\n請輸入車輛信息\n"); ? ? scanf("%c,%d,%d",&car.AL,&car.NO,&car.time); ?? ?while(car.AL!='Q') ?? ?{ ?? ? ? if(car.AL=='A' ) ?? ? ? {?? ??? ??? ??? ??? ??? ??? ??? ?//?? ?汽車到達的情況 ?? ??? ??? ??? ? if(ISFULL_SeqStack(parkstack,n)==1)?? ??? ?//棧滿的情況 ?? ??? ??? ??? ? { ?? ??? ??? ??? ??? ?IN_Lqueue(parkqueue,car);?? ??? ??? ??? ?//進入隊列等待 ?? ??? ??? ??? ? ? ?printf("這輛車在門外便道上第%d個位置\n",parkqueue->num); ?? ??? ??? ??? ??? ?printf("\n"); ?? ??? ??? ??? ??? ?printf("請輸入車輛信息\n"); ?? ??? ??? ??? ? } ?? ??? ??? ??? ? else ?? ??? ??? ??? ? { ?? ??? ??? ??? ??? ? Push_SeqStack(parkstack,car);?? ??? ?//入棧 ?? ??? ??? ??? ??? ?printf("這輛車在停車場內(nèi)第%d個位置\n",parkstack->num); ?? ??? ??? ??? ??? ?printf("\n"); ?? ??? ??? ??? ??? ?printf("請輸入車輛信息\n"); ?? ??? ??? ??? ? } ?? ? ? } ?? ? ? if(car.AL=='L' )?? ??? ??? ??? ??? ?//汽車離開的情況 ?? ? ? { ? ? ? ? ? ? ? ? t=POP_SeqStack(parkstack,car);//出棧 ?? ??? ??? ??? ?printf("這輛車停留時間為%d,收費為%f。\n",t,f*t); ?? ??? ??? ??? ?printf("\n"); ?? ??? ??? ??? ?printf("請輸入車輛信息\n"); ?? ??? ??? ? ? ?if(ISEmpty_LQueue(parkqueue)==0) ? //隊列不為空需要進棧 ?? ??? ??? ? ? ? ?Push_SeqStack(parkstack,Out_LQueue(parkqueue) ); ?? ? ? } ?? ? ? if(car.AL=='P'&&car.NO==0&&car.time==0 )//顯示停車場的車數(shù) ?? ? ? { ?? ??? ? ? printf("停車場的車數(shù)為%d\n",parkstack->num); ?? ? ? ? ? printf("\n"); ?? ??? ? ? printf("請輸入車輛信息\n"); ?? ? ? } ? ? ? ?if(car.AL=='W'&&car.NO==0&&car.time==0 )//顯示候車場的車數(shù) ?? ? ? { ?? ??? ? ? printf("候車場的車數(shù)為%d\n",parkqueue->num); ?? ??? ??? ?printf("\n"); ?? ??? ??? ?printf("請輸入車輛信息\n"); ?? ? ? } ?? ??? ?scanf("%c,%d,%d",&car.AL,&car.NO,&car.time); ?? ?} ?? ?printf("輸入結(jié)束\n"); ?? ?return 1; }
二、運行
關(guān)鍵字:A——arrive;L——leave;P——park;Q——quit;W——wait.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中將Char轉(zhuǎn)換成String的4種方法
本文主要介紹了C++中將Char轉(zhuǎn)換成String的4種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03C語言中g(shù)etopt()函數(shù)和select()函數(shù)的使用方法
這篇文章主要介紹了C語言中g(shù)etopt()函數(shù)和select()函數(shù)的使用方法,是C語言入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09C語言實現(xiàn)班級檔案管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細介紹了C語言實現(xiàn)班級檔案管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12利用簡潔的C語言代碼解決跳臺階問題與約瑟夫環(huán)問題
這篇文章主要介紹了利用簡潔的C語言代碼解決跳臺階問題與約瑟夫環(huán)問題的方法,跳臺階問題與約瑟夫環(huán)問題是常見的基礎(chǔ)算法題目,需要的朋友可以參考下2016-02-02