使用devc++实现以下功能 电梯仿真系统:某中心共40层,设有电梯10部(用E0~E9标识)。 限定条件: (1) 每部电梯的最大载客量均为K人(K值可以根据仿真情况在10~18人之间确定)。 (2) 仿真开始时,各电梯都位于第1层,为空梯。 (3) 仿真开始后,有N位乘客(0 < N < 1000)在M分钟(0 < M < 10)内随机地到达该国际会展中心的1层,开始乘梯活动。 (4) 电梯运行的方向由先发出请求者决定,不允许后发出请求者改变电梯的当前运行方向,除非是未被请求的空梯。 (5) 当某层有乘客按下乘梯按钮时,优先考虑离该层最近的、满足条件(4)、能够最快达到目标层的电梯。 (6) 不允许电梯超员。超员时应给出提示。 (7) 电梯运行速度为S秒/层(S值可以根据仿真情况在1~5之间确定),每人上下时间为T秒(T值可以根据仿真情况在2~10之间确定)。
时间: 2024-02-06 15:11:26 浏览: 120
电梯仿真系统,VC++开发
5星 · 资源好评率100%
好的,这是一个比较复杂的任务,需要用到C++的面向对象编程思想。首先,我们需要设计一个电梯类,包含电梯的状态、运行方向、当前层数、载客量等属性,以及电梯的控制方法。然后,我们需要设计一个乘客类,包含乘客的状态、目标楼层等属性,以及乘客的控制方法。最后,我们需要在主函数中模拟乘客的到达和电梯的运行,以及相应的输出。
以下是一个简单的示例代码,供参考:
```
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
const int MAX_FLOOR = 40;
const int MAX_ELEVATOR = 10;
class Elevator {
public:
Elevator(int id) {
m_id = id;
m_direction = 0;
m_floor = 1;
m_capacity = 10;
m_current_capacity = 0;
m_time_per_floor = 1;
m_time_per_person = 3;
}
int get_id() const {
return m_id;
}
int get_direction() const {
return m_direction;
}
int get_floor() const {
return m_floor;
}
int get_capacity() const {
return m_capacity;
}
int get_current_capacity() const {
return m_current_capacity;
}
void set_direction(int direction) {
m_direction = direction;
}
void set_capacity(int capacity) {
m_capacity = capacity;
}
void add_person() {
m_current_capacity++;
}
void remove_person() {
m_current_capacity--;
}
int time_to_floor(int floor) const {
return abs(floor - m_floor) * m_time_per_floor;
}
int time_to_person() const {
return m_time_per_person;
}
void move_to_floor(int floor) {
m_floor = floor;
}
bool is_full() const {
return m_current_capacity >= m_capacity;
}
bool is_empty() const {
return m_current_capacity == 0;
}
private:
int m_id;
int m_direction;
int m_floor;
int m_capacity;
int m_current_capacity;
int m_time_per_floor;
int m_time_per_person;
};
class Passenger {
public:
Passenger(int id) {
m_id = id;
m_state = 0;
m_current_floor = 1;
m_target_floor = rand() % MAX_FLOOR + 1;
}
int get_id() const {
return m_id;
}
int get_state() const {
return m_state;
}
int get_current_floor() const {
return m_current_floor;
}
int get_target_floor() const {
return m_target_floor;
}
void set_state(int state) {
m_state = state;
}
void move_to_floor(int floor) {
m_current_floor = floor;
}
private:
int m_id;
int m_state;
int m_current_floor;
int m_target_floor;
};
int main() {
srand(time(nullptr));
vector<Elevator> elevators;
for (int i = 0; i < MAX_ELEVATOR; i++) {
elevators.push_back(Elevator(i));
}
vector<Passenger> passengers;
int num_passengers = rand() % 1000 + 1;
for (int i = 0; i < num_passengers; i++) {
passengers.push_back(Passenger(i));
}
int time = 0;
while (time < 10) {
// simulate passenger arrival
int num_new_passengers = rand() % 10 + 1;
for (int i = 0; i < num_new_passengers; i++) {
passengers.push_back(Passenger(passengers.size()));
}
// handle passengers waiting on floors
for (int floor = 1; floor <= MAX_FLOOR; floor++) {
vector<Passenger> waiting;
for (auto& passenger : passengers) {
if (passenger.get_state() == 0 && passenger.get_current_floor() == floor) {
waiting.push_back(passenger);
passenger.set_state(1);
}
}
if (!waiting.empty()) {
// find the closest elevator
int min_time = MAX_FLOOR + 1;
int min_index = -1;
for (int i = 0; i < MAX_ELEVATOR; i++) {
if (elevators[i].is_empty() || elevators[i].get_direction() == 0 || elevators[i].get_direction() == (floor - elevators[i].get_floor()) / abs(floor - elevators[i].get_floor())) {
int time_to_elevator = elevators[i].time_to_floor(floor) + elevators[i].time_to_person() * waiting.size();
if (time_to_elevator < min_time) {
min_time = time_to_elevator;
min_index = i;
}
}
}
if (min_index != -1) {
// add passengers to elevator
for (auto& passenger : waiting) {
if (!elevators[min_index].is_full()) {
elevators[min_index].add_person();
passenger.set_state(2);
}
else {
cout << "Elevator " << elevators[min_index].get_id() << " is full!" << endl;
break;
}
}
// update elevator state
elevators[min_index].set_direction((floor - elevators[min_index].get_floor()) / abs(floor - elevators[min_index].get_floor()));
elevators[min_index].move_to_floor(floor);
}
}
}
// handle passengers in elevators
for (auto& elevator : elevators) {
if (elevator.is_empty()) {
continue;
}
int target_floor = elevator.get_floor();
bool found_target = false;
for (auto& passenger : passengers) {
if (passenger.get_state() == 2 && passenger.get_target_floor() == target_floor) {
found_target = true;
break;
}
}
if (!found_target) {
for (auto& passenger : passengers) {
if (passenger.get_state() == 2 && (elevator.get_direction() == 0 || elevator.get_direction() == (passenger.get_target_floor() - elevator.get_floor()) / abs(passenger.get_target_floor() - elevator.get_floor()))) {
target_floor = passenger.get_target_floor();
break;
}
}
}
if (target_floor == elevator.get_floor()) {
// remove passengers from elevator
vector<Passenger> exiting;
for (auto& passenger : passengers) {
if (passenger.get_state() == 2 && passenger.get_target_floor() == target_floor) {
exiting.push_back(passenger);
passenger.set_state(3);
}
}
for (auto& passenger : exiting) {
elevator.remove_person();
}
}
else {
// move elevator to target floor
elevator.move_to_floor(elevator.get_floor() + elevator.get_direction());
}
}
time++;
}
return 0;
}
```
这是一个简单的实现,可能还有很多需要完善的地方。如果需要更详细的功能或者界面,可以使用图形用户界面(GUI)库或者游戏开发引擎来实现。
阅读全文