Advertisement
trds

6ianuarie2021

Jan 6th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. import math
  2. class Fractie:
  3.     def __init__(self,a,b):
  4.         self.numarator = a
  5.         self.numitor = b
  6.     def __repr__(self):
  7.         return '{}/{}'.format(self.numarator,self.numitor)
  8.     def __add__(self, other):
  9.         numarator = self.numarator * other.numitor + other.numarator * self.numitor
  10.         numitor = self.numitor * other.numitor
  11.         rezultat = Fractie(numarator,numitor)
  12.         return rezultat
  13.     def __sub__(self, other):
  14.         numarator = self.numarator * other.numitor - other.numarator * self.numitor
  15.         numitor = self.numitor * other.numitor
  16.         rezultat = Fractie(numarator,numitor)
  17.         return rezultat
  18.     def __mul__(self, other):
  19.         numarator = self.numarator * other.numarator
  20.         numitor = self.numitor * other.numitor
  21.         rezultat = Fractie(numarator,numitor)
  22.         return rezultat
  23.     def __truediv__(self, other):
  24.         numarator = self.numarator * other.numitor
  25.         numitor = self.numitor * other.numarator
  26.         rezultat = Fractie(numarator, numitor)
  27.         return rezultat
  28.     def __neg__(self):
  29.         f = Fractie(-1,1)
  30.         self = self * f
  31.         return self
  32.     def __pow__(self, p):
  33.         numarator = self.numarator ** p
  34.         numitor = self.numitor ** p
  35.         rezultat = Fractie(numarator,numitor)
  36.         return rezultat
  37.     def sqrt(self):
  38.         numarator = int(math.sqrt(self.numarator))
  39.         numitor = int(math.sqrt(self.numitor))
  40.         f = Fractie(numarator,numitor)
  41.         return f
  42.     def __eq__(self, other):
  43.         if self.numarator == other.numarator and self.numitor == other.numitor:
  44.             return True
  45.         else:
  46.             return False
  47.     def __lt__(self, other):
  48.         if self.numarator * other.numitor < self.numitor * other.numarator:
  49.             return True
  50.         else:
  51.             return False
  52.     def __gt__(self, other):
  53.         if self.numarator * other.numitor > self.numitor * other.numarator:
  54.             return True
  55.         else:
  56.             return False
  57. f1 = Fractie(1,2)
  58. f2 = Fractie(3,5)
  59. print(f1 + f2)
  60. print(f2 - f1)
  61. print(f1 * f2)
  62. print(f1/f2)
  63. f5 = Fractie(3,7)
  64. print(-f5)
  65. print(f1**2)
  66. f3 = Fractie(9,4)
  67. print(f3.sqrt())
  68. print(f1 == f2)
  69. print(f1<f2)
  70. print(f2>f1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement