C#控制台模拟电梯工作原理

登录-注册 发布于 2015/06/11 17:43
阅读 814
收藏 0
每天上下楼都是乘坐电梯的,就想电梯的工作原理是什么呢?于是自己写了个控制台程序来模拟一下电梯的工作原理!
采用面向对象的编程思想!将电梯拆解为两部分;
第一部分就是每个楼层的控制器(每个楼层都有叫梯按钮的哈,一个向上一个向下)
第二部分就电梯间了。电梯间里有楼层按钮,你想上那个楼层就可以按哪个按钮了!
技术难点:状态刷新、命令顺序、电梯运行走向

1. [图片] 1.png    

2. [代码][C#]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
 
namespaceElevator
{
    /// <summary>
    /// 楼层类,每个楼层有向上叫梯命令和向下叫梯命令
    /// </summary>
    publicclassFloor
    {
        Elevator elevator;
        /// <summary>
        /// 楼层号
        /// </summary>
        privateint_iFloorNo;
 
        publicintIFloorNo
        {
            get{return_iFloorNo; }
            set{ _iFloorNo = value; }
        }
        /// <summary>
        /// 上行需求
        /// </summary>
        privatebool_boolUp =false;
        /// <summary>
        /// 下行需求
        /// </summary>
        privatebool_boolDown =false;
 
        privatebool_boolStop =false;
 
        publicboolBoolStop
        {
            get{return_boolStop; }
            set{ _boolStop = value; }
        }
 
        #region 构造函数
         
       
        publicFloor(intf,Elevator e)
        {
            _iFloorNo = f;
            elevator = e;
        }
 
        publicFloor(intf)
        {
            _iFloorNo = f;
        }
 
        publicFloor()
        {
        }
        #endregion
 
        /// <summary>
        /// 获取本层是否停靠,是否为命令层
        /// </summary>
        /// <returns>停靠true;过false;</returns>
        publicboolGetStatus()
        {
            return_boolDown || _boolUp;
        }
 
        /// <summary>
        /// 上行返回1;下行返回-1;本层为目的地返回0;
        /// </summary>
        /// <returns></returns>
        publicintGetStatusFlag()
        {
            if(_boolDown)
            {
                return-1;
            }
            elseif(_boolUp)
            {
                return1;
            }
            elseif(_boolStop)
            {
                return0;
            }
            else
            {
                return-999;
            }
 
        }
 
        /// <summary>
        /// 上楼命令
        /// </summary>
        publicvoidCommandUp()
        {
            _boolUp =true;
            elevator.GoToCommandFloor();
        }
 
        /// <summary>
        /// 下楼命令
        /// </summary>
        publicvoidCommandDown()
        {
            _boolDown =true;
            elevator.GoToCommandFloor();
        }
 
        /// <summary>
        /// 楼层到达状态刷新
        /// </summary>
        publicvoidRefresh()
        {
            _boolUp =false;
            _boolDown =false;
            _boolStop =false;
        }
 
 
        /// <summary>
        /// 楼层比较看楼层号
        /// </summary>
        /// <param name="floor"></param>
        /// <returns></returns>
        publicintCompare(Floor floor)
        {
            intresult = 1;
            if(this._iFloorNo > floor._iFloorNo)
            {
                result = 1;
            }
            elseif(this._iFloorNo < floor._iFloorNo)
            {
                result = -1;
            }
            else
            {
                result = 0;
            }
            returnresult;
        }
    }
}

3. [代码][C#]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
 
namespaceElevator
{
    /// <summary>
    /// 电梯类,有楼层选择方法
    /// </summary>
    publicclassElevator
    {
        #region 属性
 
        /// <summary>
        /// 当前楼层
        /// </summary>
        publicFloor _floorCurrent ;
        /// <summary>
        /// 全部楼层
        /// </summary>
        publicList<Floor> _floorAll =newList<Floor>();
 
        #endregion
 
        /// <summary>
        /// 我想在所输入参数楼层停止
        /// </summary>
        /// <param name="floor">想要停止的楼层</param>
        publicvoidCommandStop(Floor floor)
        {
            floor.BoolStop =true;
            GoToTargetFloor();
        }
 
 
       /// <summary>
        /// 电梯向上运行,运行到floor层
       /// </summary>
       /// <param name="floor"></param>
        publicvoidGoUp(Floor floor)
        {
            if(_floorCurrent.Compare(floor) < 0)
            {
            Thread.Sleep(300);
            Console.WriteLine("上行:"+ _floorCurrent.IFloorNo);
            intindex = _floorAll.IndexOf(_floorCurrent);
            _floorCurrent = _floorAll[index + 1];
            GoUp(floor);
            }
            else{
                Thread.Sleep(500);
                Reach(floor);
            }
        }
 
        /// <summary>
        ///  电梯向下运行
        /// </summary>
        publicvoidGoDown(Floor floor)
        {
            if(_floorCurrent.Compare(floor) > 0)
            {
                Thread.Sleep(300);
                Console.WriteLine("下行:"+ _floorCurrent.IFloorNo);
                intindex = _floorAll.IndexOf(_floorCurrent);
                _floorCurrent = _floorAll[index - 1];
                GoDown(floor);
            }
            else{
                Thread.Sleep(500);
                Reach(floor);
            }
        }
 
        /// <summary>
        /// 前往命令层,循环列表是否有命令层
        /// </summary>
        publicvoidGoToCommandFloor()
        {
            foreach(varitemin_floorAll)
            {
                if(item.GetStatus())
                {
                    if(_floorCurrent.Compare(item) < 0)
                    {
                        GoUp(item);
                    }
                    elseif(_floorCurrent.Compare(item) > 0)
                    {
                        GoDown(item);
                    }
                }
            }
        }
 
        /// <summary>
        /// 前往目标楼层
        /// </summary>
        publicvoidGoToTargetFloor() {
 
            foreach(varitemin_floorAll)
            {
                if(item.GetStatusFlag()==0)
                {
                    if(_floorCurrent.Compare(item) < 0)
                    {
                        GoUp(item);
                    }
                    elseif(_floorCurrent.Compare(item) > 0)
                    {
                        GoDown(item);
                    }
                }
            }
        }
        
 
        /// <summary>
        /// 到达楼层命令
        /// </summary>
        publicvoidReach(Floor f)
        {
            Console.WriteLine("电梯门打开,停靠层:"+f.IFloorNo);
            f.Refresh();
        }
 
    }
}

4. [代码][C#]代码     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
 
namespaceElevator
{
    classProgram
    {
        staticvoidMain(string[] args)
        {
             Elevator elevator =newElevator();
            List<Floor> lfloor =newList<Floor>();
            Floor f1 =newFloor(1, elevator);
            Floor f2 =newFloor(2, elevator);
            Floor f3 =newFloor(3, elevator);
            Floor f4 =newFloor(4, elevator);
            Floor f5 =newFloor(5, elevator);
            Floor f6 =newFloor(6, elevator);
            Floor f7 =newFloor(7, elevator);
            Floor f8 =newFloor(8, elevator);
            Floor f9 =newFloor(9, elevator);
            Floor f10 =newFloor(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();
        }
    }
}
加载中
0
CodeZero
CodeZero

电梯算法,其实可以不用那么复杂,建立一个队列,然后就是一个电梯的方向,当队列有数时,电梯总会向一个方向上最近的移动,知道到达本次队列该方向上的最大值,然后改变方向(因为不是上就是下-_-),电梯楼层按钮感觉都没必要有上行下行之分了,反正都不是在那里控制的,只是叫电梯来这一层而已,就是把楼层号加入队列中

这是我个人看法,电梯算法在硬盘的读写中也有用到

返回顶部
顶部