电梯调度

  • C:

    • 用结构体保存一些必要信息,如乘客需求和最大楼层等
    • 读入信息
    • 用全局函数处理调度
    • 输出结果
  • C++:

    • 用类定义电梯,储存私有信息和公有接口
    • 利用接口读入信息
    • 利用其它类(单例类)或者成员函数进行决策调度
    • 利用接口输出信息
  • 区别:
    C是设计一个流程来处理数据,C++是设计一个类作为工具来进行使用。


代码

电梯类:

#pragma once
#include <queue>
#include <vector>
#include <cmath>
#include <iostream>
#include "Passenger.h"

enum Indicator {
	UP, DOWN, STOP, NONE
};

class Elevator {
public:
	Elevator(int maxFloor) : maxFloor(maxFloor), timer(0), currentFloor(0), achievedPassenger(0) { }
	void goToFloor(int); // 前往指定楼层
	void stop(); // 停靠在当前楼层
	void addPassenger(const Passenger&); // 添加乘客
	bool isAllAchieved() const; // 是否完成调度
	void Execute(Indicator); // 执行指令
	int getMaxFloor() const;
	int getTimer() const;
	int getCurrentFloor() const;
	const std::vector<Passenger>& getPassengers() const;

private:
	int maxFloor; // 最大楼层
	int timer; // 计时器
	int currentFloor; // 当前位置
	int achievedPassenger; // 已抵达乘客数
	std::vector<Passenger> passengers; // 乘客们
	std::queue<std::pair<int, int>> destinationQueue; // 电梯运行队列

	friend std::ostream &operator<< (std::ostream&, Elevator&);
};

乘客结构体:

#pragma once
#include <iostream>

struct Passenger { // 存储乘客信息
	Passenger() : isInside(false), isAchieved(false), waitingTime(0) { }
	int requestTime; // 请求时刻
	int initialFloor; // 起始楼层
	int destination; // 去往楼层
	int waitingTime; // 等待时间
	bool isInside; // 是否在电梯内
	bool isAchieved; // 是否到达目的地
	friend std::istream &operator>> (std::istream&, Passenger&);
};

决策类:

#pragma once
#include "Elevator.h"
#include <vector>
#include <algorithm>

class Strategy {
public:
	static Strategy* getInstance();

	Indicator decisionMaking();

	void setElevator(const Elevator*);

private:
	Strategy() {};
	const Elevator* elevator;
	static Strategy* instance;
};

相关文章:

  • 2022-03-03
  • 2022-02-09
  • 2021-10-24
  • 2021-05-23
  • 2021-10-13
  • 2021-08-12
猜你喜欢
  • 2022-01-13
  • 2021-08-11
  • 2021-06-25
  • 2021-12-13
  • 2021-06-19
  • 2022-02-14
相关资源
相似解决方案