【面向对象程序设计】作业二

电梯调度

  • 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;
};

转载于:https://www.cnblogs.com/stolf/p/8999710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值