经典的电梯模拟问题 [英] The classic elevator simulation problem

查看:54
本文介绍了经典的电梯模拟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello group

我得到了这个经典电梯模拟的代码。我有一些

的问题。

我觉得它可以简化......你能帮助我吗?

这里'' s代码:

班级按钮

{

enum button_status {off,on};

public:

按钮(button_status = off);

void switch_on();

void switch_off();

bool is_off (); // c

bool is_on(); // c

私人:

button_status状态;

};


按钮::按钮(button_status s):state(s){}


void button :: switch_on()

{

if(state == on)

cout<< 请耐心等待\ n; //它已经被推了

其他

州=开;

}


void button :: switch_off()

{

state = off;

}


bool button :: is_off()// c

{

返回状态==关闭;

}


bool button :: is_on()// c

{

return state == on;

}


类面板

{

public:

Panel(int n);

~Panel();

void press(int n);

void reset(int n);

bool is_off(int n)const ;

bool is_on(int n)const;

bool is_valid(int n)const;

private:

按钮**面板;

int number;

面板(const Panel&);

};


Panel :: Panel(int n):panel(0),number(n)

{

panel = new button * [number + 1];

for(int i = 0; i< number + 1; ++ i)

panel [i] = new button;

}


面板::〜面板()

{

for(int i = number; i> = 0; --i)

删除面板[i];

删除[]面板;

}


bool Panel :: is_off(int n)const

{

返回面板[n] - > is_off();

}


bool Panel :: is_on(int n)const

{

返回面板[n] - > is_on() ;

}


bool Panel :: is_valid(int n)const

{

return n!= 13;

}


void Panel :: press(int n)

{

if(!is_valid(n))

cout<< 没有这样的楼层!\ n;

其他

小组[n] - > switch_on();

}


void Panel :: reset(int n)

{

panel [n] - > switch_off();

}


班级电梯

{

公共:

电梯(int = 1,int = 0);

~电梯();

无效提示();

私人:

面板按钮;

int current_floor;

const int top_floor;

bool button_active(int)const;

void press(int);

void close_doors();

bool floor_is_valid(int)const;

elevator(const elevator&);

};


电梯::电梯(int n,int w):buttons(n),current_floor(1),

top_floor(n){}


电梯::〜电梯()

{

cout<< 电梯将自毁; \\ n;

}


无效电梯::提示()

{

cout<< 键入您想要访问的楼层,从1到 <<

top_floor<< ",0关闭门,EOF退出:" ;;

int floor;

while(!(cin> floor).eof())

{

if(floor == 0)

{

if(!button_active(1)&& current_floor!= 1)

按(1);

if(button_active(1))

close_doors();

}

else if(floor< 1 || floor top_floor)

cout<< ---地板 <<地板<< "无效\ n" ;;

else if(floor == current_floor)

cout<< ---你已经到达了你的楼层\ n;

其他

按(楼层);

cout<< ; 下一层:;

}

}


void elevator :: press(int n)

{

buttons.press(n);

}


void elevator :: close_doors()

{

cout<< 门正在关闭\ n;

cout<< \\\\\\\\ a" ;;

//如果按下的按钮高于当前的楼层

/ / floor,然后电梯总是向上移动。

if(button_active(current_floor))

cout<< Elevator accending\\\
;

else

cout<< Elevator decending\\\
;

sleep(1);

//只要地板按钮关闭就保持循环

while(buttons.is_off(current_floor))

{

if(button_active(current_floor))

++ current_floor;

其他

--current_floor;

if(buttons.is_off(current_floor)&&

floor_is_valid(current_floor))

{

cout<< \tPassing floor << current_floor<< ''\ a''<< ''\ n'';

睡觉(1);

}

}

cout< < \tNow on floor << current_floor<< " \a\a\a" << ''\ n'';

sleep(1);

buttons.reset(current_floor);

cout<< 门是打开的;

睡觉(1);

}


bool elevator :: button_active( int f)const

{

for(int i = f; i< = top_floor; ++ i)

if(button。 is_on(i))

返回true;

返回false;

}


bool电梯:: floor_is_valid(int f)const

{

return buttons.is_valid(f);

}


int main()

{

const int top = 9;

电梯otis(上);

otis.prompt();

返回0;

}

Hello group
I got the code of this classic elevator simulation. Im having some
problem foloowing it.
And i feel it can be simplified... can u people help me?
here''s the code:
class button
{
enum button_status {off, on};
public:
button(button_status = off);
void switch_on();
void switch_off();
bool is_off() ; //c
bool is_on() ; //c
private:
button_status state;
};

button::button(button_status s) : state(s) {}

void button::switch_on()
{
if(state == on)
cout << "Please be patient \n"; //it''s already pushed
else
state = on;
}

void button::switch_off()
{
state = off;
}

bool button::is_off() //c
{
return state == off;
}

bool button::is_on() //c
{
return state == on;
}

class Panel
{
public:
Panel(int n);
~Panel();
void press(int n);
void reset(int n);
bool is_off(int n) const;
bool is_on(int n) const;
bool is_valid(int n) const;
private:
button** panel;
int number;
Panel(const Panel&);
};

Panel::Panel(int n) : panel(0), number(n)
{
panel = new button*[number + 1];
for(int i = 0; i < number + 1; ++i)
panel[i] = new button;
}

Panel::~Panel()
{
for(int i = number; i >= 0; --i)
delete panel[i];
delete [] panel;
}

bool Panel::is_off(int n) const
{
return panel[n]->is_off();
}

bool Panel::is_on(int n) const
{
return panel[n]->is_on();
}

bool Panel::is_valid(int n) const
{
return n != 13;
}

void Panel::press(int n)
{
if(!is_valid(n))
cout << "No such floor!\n";
else
panel[n]->switch_on();
}

void Panel::reset(int n)
{
panel[n]->switch_off();
}

class elevator
{
public:
elevator(int = 1, int = 0);
~elevator();
void prompt();
private:
Panel buttons;
int current_floor;
const int top_floor;
bool button_active(int) const;
void press(int);
void close_doors();
bool floor_is_valid(int) const;
elevator(const elevator&);
};

elevator::elevator(int n, int w) : buttons(n), current_floor(1),
top_floor(n) {}

elevator::~elevator()
{
cout << "Elevator will self destruct\n";
}

void elevator::prompt()
{
cout << "Key in the floor you would like to visit, from 1 to " <<
top_floor << ", 0 to close doors, EOF to exit: ";
int floor;
while(!(cin >floor).eof())
{
if(floor == 0)
{
if(!button_active(1) && current_floor != 1)
press(1);
if(button_active(1))
close_doors();
}
else if(floor < 1 || floor top_floor)
cout << "---Floor " << floor << " is not valid\n";
else if(floor == current_floor)
cout << "---You have reached your floor now\n";
else
press(floor);
cout << "Next floor: ";
}
}

void elevator::press(int n)
{
buttons.press(n);
}

void elevator::close_doors()
{
cout << "Doors are closing\n";
cout << "\a\a\a\a\a";
// If a button is pushed on a floor higher than the current
// floor, then the elevator always moves up.
if(button_active(current_floor))
cout << "Elevator accending\n";
else
cout << "Elevator decending\n";
sleep(1);
// Keep looping as long as the floor buton is off
while(buttons.is_off(current_floor))
{
if(button_active(current_floor))
++current_floor;
else
--current_floor;
if(buttons.is_off(current_floor) &&
floor_is_valid(current_floor))
{
cout << "\tPassing floor " << current_floor << ''\a'' << ''\n'';
sleep(1);
}
}
cout << "\tNow on floor " << current_floor << "\a\a\a" << ''\n'';
sleep(1);
buttons.reset(current_floor);
cout << "Doors are open\n";
sleep(1);
}

bool elevator::button_active(int f) const
{
for(int i = f; i <= top_floor; ++i)
if(buttons.is_on(i))
return true;
return false;
}

bool elevator::floor_is_valid(int f) const
{
return buttons.is_valid(f);
}

int main()
{
const int top = 9;
elevator otis(top);
otis.prompt();
return 0;
}

推荐答案

< wh ************* @ gmail.comwrote:
<wh*************@gmail.comwrote:

我得到了这个经典电梯模拟的代码。我有一些

的问题。

我觉得它可以简化......你能帮助我吗?

这里'' s代码:
I got the code of this classic elevator simulation. Im having some
problem foloowing it.
And i feel it can be simplified... can u people help me?
here''s the code:



< snip>


我会同意它看起来很笨重,大概有205行和那里还没有

人。 (经典的电梯问题肯定会让人参与其中,

对吧?)看起来有些人说过,建立一个

工业强度whatchamcallit,然后走开了。这就是

工程师因为这次谈话而产生的结果。


我会为每层代表一个人的队列

等待在那一层。所以基本的(最有趣的)数据结构将是一个队列数组。一楼排队的人数最多,早上是b
,晚上是最小的。假设有一部电梯

建筑物(你不知道目标是什么):汽车监控队列并且

接受来自乘客的楼层选择。如果

建筑物有几部电梯,他们会分享一系列人员,但

有自己的数据,关于*那辆*车的乘客的目的地。 br />

会有一个人员生成器填充队列的对象。


我认为你已经跳过了设计阶段,并开始编写代码,

这是一件非常自然的事情。简单地写(或重写你所拥有的

)规格甚至可能是有帮助的。这样做会有

显示你可以接受比电梯更多的乘客设计

随身携带。奥蒂斯先生确保安全,但我确信当

电缆断线时非常可怕。


如果你能找到一些很好的参考资料。云"图表,他们可能会帮你

你。

<snip>

I will agree that it looks awful bulky, about 205 lines and there are no
people yet. (The classic elevator problem surely has people involved,
right?) It has the look of something where someone has said, "Build me an
industrial strength whatchamcallit", and then walked away. This is what the
engineer produced as a result of that conversation.

I would have a queue for each floor representing the number of people
waiting on that floor. So the basic (most interesting) data structure would
be an array of queues. The queue on the first floor has the most people in
the morning and the smallest number in the evening. Assuming a one elevator
building (you don''t say what the goal is): The car monitors the queues and
accepts floor selections from the passengers as they get on. If the
building has several elevators, they share the array of queues of people but
have their own data as to the destination of the passengers on *that* car.

There would be a "people generator" object to populate the queues.

I think you have skipped the design phase, and started out by writing code,
a very natural thing to do. Simply writing (or re-writing what you have
been given) the specifications can even be helpful Doing so would have
revealed that you can accept more passengers than the elevator is designed
to carry. Mr. Otis made it safe, but I am sure it is quite scary when the
cables break.

If you can find some nice reference to "cloud" diagrams, they might help
you.



我会同意它看起来很糟糕,大约205行,还没有

的人。 (经典的电梯问题肯定有人参与,

对吧?)
I will agree that it looks awful bulky, about 205 lines and there are no
people yet. (The classic elevator problem surely has people involved,
right?)



实际上问题不涉及队列,它更简单,在

不同楼层,电脑上的用户输入人们想要去的楼层数。

Actually the problem doesnt involve queues, it''s much simpler, at
different floors the user on the computer enters the number of the
floor the people want to go to.


我认为你已经跳过了设计阶段,并开始编写代码,

a非常自然的事情。
I think you have skipped the design phase, and started out by writing code,
a very natural thing to do.



你是绝对正确的,没有设计阶段,我只是得到这个

代码,并试图遵循逻辑......生病,尝试寻找一些

云图。你认为生病需要小组课吗?

奥蒂斯先生让它安全,但我确信当

You are absolutely right, there was no design phase, i just got this
code and was trying to follow the logic...ill try and look for some
cloud diagrams too. Do you think ill need the panel class?
Mr. Otis made it safe, but I am sure it is quite scary when the


时这是非常可怕的电缆坏了。
cables break.



LOL :)

感谢您的回复

LOL :)
Thank you for your reply


< wh ************* @ gmail.comwrote:
<wh*************@gmail.comwrote:

你是绝对正确的,没有设计阶段,我刚刚得到这个

代码,并试图遵循逻辑...生病尝试并寻找一些

云图。你觉得生病需要小组课吗?
You are absolutely right, there was no design phase, i just got this
code and was trying to follow the logic...ill try and look for some
cloud diagrams too. Do you think ill need the panel class?



我知道我不喜欢*小组课。我没有足够好地研究它,但是这确实是我认为有点过分的重要部分。这就是为什么我

指出建筑物有一个面板。和电梯(汽车)有一个* b $ b * * *面板。

I know I didn''t *like* the panel class. I didn''t study it enough to be
sure, but it was a substantial part of what I saw as overkill. That''s why I
made a point of "the building has a panel" and "the elevator (car) has a
*different* panel".


>


奥蒂斯先生保证安全,但我相信当$ b $时这是非常可怕的b
>

Mr. Otis made it safe, but I am sure it is quite scary when the

>电缆断开。
>cables break.



LOL :)


LOL :)



这是响应你的电梯名称。我认为那是我整个节目中最喜欢的部分:

That was in response to your name for the elevator. I think that was my
favorite part of the whole program :)


这篇关于经典的电梯模拟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆