实现

Github地址

https://github.com/ljshLLW/homework/tree/master/结对h2

电梯类

class Elevator:
def __init__(self, eNum, type, pMaxNum, weight):
"""
:param eNum: 电梯编号
:param type: 电梯类型 0 全部楼层 1 单层 2 双层
:param pMaxNum: 最大载客量
:param weight: 最大载重量
pNum      : 现载客量
direction : 电梯运行方向  1 上 0 下
floor     : 电梯所在楼层
list      : 记录电梯所有需要停下的楼层
maxFloor  : 记录运行方向最远的楼层
status    : 电梯状态 0 等待 1 运行 2 操作
"""
self.eNum = eNum
self.type = type
self.pMaxNum = pMaxNum
self.weight = weight天
self.pNum = 0
self.direction = 1
self.floor = 0
self.list = []
self.maxFloor = 0
self.status = 0
self.signal = 0

# 电梯日志函数
def log(self):
if self.direction == 0:
print("电梯 " + str(self.eNum) + " 所在楼层 " + str(self.floor) +
" 电梯运行方向 " + "↓")
else:
print("电梯 " + str(self.eNum) + " 所在楼层 " + str(self.floor) +
" 电梯运行方向 " + "↑")
·
·
·


权重算法

    def weighT(self, num, direction):
"""
算法尚不完备
:param num: 所在楼层
:param direction: 要去的方向
:return: 权值 权值小的电梯接任务
"""
if (num % 2 == 0 and (self.type == 0 or self.type == 2)) or(
num % 2 == 1 and (self.type == 0 or self.type == 1)):
if self.pNum < self.pMaxNum:
if self.direction == direction:
if direction == 1:
if self.floor > num:
return 2 * self.maxFloor - num - self.floor
else:
return num - self.floor
else:
if self.floor < num:
return self.floor + num - 2 * self.maxFloor
else:
return self.floor - num
else:
if direction == 1:
if self.floor < num:
return self.floor + num - 2 * self.maxFloor +10
else:
if self.maxFloor < num:
return self.floor + num - 2 * self.maxFloor +10
else:
return self.floor - num
else:
if self.floor > num:
return 2 * self.maxFloor - num - self.floor +10
else:
if self.maxFloor > num:
return 2 * self.maxFloor - num - self.floor +10
else:
return num - self.floor

return 999


多线程

from elevator import *

e1 = Elevator(1, 0, 10, 800)
e2 = Elevator(2, 1, 10, 800)
e3 = Elevator(3, 2, 10, 800)
e4 = Elevator(4, 0, 20, 2000)

elevators = [e1,e2,e3,e4]
# 创建线程
for e in elevators:
t.start()


写在最后

posted @ 2019-04-28 00:03  林静生寒  阅读(417)  评论(2编辑  收藏  举报