SHARE
TWEET

Untitled

a guest Mar 21st, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math as mh
  2. class Real:
  3.     def __init__(self, num, den):
  4.         self.num = num
  5.         self.den = den
  6.     def output(self):
  7.         print (str(self.num) + "/" + str(self.den))
  8.     def __add__(self, other):
  9.         num = self.num * other.den + self.den * other.num
  10.         den = self.den * other.den
  11.         return Real(num, den)
  12.     def __sub__(self, other):
  13.         num = self.num * other.den - self.den * other.num
  14.         den = self.den * other.den
  15.         return Real(num, den)
  16.     def __mul__(self, other):
  17.         num = self.num * other.num
  18.         den = self.den * other.den
  19.         return Real(num, den)
  20.     def __div__(self, other):
  21.         num = self.num * other.den
  22.         den = self.den * other.num
  23.         return Real(num, den)
  24.     def decimal(self):
  25.         return self.num/self.den
  26.     def simply(self):
  27.         c = mh.gcd(self.num, self.den)
  28.         return Real(self.num / c, self.den / c)
  29.    
  30. x = Real(2, 3)
  31. y = Real(3, 4)
  32. k = x * y
  33. k.simply().output()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top