C#控制台模拟电梯工作原理
程序员文章站
2023-12-01 16:05:28
每天上下楼都是乘坐电梯的,就想电梯的工作原理是什么呢?于是自己写了个控制台程序来模拟一下电梯的工作原理!
采用面向对象的编程思想!将电梯拆解为两部分;
第一部分就是每个...
每天上下楼都是乘坐电梯的,就想电梯的工作原理是什么呢?于是自己写了个控制台程序来模拟一下电梯的工作原理!
采用面向对象的编程思想!将电梯拆解为两部分;
第一部分就是每个楼层的控制器(每个楼层都有叫梯按钮的哈,一个向上一个向下)
第二部分就电梯间了。电梯间里有楼层按钮,你想上那个楼层就可以按哪个按钮了!
技术难点:状态刷新、命令顺序、电梯运行
核心代码一:
using system; using system.collections.generic; using system.linq; using system.text; namespace elevator { /// <summary> /// 楼层类,每个楼层有向上叫梯命令和向下叫梯命令 /// </summary> public class floor { elevator elevator; /// <summary> /// 楼层号 /// </summary> private int _ifloorno; public int ifloorno { get { return _ifloorno; } set { _ifloorno = value; } } /// <summary> /// 上行需求 /// </summary> private bool _boolup = false; /// <summary> /// 下行需求 /// </summary> private bool _booldown = false; private bool _boolstop = false; public bool boolstop { get { return _boolstop; } set { _boolstop = value; } } #region 构造函数 public floor(int f,elevator e) { _ifloorno = f; elevator = e; } public floor(int f) { _ifloorno = f; } public floor() { } #endregion /// <summary> /// 获取本层是否停靠,是否为命令层 /// </summary> /// <returns>停靠true;过false;</returns> public bool getstatus() { return _booldown || _boolup; } /// <summary> /// 上行返回1;下行返回-1;本层为目的地返回0; /// </summary> /// <returns></returns> public int getstatusflag() { if (_booldown) { return -1; } else if(_boolup) { return 1; } else if(_boolstop) { return 0; } else { return -999; } } /// <summary> /// 上楼命令 /// </summary> public void commandup() { _boolup = true; elevator.gotocommandfloor(); } /// <summary> /// 下楼命令 /// </summary> public void commanddown() { _booldown = true; elevator.gotocommandfloor(); } /// <summary> /// 楼层到达状态刷新 /// </summary> public void refresh() { _boolup = false; _booldown = false; _boolstop = false; } /// <summary> /// 楼层比较看楼层号 /// </summary> /// <param name="floor"></param> /// <returns></returns> public int compare(floor floor) { int result = 1; if (this._ifloorno > floor._ifloorno) { result = 1; } else if (this._ifloorno < floor._ifloorno) { result = -1; } else { result = 0; } return result; } } }
核心代码二:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading; namespace elevator { /// <summary> /// 电梯类,有楼层选择方法 /// </summary> public class elevator { #region 属性 /// <summary> /// 当前楼层 /// </summary> public floor _floorcurrent ; /// <summary> /// 全部楼层 /// </summary> public list<floor> _floorall = new list<floor>(); #endregion /// <summary> /// 我想在所输入参数楼层停止 /// </summary> /// <param name="floor">想要停止的楼层</param> public void commandstop(floor floor) { floor.boolstop = true; gototargetfloor(); } /// <summary> /// 电梯向上运行,运行到floor层 /// </summary> /// <param name="floor"></param> public void goup(floor floor) { if (_floorcurrent.compare(floor) < 0) { thread.sleep(300); console.writeline("上行:" + _floorcurrent.ifloorno); int index = _floorall.indexof(_floorcurrent); _floorcurrent = _floorall[index + 1]; goup(floor); } else { thread.sleep(500); reach(floor); } } /// <summary> /// 电梯向下运行 /// </summary> public void godown(floor floor) { if (_floorcurrent.compare(floor) > 0) { thread.sleep(300); console.writeline("下行:" + _floorcurrent.ifloorno); int index = _floorall.indexof(_floorcurrent); _floorcurrent = _floorall[index - 1]; godown(floor); } else { thread.sleep(500); reach(floor); } } /// <summary> /// 前往命令层,循环列表是否有命令层 /// </summary> public void gotocommandfloor() { foreach (var item in _floorall) { if (item.getstatus()) { if (_floorcurrent.compare(item) < 0) { goup(item); } else if (_floorcurrent.compare(item) > 0) { godown(item); } } } } /// <summary> /// 前往目标楼层 /// </summary> public void gototargetfloor() { foreach (var item in _floorall) { if (item.getstatusflag()==0) { if (_floorcurrent.compare(item) < 0) { goup(item); } else if (_floorcurrent.compare(item) > 0) { godown(item); } } } } /// <summary> /// 到达楼层命令 /// </summary> public void reach(floor f) { console.writeline("电梯门打开,停靠层:"+f.ifloorno); f.refresh(); } } }
核心代码三:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading; namespace elevator { class program { static void main(string[] args) { elevator elevator = new elevator(); list<floor> lfloor = new list<floor>(); floor f1 = new floor(1, elevator); floor f2 = new floor(2, elevator); floor f3 = new floor(3, elevator); floor f4 = new floor(4, elevator); floor f5 = new floor(5, elevator); floor f6 = new floor(6, elevator); floor f7 = new floor(7, elevator); floor f8 = new floor(8, elevator); floor f9 = new floor(9, elevator); floor f10 = new floor(10, elevator); lfloor.add(f1); lfloor.add(f2); lfloor.add(f3); lfloor.add(f4); lfloor.add(f5); lfloor.add(f6); lfloor.add(f7); lfloor.add(f8); lfloor.add(f9); lfloor.add(f10); elevator._floorall = lfloor; //设置当前楼层 elevator._floorcurrent = f1; //2楼叫梯 想下楼 f2.commanddown(); //目标楼层为1楼 elevator.commandstop(f1); //4楼叫梯 f4.commandup(); //目标楼层为8楼 elevator.commandstop(f8); console.readline(); } } }
以上所述就是本文的全部内容了,希望对大家熟练掌握c#能够有所帮助。
下一篇: 建设营销型网站有哪些攻略