Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- # ДЗ НА СТРОКАХ 24, 29, 32, 37 !!!
- from functools import total_ordering
- @total_ordering
- class Fraction:
- def __init__(self, numerator, denominator): #numerator denominator
- numerator/denominator
- self.a = numerator
- self.b = denominator
- def reduce(self): #сокращение
- while b!=0:
- a,b = b,a%b
- self.numerator /= a
- self.denominator /= a
- return self
- def __add__(self, other): #+
- if type(other) == int:
- other = Fraction(other,1)
- return Fraction(self.numerator * other.denominator + self.denominator * other.numerator, self.denominator * other.denominator)
- def __sub__(self, other): #-
- if type(other) == int:
- other = Fraction(other,1)
- return Fraction(self.numerator * other.denominator - self.denominator * other.numerator, self.denominator * other.denominator)
- def __neg__(self): #унарный -
- return Fraction(-self.numerator, -self.denominator)
- def __mul__(self, other): #*
- self._reduce()
- other._reduce()
- return self.numerator * other.numerator + '/' + (self.denominator * other.denominator)
- def __int__(self): #приведение к int
- if type(self) == int or self==0:
- self=self.numerator
- return Fraction(self)
- if self.numerator >= self.denominator:
- self.numerator=self.numerator%self.denominator
- return Fraction(self.numerator//self.denominator, self.numerator + '/' + self.denominator)
- def __float__(self): #дробь
- self.numerator/ self.denominator
- return(self)
- def __radd__(self,other): #смена мест
- return self+other
- def __str__(self): #вывод
- self._reduce()
- return str(self.numerator)+ '/' + str(self.denominator)
- def __eq__(self,other): #==
- self._reduce()
- other._reduce()
- return self.numerator == other.numerator and self.denominator == other.denominator
- def __lt__(self, other): #<
- self._reduce()
- other._reduce()
- return self.numerator * other.denominator - self.denominator * other.numerator < 0
Add Comment
Please, Sign In to add comment