Есть класс дробей, необходимо добавить функцию сложения, вычитания, умножения? Есть несколько вариантов, но они не работают. Может кто-то подскажетБ что еще можно сделать?
Сам класс:
class Fraction:
def __init__(self, num, denom):
self.num = num
self.denom = denom
def __str__(self):
return str(self.num)+'/'+str(self.denom)
def __add__(self, other):
newnum = self.num * other.denom + other.num * self.denom
newdenom = self.denom * other.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
def __sub__(self, other):
newnum = self.num * other.denom - other.num * self.denom
newdenom = self.denom * other.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
def __mul__(self, other):
newnum = self.num * other.num
newdenom = self.denom * other.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
if __name__ == "__main__":
f1 = Fraction(11,6)
f2 = Fraction(2,9)
print(f1+f2)
print(f1 - f2)
print(f1 * f2)
Варианты:
1.
def __add__(self, other):
if isinstance(other, (int, Fraction)):
return Fraction(self.num * other.denom +
other.num * self.denom,
self.denom * other.denom)
elif isinstance(other, float):
return float(self) + other
elif isinstance(other, complex):
return complex(self) + other
return NotImplemented
2.
def __add__(self,other): #попыткас целым числом
# является ли первый параметр сложения дробью
if not isinstance(self, Fraction):
return NotImplemented
# является ли второй параметр сложения дробью
if isinstance(other, Fraction):
self.num = other.num
self.denom = other.denom
# является ли второй параметр сложения целым числом
elif isinstance(other, int):
self.num = other
self.denom = 1
# является ли второй параметр сложения чем-то другим
else:
return NotImplemented
newnum = self.num * other.denom + other.num * self.denom
newdenom = self.denom * self.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
Сам класс:
class Fraction:
def __init__(self, num, denom):
self.num = num
self.denom = denom
def __str__(self):
return str(self.num)+'/'+str(self.denom)
def __add__(self, other):
newnum = self.num * other.denom + other.num * self.denom
newdenom = self.denom * other.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
def __sub__(self, other):
newnum = self.num * other.denom - other.num * self.denom
newdenom = self.denom * other.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
def __mul__(self, other):
newnum = self.num * other.num
newdenom = self.denom * other.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)
if __name__ == "__main__":
f1 = Fraction(11,6)
f2 = Fraction(2,9)
print(f1+f2)
print(f1 - f2)
print(f1 * f2)
Варианты:
1.
def __add__(self, other):
if isinstance(other, (int, Fraction)):
return Fraction(self.num * other.denom +
other.num * self.denom,
self.denom * other.denom)
elif isinstance(other, float):
return float(self) + other
elif isinstance(other, complex):
return complex(self) + other
return NotImplemented
2.
def __add__(self,other): #попыткас целым числом
# является ли первый параметр сложения дробью
if not isinstance(self, Fraction):
return NotImplemented
# является ли второй параметр сложения дробью
if isinstance(other, Fraction):
self.num = other.num
self.denom = other.denom
# является ли второй параметр сложения целым числом
elif isinstance(other, int):
self.num = other
self.denom = 1
# является ли второй параметр сложения чем-то другим
else:
return NotImplemented
newnum = self.num * other.denom + other.num * self.denom
newdenom = self.denom * self.denom
common = gcd(newnum, newdenom)
return Fraction(newnum//common, newdenom//common)