新聞中心
目錄
一、問題描述
二、算法結(jié)構(gòu)分析與設(shè)計
三、算法主模塊的流程及各子模塊的主要功能
四、算法詳細設(shè)計
五、源代碼
一、問題描述
- 功能要求
設(shè)停車場只有一個可以停放幾輛汽車的狹長通道,且只有一個大門可供汽車進出。汽車在停車場內(nèi)按車輛到達的先后順序依次排列,若車場內(nèi)已經(jīng)停滿幾輛汽車,則后來的汽車只能在門外的便道上等候,一旦有汽車開走,則排在便道上的第一輛汽車即可進入,當(dāng)停車場某輛車要離開時,由于停車場是狹長通道,在他之后開入的車輛必須先退出車場為他讓路,待該輛汽車開出大門之后,為他讓路的車輛按照原次序進入車場。在這里假設(shè)汽車不能從便道上開走。
2.輸入輸出要求
每一組輸入數(shù)據(jù)要求包括三個數(shù)據(jù)項:汽車“到達”或“離去”的信息、汽車拍照號碼、汽車到達或者離去的時刻。
二、算法結(jié)構(gòu)分析與設(shè)計- 時間
typedef?struct?Time
{
? int?mon=0;
? int?date=0;
? int?hour=0;
? int?min=0;
? int?sec=0;
?
}Time;
2.汽車
typedef struct Car
{
? string ChePai="";
? Time?in;
? Time out;
}Car;
3.順序棧
typedef struct SeqStack
{
? Car?CheWei[MAXSIZE];
? int?top=-1;
?
}SeqStack;
4.鏈?zhǔn)疥犃?/p>
typedef struct QNode
{
? Car?data;
? QNode?*next=NULL;
?
}QNode;
typedef struct LQueue
{
? QNode?*front,*rear;
?
}LQueue;
三、算法主模塊的流程及各子模塊的主要功能子模塊的主要功能:
1)壓棧和出棧:SeqStack* InStack(SeqStack* pl,Car a)
SeqStack* OutStack(SeqStack* pl,Car a)
2)入隊和出隊:LQueue* InQueue(Car a,LQueue* p)
Car OutQueue(LQueue* p)
3)初始化隊:LQueue* InitLQueue()
4)獲取系統(tǒng)時間,計算時間差:void differ(Time in,Time out)
Time GetTime()
5)算錢:int MoneyCalculate(int MoneyPH,Time in,Time out)
主程序流程:
1.寫出各個模塊的偽碼算法
得到系統(tǒng)時間:
{獲取系統(tǒng)時間
賦值返回
}
尋找車輛用于查詢:
{遍歷棧,若查詢到車牌一致,返回
遍歷隊列,若查詢到車牌一致,返回
}
入棧:
{top指針上移
獲取系統(tǒng)時間,賦給car進入的時間
裝填數(shù)據(jù)
}
出棧:
{如果內(nèi)部車輛出棧,外部車輛壓入輔助棧,內(nèi)部車輛出棧,外部車輛壓回;
如果最外邊的車輛出棧,則直接出棧。
獲取系統(tǒng)時間,計算時間差,計算應(yīng)繳納的金額。
}
計算繳納的費用:
{計算停泊了幾個小時,不超過半個小時的部分忽略不計
自定義每小時停泊的價格,返回總金額
}
初始化隊:
{申請頭節(jié)點空間和第一個數(shù)據(jù)節(jié)點的空間
頭節(jié)點的頭尾指針均指向第一個數(shù)據(jù)節(jié)點
第一個數(shù)據(jù)節(jié)點的后繼置空
返回頭節(jié)點
}
入隊:
{獲取系統(tǒng)時間,賦給進入的車輛
如果此時便道內(nèi)沒有車輛,第一個數(shù)據(jù)節(jié)點裝填
如果此時便道內(nèi)已經(jīng)有了車輛,申請新的節(jié)點空間,裝填數(shù)據(jù),尾指針后移
返回頭節(jié)點
}
出隊:
{如果便道內(nèi)有多個車輛,用一個node類型指針指向待入車輛,用一個car變量承接數(shù)據(jù),出隊之后釋放指針指向的空間,返回承接的數(shù)據(jù)
如果只有一個車輛,用一個car類型指針指向數(shù)據(jù),承接數(shù)據(jù)后釋放指針,返回數(shù)據(jù)
}
五、源代碼#include#includeusing namespace std;
#define MAXSIZE 5
typedef int DataType;
//定義時間
typedef struct Time
{
int mon=0;
int date=0;
int hour=0;
int min=0;
int sec=0;
}Time;
typedef struct Car
{
string ChePai="";
Time in;
Time out;
}Car;
//定義順序棧
typedef struct SeqStack
{
Car CheWei[MAXSIZE];
int top=-1;
}SeqStack;
//定義鏈?zhǔn)疥犃?typedef struct QNode
{
Car data;
struct QNode *next=NULL;
}QNode;
typedef struct LQueue
{
QNode *front=NULL,*rear=NULL;
}LQueue;
void TimePrint(Time a)
{
cout<<"時間為:"tm_mday;
a.min=p->tm_min;
a.hour=p->tm_hour;
a.sec=p->tm_sec;
return a;
}
Car Search(SeqStack* pl,LQueue* bd,Car op)
{
int j=1;
//先在棧內(nèi)找
for(int i=0;i<=pl->top;i++)
{
if(pl->CheWei[i].ChePai==op.ChePai)
{
cout<<"在第"<CheWei[i];
}
}
//然后在隊列里找
QNode* p=bd->front;
while(op.ChePai!=p->data.ChePai&&p->next!=NULL)
{
p=p->next;
j++;
}
cout<<"車在便道的第"<data;
}
SeqStack* InStack(SeqStack* pl,Car a)
{
pl->top++;
a.in=GetTime();
pl->CheWei[pl->top]=a;
TimePrint(a.in);
return pl;
}
//汽車離去,出棧
SeqStack* OutStack(SeqStack* pl,Car a)
{
SeqStack* fz=new SeqStack();//注意,需要將指針初始化。
while(pl->CheWei[pl->top].ChePai!=a.ChePai)
{
fz->top++;
fz->CheWei[fz->top]=pl->CheWei[pl->top];
pl->top--;
}
while(fz->top!=-1)
{
pl->CheWei[pl->top]=fz->CheWei[fz->top];
pl->top++;
fz->top--;
}
pl->top--;
a.out=GetTime();
TimePrint(a.out);
int MoneyCalculate(int MoneyPH,Time in,Time out);
int money=MoneyCalculate(5,a.in,a.out);//所需要的金額一并輸出
void differ(Time in,Time out);
differ(a.in,a.out);
cout<<"應(yīng)繳納:"<=30)
out.hour++;//超過半個小時部分按一個小時計算
money=MoneyPH*(out.hour-in.hour);
return money;
}
//計算時間差
void differ(Time in,Time out)
{
Time d;
if(out.minfront=q;
p->rear=q;
q->next=NULL;
return p;
}
LQueue* InQueue(Car a,LQueue* p)
{//傳入需要入隊的車輛信息和便道隊的地址
a.in=GetTime();
if(p->front->data.ChePai=="")
{
p->front->data=a;
return p;
}
else
{
QNode* q=new QNode();
q->data=a;
p->rear->next=q;
p->rear=q;
return p;
}
}
//出隊,得到車輛
Car OutQueue(LQueue* p)
{
Car RIn;
Car* op1;
if(p->front!=p->rear)
{
QNode* q=p->front;
p->front=p->front->next;
RIn=q->data;
free(q);
return RIn;
}
else
{
RIn=p->front->data;
op1=&(p->front->data);
free(op1);
return RIn;
}
}
void CarInfPrint(Car a)
{
cout<<"車牌號為:"<>a;
//查詢車輛信息
if(a=="1")
{
cout<<"請輸入車牌號:"<>op.ChePai;
op=Search(pl, bd, op);
differ(op.in,GetTime());
CarInfPrint(op);//車牌號打印正常。
continue;
}
//錄入車輛信息
if (a=="2")
{
string b;
cout<<"離去請按1,到達請按2"<>b;
//離去
if(b=="1")
{
cout<<"請輸入車牌號:"<>op.ChePai;
op=Search(pl, bd, op);
OutStack(pl, op);
if(bd->front->data.ChePai!="")
{
op1=OutQueue(bd);
InStack(pl,op1);
}
continue;
}
//到達
if(b=="2")
{
cout<<"請輸入車牌號:"<>cp;
op.ChePai=cp;
if(pl->top==MAXSIZE-1)
InQueue(op,bd);
if(pl->top
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
本文名稱:模擬停車場管理系統(tǒng)(c++,使用棧和隊列)-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://fisionsoft.com.cn/article/hhehe.html