主要代码:
电梯类:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; using System.IO; using System.Threading; namespace WindowsFormsApplication2 { class dianti { public int number; public int wight; public int sx;//电梯上行下行或停止标志 0、1、2标识 public int maxnumber; public int maxwight; public int dqlc;//电梯当前层 // private int i; // private int j; public dianti(int a, int b,int c,int d) { this.maxnumber = a; this.maxwight = b; this.dqlc = c; this.sx = d; } public void diaodu(ref ArrayList da,ref dianti dta) { if (da==null) { dta.sx = 2; return; } i = 21; if (dta.sx == 1) { foreach (chengke o in da) { if (o.sxt == dta.sx) { dta.sx = 1; break; } dta.sx = 0; } } else if (dta.sx == 0) { foreach (chengke o in da) { if (o.sxt == dta.sx) { dta.sx = 0; break; } dta.sx = 1; } } else { dta.sx = 1; foreach (chengke o in da) { if (o.sxt == dta.sx) { dta.sx = 1; break; } dta.sx = 0; } } if (dta.sx == 1)//电梯A向上走 foreach (chengke o in da)//寻找离电梯最近的请求点 { if (o.sxt == dta.sx) { if (o.f == 0 && o.newfloor >= dta.dqlc)//未上电梯 if (i > o.newfloor - dta.dqlc) { i = o.newfloor - dta.dqlc; j = o.newfloor; } if (o.f == 0 && o.newfloor < dta.dqlc) if (i > dta.dqlc - o.newfloor) { i = dta.dqlc - o.newfloor; j = o.newfloor; } if (o.f == 1 && o.goalfloor >= dta.dqlc)//已上电梯 if (i > o.goalfloor - dta.dqlc) { i = o.goalfloor - dta.dqlc; j = o.goalfloor; } } } if (dta.sx == 0) foreach (chengke o in da) { if (o.sxt == dta.sx) { if (o.f == 0 && o.newfloor <= dta.dqlc) if (i > dta.dqlc - o.newfloor) { i = dta.dqlc - o.newfloor; j = o.newfloor; } if (o.f == 0 && o.newfloor > dta.dqlc) if (i > o.newfloor - dta.dqlc) { i = o.newfloor - dta.dqlc; j = o.newfloor; } if (o.f == 1 && o.goalfloor <= dta.dqlc) if (i > dta.dqlc - o.goalfloor) { i = dta.dqlc - o.goalfloor; j = o.goalfloor; } } } } public void dtyuxinga( ref ArrayList da, ref dianti dta, ref Label label11, ref Button button1, ref Button button2) { while (true) { if (da!=null) { if (dta.sx == 1) { while (dta.dqlc < j) { dta.dqlc++; label11.Text = dta.dqlc.ToString(); Thread.Sleep(2000); } while (dta.dqlc > j) { dta.dqlc--; label11.Text = dta.dqlc.ToString(); Thread.Sleep(2000); } if (dta.dqlc == j) { dta.Opendoora(button1,button2); dta.jiancea(da,dta); Thread.Sleep(5000); dta.closedoora(button1, button2); } } if (dta.sx == 0) { while (dta.dqlc > j) { dta.dqlc--; label11.Text = dta.dqlc.ToString(); Thread.Sleep(2000); } while (dta.dqlc < j) { dta.dqlc++; label11.Text = dta.dqlc.ToString(); Thread.Sleep(2000); } if (dta.dqlc == j) { dta.Opendoora(button1, button2); dta.jiancea(da,dta); Thread.Sleep(5000); dta.closedoora(button1, button2); } } } } } public void Opendoora(Button button1,Button button2) { while (button1.Width > 5 && button2.Width > 5) { button1.Width--; button2.Width--; button2.Location = new Point(button2.Location.X + 1, button2.Location.Y); Thread.Sleep(100); } } public void closedoora(Button button1, Button button2) { while (button1.Width < 48 && button2.Width < 48) { button1.Width++; button2.Width++; button2.Location = new Point(button2.Location.X - 1, button2.Location.Y); Thread.Sleep(100); } } public void jiancea(ArrayList da,dianti dta) { for (int g = 0; g <da.Count; g++) { chengke ck = (chengke)da[g]; if (ck.goalfloor == dta.dqlc && ck.f == 1) { dta.wight = dta.wight - ck.tzhong; dta.number = dta.number - 1; da.Remove(da[g]); } } foreach (chengke o in da) { if (o.newfloor == dta.dqlc && o.f == 0) { dta.wight = dta.wight + o.tzhong; dta.number = dta.number + 1; if (dta.number <= dta.maxnumber && dta.wight <= dta.maxwight) o.f = 1; else { dta.wight = dta.wight - o.tzhong; dta.number = dta.number - 1; } } } } } }
乘客类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication2 { class chengke { public int tzhong; public int newfloor; public int goalfloor; public int f;//记录是否进入电梯0没进1进入 public int sxt;//记录上行下行 public chengke(int f) { Random ranNum = new Random(); this.tzhong = ranNum.Next(45, 120); this.f = f; } } }
软件工程-电梯调度算法分析与实现3,布布扣,bubuko.com
原文:http://www.cnblogs.com/lpjdbk/p/3615160.html