标题:电梯调度算法
只看楼主
alonecto
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-31
结帖率:0
已结贴  问题点数:20 回复次数:4 
电梯调度算法
电梯调度算法
要求:
a.    系统用一个控制的2部电梯往返于1-n层楼
b.    2部电梯在每层楼共用同一组控制按钮,电梯是上升还是下降应有相应的指示
c.    乘客要去的楼层数可手动输入并显示, 电梯运行的楼层数可自动显示



 
请论坛各位C++高手帮忙写个代码,感激不尽!有回报,写出来有运行结果的,淘宝购买赠送礼物!
搜索更多相关主题的帖子: 电梯 
2013-05-31 17:14
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
得分:10 
这算不算伸手党?

授人以渔,不授人以鱼。
2013-05-31 19:31
alonecto
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-31
得分:0 
回复 2楼 TonyDeng
算是吧,讲话算数的。
2013-05-31 19:32
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
得分:10 
那就勉为其难的封了吧...

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-06-01 09:39
alonecto
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-5-31
得分:0 
#include<iostream>
using namespace std;
const int MostLayers = 100;
class CElevator
{
public:
     CElevator();//构造函数
     CElevator(int uLayers, int lLayers);//构造函数
     CElevator(CElevator &myElevator);//拷贝构造函数
     void PressOuterButton(int direction, int floor);//电梯外部各楼层操作按钮
     void PressInnerDigitalButton(int floor);//电梯内部数字按钮
     void PressOpen_CloseButton(int operate);//电梯内部开合控制按钮
     void PressAlert();//电梯内部报警按钮
     void run();//电梯运行
     int States[5];//电梯状态
     int DigitalButton[MostLayers];
     int OuterDownButton[MostLayers];
     int OuterUpButton[MostLayers];
     ~CElevator();//析构函数
protected:
     void OpenElevator();//打开电梯
     void CloseElevator();//关闭电梯
     void UserOperate();//由于程序单线程运行,因此将用户的所有操作集中在每一层停止的时候完成。
     const int UpperLayers; //电梯最高能到达的楼层
     const int LowerLayers; //电梯最低能到达的楼层  
};

CElevator::CElevator():UpperLayers(10),LowerLayers(1)
{
     States[0] = 0;//初始状态为停止
     States[1] = 1;//初始楼层在1楼
     States[2] = 0;//初始方向为向上
     States[3] = 1;//初始电梯为合
     States[4] = 0;//初始电梯正常
     for(int i=0;i<MostLayers;i++)
     {
          DigitalButton[i] = 0;//记录电梯内i楼层是否被按下
          OuterDownButton[i] = 0;//记录电梯外i楼层向下按钮是否被按下
          OuterUpButton[i] = 0;//记录电梯外i楼层向上按钮是否被按下
     }
}
 
CElevator::CElevator(int uLayers, int lLayers):UpperLayers(uLayers),LowerLayers(lLayers)
{
     States[0] = 0;//初始状态为停止
     States[1] = 1;//初始楼层在1楼
     States[2] = 0;//初始方向为向上
     States[3] = 1;//初始电梯为合
     States[4] = 0;//初始电梯正常
     for(int i=0;i<MostLayers;i++)
     {
          DigitalButton[i] = 0;
          OuterDownButton[i] = 0;
          OuterUpButton[i] = 0;
     }   
}
 
CElevator::CElevator(CElevator &myElevator):UpperLayers(myElevator.UpperLayers),LowerLayers( myElevator.LowerLayers)
{
     States[0] = myElevator.States[0];
     States[1] = myElevator.States[1];
     States[2] = myElevator.States[2];
     States[3] = myElevator.States[3];
     States[4] = myElevator.States[4];
     for(int i=0;i<MostLayers;i++)
     {
          DigitalButton[i] = myElevator.DigitalButton[i];  
          OuterDownButton[i] = myElevator.DigitalButton[i];  
          OuterUpButton[i] = myElevator.DigitalButton[i];
     }
}
 
CElevator::~CElevator()
{
}
 
void CElevator::PressOuterButton(int direction, int floor)
{
     if(direction==0)
          OuterUpButton[floor-LowerLayers-1] = 1;
     else
          OuterDownButton[floor-LowerLayers-1] = 1;
}
 
void CElevator::PressInnerDigitalButton(int floor)
{
     DigitalButton[floor-LowerLayers-1] = 1;
}
 
void CElevator::PressOpen_CloseButton(int operate)
{
     if(operate == 0)//打开电梯
     {
         OpenElevator();
     }
     else//关闭电梯
     {
         CloseElevator();
     }
}
 
void CElevator::OpenElevator()
{
     if(States[3] == 0)
     {
          cout<<"电梯已打开,请先出后进!"<<endl<<endl;
     }
     else
     {
         cout<<"电梯正在打开… 电梯已打开,请先出后进!"<<endl<<endl;
         States[3] = 0;
     }
}
 
void CElevator::CloseElevator()
{
     if(States[3] == 1)
     {
          cout<<"电梯已关闭,请保持安静!"<<endl<<endl;
     }
     else
     {
          cout<<"电梯正在关闭… 电梯已关闭,请保持安静!"<<endl<<endl;
          States[3] = 1;
     }
}
 
void CElevator::PressAlert()//按报警按钮
{
     cout<<"电梯内部有人报警,请援助!"<<endl;
     States[4] = 1;
}
 
void CElevator::UserOperate()
{
     int alm;
     cout<<"按报警按钮吗?输入1表示按,其他表示不按:";
     cin>>alm;
     cout<<endl;
     if(alm==1)
     {
          States[4] = 1;
          cout<<"电梯出现故障,请维修!维修好后请输入0!"<<endl;
          while(States[4]==1)
          {   
              cout<<"正在维修中...  维修好后请输入0!"<<endl;
              cin>>alm;
              cout<<endl;
              if(alm == 0 || alm ==1)
              {
                   States[4] = alm;
              }
         }
     }
     OpenElevator();
     //电梯内部输入到达楼层
     cout<<"请电梯内乘客输入所要到达的楼层!"<<endl;
     cout<<"多个楼层中间用空格隔开,结束输入请按0:";
     int floor = 1;
     while(true)
     {
          cin>>floor;
          if(floor==0)
          {
              break;
          }
          if((floor>=LowerLayers) && (floor<=UpperLayers) && (floor!=States[1]))
          {
              if(floor<0)//地下
              {
                  DigitalButton[floor-LowerLayers] = 1;
              }
              else//地上
              {
                  DigitalButton[floor-LowerLayers-1] = 1;//上floor楼层的按钮被按下
              }
          }
     }
     cout<<endl;
     //电梯外部各楼层输入上楼信号
     cout<<"请电梯外部乘客输入所在楼层的上楼信号!"<<endl;
     cout<<"多个楼层用空格隔开,结束输入请按0:";
     while(true)
     {
          cin>>floor;
          if(floor==0)
          {
              break;
          }
          else
          {
              if((floor>=LowerLayers) && (floor<UpperLayers) && (floor!=States[1]))
              {
                   if(floor<0)//地下
                   {
                        OuterUpButton[floor-LowerLayers] = 1;//floor楼层被按下
                   }
                   else//地上
                   {
                        OuterUpButton[floor-LowerLayers-1] = 1;//floor楼层被按下
                   }
              }
         }
     }
     cout<<endl;
     //电梯外部各楼层输入下楼信号
     cout<<"请电梯外部乘客输入所在楼层的下楼信号,"<<endl;
     cout<<"多个楼层用空格隔开,结束输入请按0:";
     while(true)
     {
          cin>>floor;
          if(floor==0)
          {
              break;
          }
          else
          {
              if((floor>LowerLayers) && (floor<=UpperLayers) && (floor!=States[1]))
              {
                   if(floor<0)//地下
                   {
                        OuterDownButton[floor-LowerLayers] = 1;//floor楼层被按下
                   }
                   else//地上
                   {
                        OuterDownButton[floor-LowerLayers-1] = 1;//floor楼层被按下
                   }
              }
         }
     }
     cout<<endl;
}
 
void CElevator::run()
{
     while(true)
     {
          if(States[0]==0)
         {
              cout<<"电梯停在"<<States[1]<<"层!"<<endl;
              if(States[2]==0)//向上
              {
                   if(States[1]<0)//地下
                   {
                        OuterUpButton[States[1]-LowerLayers] = 0;//将记录电梯States[1]层有向上的标志取消
                        DigitalButton[States[1]-LowerLayers] = 0;//将记录电梯在States[1]层停靠的标志取消
                   }
                   else//地上
                   {
                        OuterUpButton[States[1]-LowerLayers-1] = 0;//将记录电梯States[1]层有向上的标志取消
                        DigitalButton[States[1]-LowerLayers-1] = 0;//将记录电梯在States[1]层停靠的标志取消
                   }                 
              }
              else//向下
              {
                   if(States[1]<0)//地下
                   {
                        OuterDownButton[States[1]-LowerLayers] = 0;//将记录电梯States[1]层有向下的标志取消
                        DigitalButton[States[1]-LowerLayers] = 0;//将记录电梯在States[1]层停靠的标志取消
                   }
                   else//地上
                   {
                        OuterDownButton[States[1]-LowerLayers-1] = 0;//将记录电梯States[1]层有向下的标志取消
                        DigitalButton[States[1]-LowerLayers-1] = 0;//将记录电梯在States[1]层停靠的标志取消
                   }   
              }   
              UserOperate();
              States[0] = 1;
         }
         else
         {
              if(States[2]==0)//向上
              {
                   States[1]++;
                   if(States[1]==0)
                   {
                        States[1]++;//没有层
                   }
                   else
                   {
                       cout<<"电梯向上运行,将要到达"<<States[1]<<"层!"<<endl<<endl;
                       if(States[1]==UpperLayers)//向上到最顶,必定要停,且方向变为向下
                       {                                
                           States[2] = 1;//变方向
                           States[0] = 0;
                       }
                       else
                       {
                            if(OuterUpButton[States[1]-LowerLayers-1]==0 && DigitalButton[States[1]-LowerLayers-1]==0)
                            {
                                 cout<<"没有乘客在"<<States[1]<<"层上下,电梯继续向上运行!"<<endl<<endl;                                 
                            }
                            else
                            {
                                 States[0] = 0;
                            }
                       }                       
                   }
              }
              else//向下
              {
                   States[1]--;
                   if(States[1]==0)
                   {
                        States[1] = 0;//没有层
                   }
                   else
                   {
                       cout<<"电梯向下运行,将要到达"<<States[1]<<"层!"<<endl<<endl;
                       if(States[1]==LowerLayers)//向下到最底,必定要停,且方向变为向上
                       {
                            States[2] = 0;//变方向
                            States[0] = 0;
                       }
                       else
                       {
                            if(OuterDownButton[States[1]-LowerLayers-1]==0 && DigitalButton[States[1]-LowerLayers-1]==0)
                            {
                                 cout<<"没有乘客在"<<States[1]<<"层上下,电梯继续向下运行!"<<endl<<endl;                                 
                            }
                            else
                            {
                                 States[0] = 0;
                            }
                       }
                   }            
              }              
         }         
     }
}

void main()
{
    CElevator CEl1;
    CEl1.run();
}
2013-06-01 10:44



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-412678-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.200573 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved