Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def gcd(a, b):
- while b:
- a, b = b, a%b
- return a
- def division(a, b):
- res = ''
- check = a
- while (check != a or res == ''):
- while not a // b and a != 0:
- a *= 10
- res += str(a // b)
- if a // 10 == a % b:
- return res[:-1] + '(' + res[-1] + ')'
- a %= b
- return '(' + res + ')'
- class Fraction(object):
- def __init__(self, num, denum=1):
- cur_gcd = gcd(abs(num), abs(denum))
- self.num = abs(num) // cur_gcd
- self.denum = abs(denum) // cur_gcd
- if self.num / self.denum:
- self.sign = num * denum // (abs(num * denum))
- else:
- self.sign = 1
- def __str__(self):
- if self.num // self.denum:
- return f'{self.sign * (self.num // self.denum)} and {self.num % self.denum} / {self.denum}'
- return f'{self.sign * self.num} / {self.denum}'
- def __repr__(self):
- return f'Fraction({self.num} / {self.denum})'
- def __add__(self, sec):
- return Fraction(self.sign * self.num * sec.denum + self.denum * sec.sign * sec.num, self.denum * sec.denum)
- def __sub__(self, sec):
- return Fraction(self.sign * self.num * sec.denum - self.denum * sec.sign * sec.num, self.denum * sec.denum)
- def __mul__(self, sec):
- return Fraction(self.sign * sec.sign * self.num * sec.num, self.denum * sec.denum)
- def __truediv__(self, sec):
- try:
- return self * Fraction(sec.denum, sec.num)
- except ZeroDivisionError:
- return "You can't divide by zero"
- def __pow__(self, power):
- return Fraction(self.num ** power, self.denum ** power)
- def to_float(self):
- if self.num // self.denum:
- res = str(self.num // self.denum)
- a = self.num % self.denum
- b = self.denum
- else:
- res = '0'
- a = self.num
- b = self.denum
- c = division(a, b)
- if c[-3:] == '(0)':
- c = c[:-3]
- return res + '.' + c
- n, m = input().split()
- a = Fraction(int(n), int(m))
- print(a.to_float())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement