- 深刻理解python面向对象的意义,创建类、并使用类继承。
1、实现分数的相关运算
class Fraction:
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def show(self):
print(self.num, "/", self.den)
def __str__(self):
common = self.gcd(self.num, self.den)
return str(self.num//common) + "/" + str(self.den//common)
def gcd(self, m, n):
'''求两个数的最大公约数,用于分数的化简'''
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
def __add__(self, otherFraction):
newnum = self.num * otherFraction.den + self.den * otherFraction.num
newden = self.den * otherFraction.den
common = self.gcd(newnum, newden)
return Fraction(newnum//common,newden//common)
def __eq__(self,other):
firstnum = self.num * other.den
secondnum = self.den * other.num
return firstnum == secondnum
def __sub__(self, other):
newnum = self.num * other.den -self.den * other.num
newden = self.den * other.den
common = self.gcd(newnum, newden)
return Fraction(newnum//common , newden//common)
def __mul__(self, other):
newnum = self.num * other.num
newden = self.den * other.den
common = self.gcd(newnum, newden)
return Fraction(newnum//common , newden//common)
def __truediv__(self, other):
newnum = self.num * other.den
newden = self.den * other.num
common = self.gcd(newnum, newden)
return Fraction(newnum//common , newden//common)
def __lt__(self, other):
'''小于'''
newnum = self.num * other.den -self.den * other.num
return newnum < 0
def __gt__(self, other):
'''大于'''
newnum = self.num * other.den -self.den * other.num
return newnum > 0
f1 = Fraction(1,4)
f2 = Fraction(1,2)
f3 = Fraction(3,5)
f4 = Fraction(4,8)
print(f1 + f2)
3/4
print(f1 - f2)
-1/4
print(f1 + f3)
17/20
f1 == f3
False
f2 == f4
True
print(f2 - f4)
0/1
f1 < f2
True
f1 > f2
False
print(f1 * f2, f2 * f3, f2 * f4 )
1/8 3/10 1/4
print(f1 / f2, f2 / f3, f2 / f4)
1/2 5/6 1/1
2、逻辑电路仿真
class LogicGate:
def __init__(self, name):
self.label = name
self.output = None
def getLabel(self):
return self.label
def getOutput(self):
self.output = self.performGateLogic()
return self.output
- 上述代码并没有实现performGateLogic()函数,是因为想让后面具体的逻辑电路实现。
class BinaryGate(LogicGate):
def __init__(self,name):
LogicGate.__init__(self,name)
self.pinA = None
self.pinB = None
def getPinA(self):
return int(input("Enter Pin A input for gate " + self.getLabel() + "-->"))
def getPinB(self):
return int(input("Enter Pin B input for gate " + self.getLabel() + "-->"))
def setNextPin(self,source):
if self.pinA == None:
self.pinA = source
else:
if self.pinB == None:
self.pinB = source
else:
print("Cannot Connect: NO EMPTY PINS on this gate")
class UnaryGate(LogicGate):
def __init__(self,name):
LogicGate.__init__(self,name)
self.pin = None
def getPin(self):
if self.pin == None:
return int(input("Enter Pin input for gate "+self.getLabel()+"-->"))
else:
return self.pin.getFrom().getOutput()
def setNextPin(self, source):
if self.pin == None:
self.pin = source
else:
raise RuntimeError("Error: No Empty Pins")
class AndGate(BinaryGate):
def __init__(self, name):
BinaryGate.__init__(self, name)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a==1 and b==1:
return 1
else:
return 0
class OrGate(BinaryGate):
def __init__(self, name):
BinaryGate.__init__(self, name)
def performGateLogic(self):
a = self.getPinA()
b = self.getPinB()
if a==0 and b==0:
return 0
else:
return 1
class NotGate(UnaryGate):
def __init__(self, name):
UnaryGate.__init__(self, name)
def performGateLogic(self):
if self.getPin():
return 0
else:
return 1
'''测试'''
g1 = AndGate("G1")
g1.getOutput()
Enter Pin A input for gate G1-->1
Enter Pin B input for gate G1-->1
1
g2 = OrGate("G2")
g2.getOutput()
Enter Pin A input for gate G2-->1
Enter Pin B input for gate G2-->0
1
g3 = NotGate("G3")
g3.getOutput()
Enter Pin input for gate G3-->1
0
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
def getPinA(self):
if self.pinA ==None:
return input("Enter Pin A input for gate " + self.getName()+"-->")
else:
return self.pinA.getFrom().getOutput()
g1 = AndGate("G1")
g2 = AndGate("G2")
g3 = OrGate("G3")
g4 = NotGate("G4")
c1 = Connector(g1,g3)
c2 = Connector(g2,g3)
c3 = Connector(g3,g4)
g4.getOutput()
Enter Pin A input for gate G3-->1
Enter Pin B input for gate G3-->0
0