📅  最后修改于: 2023-12-03 15:28:41.371000             🧑  作者: Mango
该题目是GATE-CS-2002的第38道题目,要求实现一个门类(Gate)的结构体与相关的操作(函数)。以下是该题目的详细介绍:
定义一个结构体 Gate 表示门对象,每个门都有三种基本状态,如下:
Gate 结构体应该有下面的元素:
结构体应该支持下面的操作:
以下是结构体 Gate 的定义以及相应操作的实现代码:
class Gate:
def __init__(self, state, num_of_doors, is_locked, door_names):
self.state = state
self.num_of_doors = num_of_doors
self.is_locked = is_locked
self.door_names = door_names
def Display(self):
print(self.door_names[0] + " door is " + self.state)
def Open(self):
if self.state == "BROKEN":
print("Can't open the door. It's BROKEN!")
elif self.is_locked:
print("Can't open the door. It's locked!")
else:
self.state = "OPEN"
self.Display()
def Close(self):
if self.state == "BROKEN":
print("Can't close the door. It's BROKEN!")
else:
self.state = "CLOSED"
self.Display()
def Lock(self):
if self.state == "BROKEN":
print("Can't lock the door. It's BROKEN!")
elif self.is_locked:
print("The door is already locked!")
else:
self.is_locked = True
self.Display()
def Unlock(self):
if self.state == "BROKEN":
print("Can't unlock the door. It's BROKEN!")
elif not self.is_locked:
print("The door is already unlocked!")
else:
self.is_locked = False
self.Display()
def SetState(self, new_state):
if self.state == "BROKEN":
print("Can't set the state. The door is BROKEN!")
else:
self.state = new_state
self.Display()
def SetDoors(self, num_of_doors, door_names):
if self.state == "BROKEN":
print("Can't set door details. The door is BROKEN!")
else:
self.num_of_doors = num_of_doors
self.door_names = door_names
self.Display()
以上代码实现了题目中要求定义的Gate类,并实现了各个操作。可以通过初始化一个Gate对象并直接调用类的函数来测试这些操作。