停車場管理(棧和隊列的應用)

//Stack.h

 

#include <iostream>
using namespace std;

class Bus;

#define Type      Bus
#define MAXSIZE   100

class Bus
{
private:
 char* operation;                         //操作類型
 unsigned int  busnum;                    //車輛編號
 float time;                              //時間
 float fee;                               //費用
public:
 Bus(char* oper="START",unsigned int num=0,float tm=0,float f=0)
  :operation(oper),busnum(num),time(tm),fee(f){}
 ~Bus(){}

 //基本操作
 void SetBus(char* oper,unsigned int num,float tm,float f)
 {
  operation=oper;
  busnum=num;
  time=tm;
  fee=f;
 }

 char* GetOperation() const
 {return operation;}

 unsigned int GetBusnum() const
 {return busnum;}

 float GetTime() const
 {return time;}

 float GetFee() const
 {return fee;}
};


class Stack
{
private:
 Type *base;                   //棧底指針
 Type *top;                    //棧頂指針
 int stacksize;                //棧的大小
public:
 Stack();
 Stack(int size);
 ~Stack();

 //棧的基本操作
 bool Push(const Type e);
 bool Pop(Type& e);
 Type GetTop() const;
 bool Empty();
 bool Full();
 bool Clear();
 int GetStacksize();
};

 

//Stack.cpp

 

#include "stdafx.h"


#include "Stack.h"

Stack::Stack()
{
 base=new Type[MAXSIZE];
 top=base;
 stacksize=0;
}

Stack::Stack(int size)
{
 base=new Type[size];
 top=base;
 stacksize=0;
}

Stack::~Stack()
{
 delete []base;
}

bool Stack::Push(const Type e)
{
 if(stacksize<MAXSIZE-1)
 {
  *top++=e;
  stacksize++;
  return true;
 }
 return false;
}

bool Stack::Pop(Type& e)
{
 if(stacksize>0)
 {
  e=*--top;
  stacksize--;
  return true;
 }
 return false;
}

Type Stack::GetTop() const
{
 if(stacksize>0)
 {
  return *(top-1);
 }
 return 0;
}

bool Stack::Empty()
{
 if(((top-base)==0) && (stacksize==0))
  return true;
 return false;
}

bool Stack::Clear()
{
 stacksize=0;
 top=base;
 return true;
}

bool Stack::Full()
{
 if((5<=(top-base)) && stacksize>0)
  return true;
 return false;
}

int Stack::GetStacksize()
{
 return stacksize;
}

 

 

//main.cpp

 

// DS_experiment_2.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"

#include "stack.h"

 

//處理函數
void Process(Stack* parkinglot,Stack* waitlot,const Bus* bs,float unit_price=1);

//parkinglot 大小有限制 waitlot大小沒限制
void Process(Stack* parkinglot,Stack* waitlot,const Bus* bs,float unit_price)
{
 Bus temp;

 if(strcmp(bs->GetOperation(),"End"))
 {
  if(!strcmp(bs->GetOperation(),"Arrival"))
  {
   if(!parkinglot->Full())
   {
    parkinglot->Push(*bs);           //進站
    cout<<bs->GetBusnum()<<" bus enter the parkinglot!"<<endl;
   }

   else
   {   //停車場位置滿了
    cout<<"The parkinglot is full!"<<endl;
   }
   return;
  }

  if(!strcmp(bs->GetOperation(),"Departure"))
  {
   if(!parkinglot->Empty())
   {
    while((!parkinglot->Empty()) && (parkinglot->GetTop().GetBusnum()!=bs->GetBusnum()))
    {
     parkinglot->Pop(temp);
     waitlot->Push(temp);
    }

    if(!parkinglot->Empty())
    {
     //輸出費用
     cout<<bs->GetBusnum()<<" bus leave the parkinglot!"<<endl;
     cout<<bs->GetBusnum()<<" bus stay "<<bs->GetTime()-parkinglot->GetTop().GetTime()<<" hours!"<<endl;
     cout<<"It costs ¥"<<bs->GetTime() - parkinglot->GetTop().GetTime()<<"."<<endl;
     parkinglot->Pop(temp);
    }

    else
    {
     cout<<bs->GetBusnum()<<" bus never stay here!"<<endl;
    }

    //爲當前車讓位的車歸位
    while(!waitlot->Empty())
    {
     waitlot->Pop(temp);
     parkinglot->Push(temp);
    }
   }
   else
   {   //停車場沒有車
    cout<<"The parkinglot have no bus!"<<endl; 
   }
   return;
  }
 }

 else
 {
  cout<<"The End!"<<endl;
 }
 return;
}

int main(int argc, char* argv[])
{
    char ch;
 char oper[10];
 unsigned int num=0;
 float time=0;
 Bus* bs=new Bus();
 Stack* park=new Stack(6);
 Stack* wait=new Stack();

 cout<<"Please Choose the operation:"<<endl<<"c: Conitue  e: Exit"<<endl;

 cin>>ch;
 while(ch=='c')
 {
  cin>>oper>>num>>time;
  cout<<"/t/t/t";
  bs->SetBus(oper,num,time,0);
  Process(park,wait,bs);
  
  cout<<"Please Choose the operation:"<<endl<<"c: Conitue  e: Exit"<<endl;
  cin>>ch;
 }
 return 0;
}

發佈了31 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章