我需要将gcd放入构造函数中。我让gcd在课外工作,但是当我尝试放入构造函数时,我无法让它工作。我可以使用一些帮助。
这是我的代码:
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
if self.num == 0:
return str(0)
elif self.num > self.den:
if self.den == 1:
return str(self.num)
else:
return str(self.num // self.den)+\
' '+str(self.num%self.den)+\
'/'+str(self.den)
else:
return str(self.num)+"/"+str(self.den)
def show(self):
print(self.num,"/",self.den)
def __add__(self,otherfraction):
newnum = self.num*otherfraction.den + \
self.den*otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __sub__(self,otherfraction):
newnum = self.num*otherfraction.den - \
self.den*otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __mul__(self,otherfraction):
newnum = self.num*otherfraction.num
newden = self.den * otherfraction.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __truediv__(self,otherfraction):
newnum = self.num*otherfraction.den
newden = self.den * otherfraction.num
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __gt__(self,other):
frac1 = self.num*other.den
frac2 = self.den * other.num
if frac1 > frac2:
return self.num//self.den
else:
return other.num//other.den
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum == secondnum
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
def main():
try:
getNum1 = int(input("Enter a numerator: "))
getDen1 = int(input("Enter a denominator: "))
getNum2 = int(input("Enter a numerator: "))
getDen2 = int(input("Enter a denominator: "))
f1 = Fraction(getNum1,getDen2)
f2 = Fraction(getNum2,getDen2)
print("[",f1,"]","[",f2,"]",sep='')
f3 = f1 + f2
print("Adding Fractions:",f3)
f3 = f1 - f2
print("Subtracting Fraction:",f3)
f3 = f1 * f2
print("Multiply Fraction:",f3)
f3 = f1 / f2
print("Dividing Fraction:",f3)
if f1 > f2:
print(f1,"Greater than",f2)
else:
print(f2,"Greater than",f1)
if f1 == f2:
print("Fractions are equal")
else:
print("Fractions are not equal")
except:
input("new Fraction")
main()
这是我试图把它放在构造函数中的地方:
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
self.gcd = n
while self.den%other.den != 0:
oldm = self.den
oldn = other.den
m = oldn
n = oldm%oldn
return n
def __str__(self):
if self.num == 0:
return str(0)
elif self.num > self.den:
if self.den == 1:
return str(self.num)
else:
return str(self.num // self.den)+\
' '+str(self.num%self.den)+\
'/'+str(self.den)
else:
return str(self.num)+"/"+str(self.den)
def show(self):
print(self.num,"/",self.den)
def __add__(self,otherfraction,gcd):
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 __sub__(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 __mul__(self,otherfraction):
newnum = self.num*otherfraction.num
newden = self.den * otherfraction.den
common = self.gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __truediv__(self,otherfraction):
newnum = self.num*otherfraction.den
newden = self.den * otherfraction.num
common = self.gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __gt__(self,other):
frac1 = self.num*other.den
frac2 = self.den * other.num
if frac1 > frac2:
return self.num//self.den
else:
return other.num//other.den
def __eq__(self, other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum == secondnum
def main():
#try:
getNum1 = int(input("Enter a numerator: "))
getDen1 = int(input("Enter a denominator: "))
getNum2 = int(input("Enter a numerator: "))
getDen2 = int(input("Enter a denominator: "))
f1 = Fraction(getNum1,getDen2)
f2 = Fraction(getNum2,getDen2)
print("[",f1,"]","[",f2,"]",sep='')
f3 = f1 + f2
print("Adding Fractions:",f3)
f3 = f1 - f2
print("Subtracting Fraction:",f3)
f3 = f1 * f2
print("Multiply Fraction:",f3)
f3 = f1 / f2
print("Dividing Fraction:",f3)
if f1 > f2:
print(f1,"Greater than",f2)
else:
print(f2,"Greater than",f1)
if f1 == f2:
print("Fractions are equal")
else:
print("Fractions are not equal")
#except:
#input("new Fraction")
main()
答案 0 :(得分:1)
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
这是您的gcd
功能,有两个参数m
和n
,并将gcd作为n
返回。
因此,为了在构造函数中内联此函数,您只需要正确提供n
和m
,然后保存最终的gcd结果:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
# gcd arguments
m, n = self.num, self.den
# original gcd code, using `n` and `m`
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
# instead of returning the gcd `n`, save it in the instance:
self.gcd = n
请注意,您也可以将函数保留为函数,只需在构造函数中调用它:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
self.gcd = gcd(self.num, self.den)
这通常是一个更清洁的解决方案,因为它保持(不相关的)gcd逻辑不属于您的类型并且只是利用它。
由于您希望从类型中访问gcd
函数(因此可以在各种操作中使用它),因此应将其保留为可调用函数。您基本上有两种选择:您可以将其保留为原始形式,gcd
函数在类外部定义,或者您可以将其作为静态方法在类中移动。对于这样的辅助方法,前一个选项通常是首选,因此您实际上不需要更改原始代码中的任何内容。但如果你想把它移到里面,你可以这样做:
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
@staticmethod
def gcd (m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
# and the other methods …
然后,您可以像self.gcd(m, n)
实施中那样将方法称为__add__
。请注意,您要删除那里的gcd
方法参数。
另一种解决方案是取消运算符实现中的分数,但实际上是在创建新分数时。例如,这会自动将Fraction(30, 100)
简化为Fraction(3, 10)
。你可以通过仅在构造函数中计算gcd(如前所述)并减少那里的分数部分来实现这一点:
def __init__ (self, top, bottom):
# run gcd first
m, n = top, bottom
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
# and now store the shortened values
self.num = top // n
self.den = bottom // n
这会将操作符方法简化为:
def __add__ (self, other):
return Fraction(self.num * other.den + self.den * other.num, self.den * other.den)