[C++]课题设计:电梯问题(第三版,本人最终版)

//Bell.h

#ifndef BELL_H
#define BELL_H

class Bell{
public:
 void Ring()
 {
  cout << "电梯铃发出:叮!" << endl;
 }
};

#endif //BELL_H

 

//Building.h

#ifndef BUILDING_H
#define BUILDING_H

#include "Elevator.h"
#include "Floor.h"
#include "ElevatorDoor.h"

class Building {
public:
 void PressOutsideButton1(vector<Person>::iterator);
 void PressOutsideButton2(vector<Person>::iterator);
protected:
 Floor F1;
 Floor F2;
 Elevator Ele;
 ElevatorDoor EleDoor1;
 ElevatorDoor EleDoor2;
 Button OutsideButton1;
 Button OutsideButton2;
 void CheckElevatorOutsideButton();
};

inline void Building::PressOutsideButton1(vector<Person>::iterator iter)
{
 (*iter).PrintPersonNum();
 cout << "按下大楼第1层的电梯外开关,";
 if (Ele.State==1) EleDoor1.Open();
 else if (Ele.State==2)
 {
  Ele.Down2to1();
  OutsideButton1.LightOn();
 }
 else OutsideButton1.LightOn();
}

inline void Building::PressOutsideButton2(vector<Person>::iterator iter)
{
 (*iter).PrintPersonNum();
 cout << "按下大楼第2层的电梯外开关,";
 if (Ele.State==2) EleDoor2.Open();
 else if (Ele.State==1)
 {
  Ele.Up1to2();
  OutsideButton2.LightOn();
 }
 else OutsideButton2.LightOn();
}

inline void Building::CheckElevatorOutsideButton()
{
 EleDoor1.Close(); //执行电梯关门造作。
 EleDoor2.Close();
 if (Ele.State==1 ||Ele.State==2)//电梯状态静止
 {
  if (OutsideButton1.LightState()) //如果1层外的灯还亮着
   Ele.Down2to1(); //让电梯从2层去1层
  if (OutsideButton2.LightState()) //如果2层外的灯还亮着
   Ele.Up1to2(); //让电梯从1层去2层
 }
}

#endif // BUILDING_H

 

//Button.h

#ifndef BUTTON_H
#define BUTTON_H

#include <iostream>
using namespace std;

class Button{
public:
 Button():State(0){}
 void LightOn();
 void LightOff();
 bool LightState() {return State;}
private:
 bool State;     // 0:灭  1:亮
};

inline void Button::LightOn()
{
 if (!State)
 {
  State = 1;
  cout << "开关的灯亮了。"<<endl;
 }
}

inline void Button::LightOff()
{
 if (State)
 {
  State = 0;
  cout << "开关的灯灭了。"<<endl;
 }
}

#endif //BUTTON_H

 

//Elevator.cpp

#include "Elevator.h"

void Elevator::Next()
{
 if (LeftTime==0)
 {
  if (State==12)  //到达了2层
  {
   State=2;
   Stop();
   if (InsideButton2.LightState())
   {
    cout << "电梯内";
    InsideButton2.LightOff();
   }
   ElevatorBell.Ring();
  }
  if (State==21)  //到达了1层
  {
   State=1;
   Stop();
   if (InsideButton1.LightState())
   {
    cout << "电梯内";
    InsideButton1.LightOff();
   }
   ElevatorBell.Ring();
  }
 }
 else
 {
  LeftTime--;
  if (State==12) //正在升至2层
   Up();
  if (State==21) //正在将至1层
   Down();
 }
}

void Elevator::PressInsideButton1(vector<Person>::iterator iter) //去1楼
{
 (*iter).PrintPersonNum();
 cout <<"按下了电梯内去1层的开关," ;
 InsideButton1.LightOn();
 Down2to1();
}

void Elevator::PressInsideButton2(vector<Person>::iterator iter) //去2楼
{
 (*iter).PrintPersonNum();
 cout <<"按下了电梯内去2层的开关," ;
 InsideButton2.LightOn();
 Up1to2();
}

inline void Elevator::Down2to1()
{
 LeftTime = 5 - 1; //电梯从2层移至1层需要5秒钟。
 State = 21;
}

inline void Elevator::Up1to2()
{
 LeftTime = 5 - 1; //电梯从1层移至2层需要5秒钟。
 State = 12;
}

inline void Elevator::Down()
{
 cout << "电梯正在下降..."<<endl;
}

inline void Elevator::Up()
{
 cout << "电梯正在上升..."<<endl;
}

inline void Elevator::Stop()
{
 cout << "电梯停止升降。"<<endl;
}

 

//Elevator.h

#ifndef ELEVATOR_H
#define ELEVATOR_H

#include "Button.h"
#include "Person.h"
#include <vector>
#include "Bell.h"

class Elevator{
public:
 Elevator():State(1),LeftTime(0){} //开始时,电梯停在1楼
 void Next();
 void Up1to2();
 void Down2to1();
 void Up();
 void Down();
 void Stop();
 void PressInsideButton1(vector<Person>::iterator);
 void PressInsideButton2(vector<Person>::iterator);
 int State;// 1:停在一楼  2:停在二楼  12:从一到二楼  21:从二到一楼
private:
 int LeftTime;    // 剩余到达时间
 Button InsideButton1; //内按钮1
 Button InsideButton2; //内按钮1
 Bell ElevatorBell;    //电梯铃
};

#endif //ELEVATOR_H

 

//ElevatorDoor.h

#ifndef ELEVATORDOOR_H
#define ELEVATORDOOR_H

class ElevatorDoor{
public:
 ElevatorDoor():State(0) {}
 void Open();
 void Close();
 bool EleDoorState() {return State;}
private:
 bool State;  // 1:开  0:关
};

inline void ElevatorDoor::Close()
{
 if (State==1)
 {
  State = 0;
  cout << "电梯门自动关闭。"<<endl;
 }
}

inline void ElevatorDoor::Open()
{
 if (State==0)
 {
  State = 1;
  cout << "电梯门自动打开。"<<endl;
 }
}

#endif //ELEVATORDOOR_H

 

//ElevatorEvent.cpp

/*
   电梯问题(第三版)
   06030706 毛佳茗
   2005年12月13日
*/

#include "windows.h"
#include "Event.h"

int TimeLeft;
Event ElevatorEvent;

//SetTimer,用法参考自MSDN
VOID CALLBACK OnTimer(
  HWND hwnd,     // handle of window for timer messages
  UINT uMsg,     // WM_TIMER message
  UINT idEvent,  // timer identifier
  DWORD dwTime   // current system time
)
{
 ElevatorEvent.NextEvent(); //电梯事件
 TimeLeft--;
}

int main()
{
 int sec=0;
 cout << "请输入需要演示多少时间?(单位:秒)";
 cin >> TimeLeft;
 ElevatorEvent.Begin();//开始演示
 MSG msg; 
 UINT_PTR send=SetTimer(NULL,0, 1000, (TIMERPROC)OnTimer);
 while (GetMessage (&msg, NULL, 0, 0))
 { 
  if (TimeLeft==-1) break;
  TranslateMessage (&msg) ;
  DispatchMessage (&msg) ;
 }
 KillTimer(NULL,send);
 ElevatorEvent.End(); //结束演示
}

 

//Event.cpp

#include "Event.h"
#include <algorithm>
#include <cstdlib>

// 比较用户优先级函数
bool GreaterPersonStatus(const Person& p1, const Person& p2)
{
 return p1.Status > p2.Status;
}

// 下一个事件函数
void Event::NextEvent()
{
 cout << "/n现在时间:" << timing(1)  << "秒" << endl;  //打印并增加时间
 NextProducePerson(); //是否产生人?
 Ele.Next(); //下一个电梯事件
 //按人的状态3,2,1对用户组排序
 stable_sort(_Person.begin(),_Person.end(),GreaterPersonStatus);
 //根据用户情况,判断下一步动作
 for (PersonIterVec iter = _Person.begin() ; iter!= _Person.end(); ++iter)
 {
  switch( (*iter).WhichStatus() )
  {
  case 1:          //等待进入楼层
   WaitforFloor(iter);
   WaitforElevator(iter);
   break;
  case 2:          //等待进入电梯
   WaitforElevator(iter);
   break;
  case 3:          //正在电梯中
   if ( Ele.State == (*iter).WhichGoStorey() )  PersonLeave(iter);
   break;
  default:
   break;
  }
 }
    CheckElevatorOutsideButton();//检查是否亮灯,并关门
}


//等待进入楼层
void Event::WaitforFloor(PersonIterVec iter)
{
 (*iter).PrintPersonNum();
 if ((*iter).WhichOnStorey()==1) //在1层
  if (F1.OneCome())  (*iter).ChangeStatus(2); // 状态改变为等待进入电梯    
  else cout << "由于楼层被他人占用,需等待。"<< endl;
 if ((*iter).WhichOnStorey()==2)
  if (F2.OneCome())  (*iter).ChangeStatus(2); // 状态改变为等待进入电梯    
  else cout << "由于楼层被他人占用,需等待。"<< endl;
}


//等待进入电梯
void Event::WaitforElevator(PersonIterVec iter)
{
 if ((*iter).WhichStatus() ==2) //确定状态已经为等待进入电梯
 {   //在1层
  if ((*iter).WhichOnStorey()==1)  WaitforElevatorOnF1(iter);
  //在2层
  if ((*iter).WhichOnStorey()==2)  WaitforElevatorOnF2(iter);
 }
}


void Event::WaitforElevatorOnF1(PersonIterVec iter)   //等待进入电梯,在1层
{
 if (Ele.State == 1) //电梯所在层与用户所在层相同
 {
  if (OutsideButton1.LightState()) //如果灯亮着,则自动打开门,并熄灭灯
  {
   cout << "大楼第1层外电梯";
   OutsideButton1.LightOff();
   EleDoor1.Open();
  }
  if (!EleDoor1.EleDoorState())  PressOutsideButton1(iter); //如果门关着,则需按下按钮
  (*iter).ChangeStatus(3); //状态变为已近进入电梯
  F1.OneGo();//离开该层
  Ele.PressInsideButton2(iter);//按下电梯内去2层的按钮
  EleDoor1.Close();
 }
 //如果电梯不在该层,则
 else
  if (!OutsideButton1.LightState())  PressOutsideButton1(iter); //用户还没有去按,则
  else PersonWait(iter);
}


void Event::WaitforElevatorOnF2(PersonIterVec iter)   //等待进入电梯,在2层
{
 if (Ele.State == 2) //电梯所在层与用户所在层相同
 {
  if (OutsideButton2.LightState()) //如果灯亮着,则自动打开门,并熄灭灯
  {
   cout << "大楼第2层外电梯";
   OutsideButton2.LightOff();
   EleDoor2.Open();
  }
  if (!EleDoor2.EleDoorState())  PressOutsideButton2(iter); //如果门关着,则需按下按钮
  (*iter).ChangeStatus(3); //状态变为已近进入电梯
  F2.OneGo();//离开该层
  Ele.PressInsideButton1(iter);//按下电梯内去1层的按钮
  EleDoor2.Close();
 }
 //如果电梯不在该层,则
 else
  if (!OutsideButton2.LightState())  PressOutsideButton2(iter); //用户还没有去按,则
  else PersonWait(iter);
}


inline void Event::PersonWait(PersonIterVec iter)
{
 (*iter).PrintPersonNum();
 cout << "继续等待电梯。"<<endl;
}

void Event::PersonLeave(PersonIterVec iter)
{
 if ((*iter).WhichGoStorey() == 1) EleDoor1.Open();
 if ((*iter).WhichGoStorey() == 2) EleDoor2.Open();
 (*iter).ChangeStatus(4);
}

void Event::ProducePerson()
{
 Person NewPerson;
 _Person.push_back (NewPerson);
}

void Event::NextProducePerson()
{
 if (NextProducePersonTime == WhatNowTime())
 {
        ProducePerson();
  srand( (unsigned)time( NULL ) );
  NextProducePersonTime = WhatNowTime() + rand() % 16 + 5; //假设每隔5到20 秒人们随机到达每层。
  cout << "下一个人,将会在第" << NextProducePersonTime  << "秒出现。" << endl;
 }
}

 

//Event.h

#ifndef EVENT_H
#define EVENT_H

#include "Time.h"
#include "Person.h"
#include "Building.h"
#include <vector>
typedef vector<Person>::iterator PersonIterVec;

class Event : private Building, private Time {  // 继承 Building类 和 Time类
public:
 Event():NextProducePersonTime(0){};
 void Begin();
 void End();
 void NextEvent();
private:
 void NextProducePerson();
 void ProducePerson();
 void PersonWait(PersonIterVec);
 void PersonLeave(PersonIterVec);
 void WaitforFloor(PersonIterVec);
 void WaitforElevator(PersonIterVec);
 void WaitforElevatorOnF1(PersonIterVec);
 void WaitforElevatorOnF2(PersonIterVec);
 int NextProducePersonTime;
 vector <Person> _Person;
};

inline void Event::Begin()
{
 cout << "/n开始演示电梯事件:" << endl ;
}

inline void Event::End()
{
 cout << "/n电梯事件演示结束。" << endl;
}

#endif //EVENT_H

 

//Floor.h

#ifndef FLOOR_H
#define FLOOR_H

class Floor {
public:
 Floor():State(0){}
 bool OneCome();  // 要来一个人
 void OneGo() {State = 0;} // 走了一个人
private:
 bool State;      // 0:没有人  1:有人了
};

inline bool Floor::OneCome()
{
 if (State==0)
 {
  cout << "来到电梯门外,等候电梯。" << endl;
  State=1;
  return 1;
 }
 else return 0;
}

#endif //FLOOR_H

 

//Person.cpp

#include "Person.h"

int Person::Num = 0;

void Person::ChangeStatus(int i)
{
 switch(i)
 {
 case 1:
  Status = 1;
  cout << "用户" << PersonNum << "的状态转变为:" << "等待进入楼层。" << endl;
  break;
 case 2:
  Status =2;
  cout << "用户" << PersonNum << "的状态转变为:" << "等待进入电梯。" << endl;
  break;
 case 3:
  Status = 3;
  cout << "用户" << PersonNum << "的状态转变为:" << "已经进入电梯。" << endl;
  break;
 case 4:
  Status = 4;
  cout << "用户" << PersonNum << "的状态转变为:" << "走出电梯,去做其它事情。" << endl;
  break;
 }
}

 

//Person.h

#ifndef PERSON_H
#define PERSON_H

#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

class Person {
public:
 Person():Status(1)
 {
  srand( (unsigned)time( NULL ) );
  OnStorey = rand() % 2 + 1;
  if (OnStorey==1) GoStorey = 2;
  else GoStorey = 1;
  PersonNum = ++Num;
  cout << "一个人到来,编号:" << PersonNum << ",他将会在" << OnStorey << "层守候电梯,然后去" << GoStorey << "层。" <<endl;
 }
 friend bool GreaterPersonStatus(const Person&, const Person&);
 void ChangeStatus(int);
 void PrintPersonNum(){ cout << "用户" << PersonNum; };
 int WhichOnStorey() {return OnStorey;};
 int WhichGoStorey() {return GoStorey;};
 int WhichStatus() {return Status;};
private:
 int OnStorey;   // 所在层
 int GoStorey;   // 要去的层
 int Status;     // 1,等待进入楼层 2,等待进入电梯 3,在电梯中  4,已近离开电梯
 int PersonNum;  // 编号
 static int Num; // 已经产生的总人数
};

#endif //PERSON_H

 

//Time.h

#ifndef TIME_H
#define TIME_H

class Time {
public:
 Time():NowTime(-1){} //初始时间为-1
 int WhatNowTime() { return NowTime; }; //返回现在的时间
 int timing (int) ; //时间增加
private:
 int NowTime;
};

inline int Time::timing(int AddTime)
{
 NowTime += AddTime;
 return NowTime;
}

#endif //TIME_H

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页