电梯初步版本
经过去实际大楼的调查和一周的学习,初步完成了电梯的制作,但是这个版本的电梯功能并不是很全面,而且界面有待于改善,现在做出了测试版本,稍后进一步跟进新的版本,现在的版本初步完成的是电梯楼层之间上下调度的问题,还有四部电梯之间路程最短的调度,即一个总控控制四部电梯,比较四部电梯哪个电梯距离上电梯者最近,优先调度,还有一部电梯的调度,例如在向上的过程中首先响应向上的楼层,然后再调度其他任务,电梯允许多按钮,并且有颜色变化。其中用到了线程,主要用到了textbox控件,button按钮,panel容器,每个电梯基本用到的算法与前面所说的基本一致,此外在多textbox输出方面增加了foreach方法。分工问题:主要代码:
int floor = 1;//电梯所在楼层,初始状态为1楼 int direction = 0;//1状态向上,2向下,0静止 bool Outdir; bool[] Wait = new bool[21];//同向数组 bool[] Ywait = new bool[21];//反向数组 int Preprocess = 0;
public void Stateofelevator()//显示楼层和上下 { textBox1.Text = floor.ToString(); Control.ControlCollection cc = panel2.Controls; foreach (Control ct in cc) { if (ct is TextBox) { ct.Text = floor.ToString(); } } if (direction == 1) { textBox1.Text += '↑'; foreach (Control ct in cc) { if (ct is TextBox) { ct.Text+= '↑'; } } } else if (direction == 2) { textBox1.Text +='↓'; foreach (Control ct in cc) { if (ct is TextBox) { ct.Text += '↓'; } } } else { textBox1.Text += "--"; foreach (Control ct in cc) { if (ct is TextBox) { ct.Text += "--"; } } } direction = 0; }
public void Layerofelevator() { int i; for (i = floor; i <= 20; i++) { if (Wait[i] == true) { while(floor <=i) { if (floor == i) { direction = 0; } else { direction = 1; } Stateofelevator(); Thread.Sleep(1000); Application.DoEvents(); floor++; } Thread.Sleep(5000); floor--; Preprocess--; Wait[i] = false; } } for (i = floor; i>=0; i--) { if (Wait[i] == true) { while (floor >= i) { if (floor == i) { direction = 0; } else { direction = 2; } Stateofelevator(); Thread.Sleep(1000); Application.DoEvents(); floor--; } Thread.Sleep(10000); floor++; Preprocess--; Wait[i] = false; } } }
public void Outofelevator(int j) { if ((j < floor) && (direction == 2 || direction == 0) && (Outdir == false)) { direction = 2; Wait[j] = true; Preprocess++; } else if ((j > floor) && (direction == 1 || direction == 0) && (Outdir == true)) { direction = 1; Wait[j] = true; Preprocess++; } else Ywait[j] = true; if ((floor == 20) || (floor == 0) || (Preprocess ==0)) { if (direction == 2) { direction = 1; } else if (direction == 1) { direction = 2; } } for (int i = 0; i <= 20; i++) { if (Ywait[i] == true) { Wait[i] = true; Preprocess++; } } }
程序截图: