基础部分
pythondef squareroot(n):
root = n/2
for i in range(20):
root = (1/2) * (root + (n / root))
return root
if __name__ == '__main__':
print(squareroot(n=4563))
pythonclass Fraction:
"""
构建分数类
"""
# 定义构造方法
"""
self 是一个总是指向对象本身的特殊参数,它必须是第一个形参。
然而,在调用方法时,从来不需要提供相应的实际参数。分数需要分子与分母两部分状态数据。
构造方法中的 self.num 定义了 Fraction 对象有一个叫作 num 的内部数据对象作为其状态的一部分。
同理,self.den 定义了分母。这两个实际参数的值在初始时,赋给了状态,使得新创建的 Fraction 对象能够知道其初始值。
"""
def __init__(self, top, bottom):
self.num = top
self.den = bottom
# 定义输出方法
def show(self):
return str(self.num) + "/" + str(self.den)
def __str__(self):
return str(self.num) + "/" + str(self.den)
# 定义分数加法
def __add__(self, other):
new_num = self.num * other.den + self.den * other.num
new_den = self.den * other.den
return Fraction(new_num, new_den)
myfraction = Fraction(4, 8)
print(myfraction.show())
print(myfraction)
f1 = Fraction(2, 3)
f2 = Fraction(3, 4)
print(f1 + f2)
python# 继承:逻辑门与电路
class LogicGate:
"""
超类LogicGate(逻辑门)的通用特性:标签、输出
子类:2个输入的逻辑门BinaryGate(二元门)、1个输入的逻辑门UnaryGate(一元闸)
"""
def __init__(self, n):
self.label = n
self.output = None
def getLabel(self):
return self.label
def getOutput(self):
# perfromGateLogin 进行自己的逻辑运算
self.output = self.perfromGateLogin()
return self.output
class BinaryGate(LogicGate):
"""
子类BinaryGate
"""
# 常用模式:子类的构造方法需要先使用super函数调用父类的构造方法,再初始化子类独有的数据
# 创建子类的实例时,首先要初始化所有从父类继承的数据项(如逻辑门的标签),接着构造方法添加子类输入
def __init__(self, n):
super().__init__(n)
self.pinA = None
self.pinB = None
# 如果pinA或pinB无任何逻辑门连接,默认用户输入;如有连接,访问该连接并且获取fromgate值
def getPinA(self):
if self.pinA is None:
return int(input(self.getLabel() + ":输入 pinA = "))
else:
# Connector类实例化时,运行tgate.setNextPin(self)并向其中传递类自己的示例
# setNextPin()中的source=Connector类实例==self.pinA,因此可调用getFrom()
return self.pinA.getFrom().getOutput()
def getPinB(self):
if self.pinB is None:
return int(input(self.getLabel() + ":输入 pinB = "))
else:
return self.pinB.getFrom().getOutput()
# 连接器选择
# 如果2个都无连接,默认选择pinA; 如果pinA已经有了连接,则选择pinB; 如果2个都有连接,则无法连接逻辑门
def setNextPin(self, source):
if self.pinA is None:
self.pinA = source
else:
if self.pinB is None:
self.pinB == source
else:
raise RuntimeError("Error: No Empty Pins")
class UnaryGate(LogicGate):
"""
子类UnaryGate
"""
def __init__(self, n):
super().__init__(n)
self.pin = None
def getPin(self):
return int(input(self.getLabel() + ":输入pin = "))
def setNextPin(self, source):
if self.pin is None:
self.pin = source
else:
raise RuntimeError("Error: No Empty Pins")
class AndGate(BinaryGate):
"""
提供perfromGateLogin方法,与门运算实现
"""
def __init__(self, n):
super().__init__(n)
def perfromGateLogin(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 and b == 1:
return 1
else:
return 0
class OrGate(BinaryGate):
"""
提供perfromGateLogin方法,或门运算实现
"""
def __init__(self, n):
super().__init__(n)
def perfromGateLogin(self):
a = self.getPinA()
b = self.getPinB()
if a == 1 or b == 1:
return 1
else:
return 0
class NotGate(UnaryGate):
"""
提供perfromGateLogin方法,非门运算实现
"""
def __init__(self, n):
super().__init__(n)
def perfromGateLogin(self):
a = self.getPin()
if a == 1:
return 0
else:
return 1
class Connector:
"""
"""
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
tgate.setNextPin(self)
def getFrom(self):
return self.fromgate
def getTo(self):
return self.togate
ag = AndGate("与门运算")
print(ag.getOutput())
og = OrGate("或门运算")
print(og.getOutput())
ng = NotGate("非门运算")
print(ng.getOutput())
c1 = Connector(ag, og)
c2 = Connector(ag, ng)
print(c1, c2)
本文作者:赵耀伟
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!