电梯调度程序的实施 by郭莉莉&&李亚文

一、开门见山,代码粘

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace lift
{
    public partial class Form1 : Form
    {
        int destion = 0;//目的楼层
        int number = 0;//所到楼层
        public Form1()
        {
            InitializeComponent();
            //this.skinEngine1.SkinFile = "Vista2_color5.ssk";
        }

        private void button21_Click(object sender, EventArgs e)
        {
            if (destion == 0)
            {
                lblWarming.Visible = true;
            }
            else
            {
                tmrSeconds.Start();
            }
            
        }

        private void lift_click(object sender, EventArgs e)
        {
            if (destion == 0)
            {
                destion = Convert.ToInt32(((Button)sender).Tag);
                ((Button)sender).BackColor = Color.Red;
                if (destion > 21)
                {
                    number = destion / 100 * 100 + number;
                }
            }
            else
            {
                MessageBox.Show("你只能选择一个楼层","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        

        private void tmrSeconds_Tick(object sender, EventArgs e)
        {
           
           
            if (number < destion)
            {
               // FormmoveUp();
                number++;   
                foreach (Control item in this.Controls)
                {
                   
                    if (item is Button)
                    {
                        if(number<=destion)
                        {
                            item.BackColor = Color.LightGray;
                        }
                        int index = Convert.ToInt32(item.Tag);
                        if (index == number)
                        {
                            item.BackColor = Color.Green;
                        }
                    }
                }
                lblPostion.Text = (number%100).ToString();
            } 
            else if(number>destion)
            {
              //  FormmoveDown();
                number--;
                foreach (Control item in this.Controls)
                {
                    if (item is Button)
                    {
                        //number=destion / 100 * 100 + number;
                        if (number >= destion)
                        {
                            item.BackColor = Color.LightGray;
                        }
                        int index = Convert.ToInt32(item.Tag);
                        if (index == number)
                        {
                            item.BackColor = Color.Green;
                        }
                    }
                }
                lblPostion.Text = (number%100).ToString();
            }
            else
            {
                tmrSeconds.Stop();
                foreach(Control item in this.Controls )
                {
                    if (item is Button)
                    {
                        int index = Convert.ToInt32(item.Tag);
                        if (index == destion)
                        {
                            item.BackColor = Color.LightGray;
                        }
                    }
                }
                MessageBox.Show(destion%100+"层到了", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                destion = 0;
                number = number % 100;
            }
        }

        private void button21_MouseLeave(object sender, EventArgs e)
        {
            lblWarming.Visible = false;
        }

        private void lblWarming_Click(object sender, EventArgs e)
        {

        }

    }
}

二、实现思路

1、首先考虑一部电梯运行的情况,乘客上电梯后,通过按按钮确定目的楼层,因此,按钮的click事件是同一个道理,因此用一个函数实现即lift_click;

2、其次考虑按下按钮后,电梯上升和下降,首先比较当前楼层与目的楼层的数值大小,如果目的楼层destion大于当前楼层number,则上升一层则number(所到楼层)加一,否则number-1,将按钮变成绿色表示到达该楼层;

3、然后思考当到达该楼层时候,给出提示框提示。

4、最后考虑四部电梯的情况。

三、开发日志

3月12日 星期三 14:00-16:30

万事开头难,我们今天讨论了具体思路和编程的策略,并且查找了很多资料,做了个界面:

3月13日星期四 16:30-18:50

一部电梯调度的click事件,界面很简单,做好界面以后开始写函数,完成如下:

private void lift_click(object sender, EventArgs e)
        {
            if (destion == 0)
            {
                destion = Convert.ToInt32(((Button)sender).Tag);
                ((Button)sender).BackColor = Color.Red;
            }
            else
            {
                MessageBox.Show("你只能选择一个楼层","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

3月14日 星期五 18:00-22:00

按下电梯运行按钮,实现了电梯上下楼的功能:

 private void tmrSeconds_Tick(object sender, EventArgs e)
        {
           
            if (number < destion)
            {
                FormmoveUp();
                number++;   
                foreach (Control item in this.Controls)
                {
                    if (item is Button)
                    {
                        if(number<=destion)
                        {
                            item.BackColor = Color.LightGray;
                        }
                        int index = Convert.ToInt32(item.Tag);
                        if (index == number)
                        {
                            item.BackColor = Color.Green;
                        }
                    }
                }
                lblPostion.Text = number.ToString();
            } 
            else if(number>destion)
            {
                FormmoveDown();
                number--;
                foreach (Control item in this.Controls)
                {
                    if (item is Button)
                    {
                        if (number >= destion)
                        {
                            item.BackColor = Color.LightGray;
                        }
                        int index = Convert.ToInt32(item.Tag);
                        if (index == number)
                        {
                            item.BackColor = Color.Green;
                        }
                    }
                }
                lblPostion.Text = number.ToString();
            }
            else
            {
                tmrSeconds.Stop();
                foreach(Control item in this.Controls )
                {
                    if (item is Button)
                    {
                        int index = Convert.ToInt32(item.Tag);
                        if (index == destion)
                        {
                            item.BackColor = Color.LightGray;
                        }
                    }
                }
                MessageBox.Show(destion+"层到了", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                destion = 0;
            }
        }


这样程序已经可以运行了:

运行结果如下

2014年3月16日 14:00-18:00

编写四个电梯的程序,首先修改界面,但是遇到了很大的问题,如果复制粘贴以后的按钮属性全都相同,四个电梯就像一个电梯一样,后来我们看了半天也没找到解决方案,于是放弃了。界面修改如下:

2014年3月17日 星期一  18:00-23:00

提交程序在即,但是我们还是一个电梯,我们本来想就此罢手,但是我们能这么做吗?不能!!!!!!我们于是仔细看了问题的症结,发现只要修改Tag值和number值就可以达到效果,于是,将第二个电梯的值加了100,第三个电梯的值加了200,第四个电梯Tag值加了300然后修改了number让他变成postion/100*100+number实现利用整型除法的弊端来解决了问题。得到了如下结果:

还有很多不足之处!

原文地址:https://www.cnblogs.com/guolili/p/3606350.html