Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- class Fractie:
- def __init__(self,a,b):
- self.numarator = a
- self.numitor = b
- def __repr__(self):
- return '{}/{}'.format(self.numarator,self.numitor)
- def __add__(self, other):
- numarator = self.numarator * other.numitor + other.numarator * self.numitor
- numitor = self.numitor * other.numitor
- rezultat = Fractie(numarator,numitor)
- return rezultat
- def __sub__(self, other):
- numarator = self.numarator * other.numitor - other.numarator * self.numitor
- numitor = self.numitor * other.numitor
- rezultat = Fractie(numarator,numitor)
- return rezultat
- def __mul__(self, other):
- numarator = self.numarator * other.numarator
- numitor = self.numitor * other.numitor
- rezultat = Fractie(numarator,numitor)
- return rezultat
- def __truediv__(self, other):
- numarator = self.numarator * other.numitor
- numitor = self.numitor * other.numarator
- rezultat = Fractie(numarator, numitor)
- return rezultat
- def __neg__(self):
- f = Fractie(-1,1)
- self = self * f
- return self
- def __pow__(self, p):
- numarator = self.numarator ** p
- numitor = self.numitor ** p
- rezultat = Fractie(numarator,numitor)
- return rezultat
- def sqrt(self):
- numarator = int(math.sqrt(self.numarator))
- numitor = int(math.sqrt(self.numitor))
- f = Fractie(numarator,numitor)
- return f
- def __eq__(self, other):
- if self.numarator == other.numarator and self.numitor == other.numitor:
- return True
- else:
- return False
- def __lt__(self, other):
- if self.numarator * other.numitor < self.numitor * other.numarator:
- return True
- else:
- return False
- def __gt__(self, other):
- if self.numarator * other.numitor > self.numitor * other.numarator:
- return True
- else:
- return False
- f1 = Fractie(1,2)
- f2 = Fractie(3,5)
- print(f1 + f2)
- print(f2 - f1)
- print(f1 * f2)
- print(f1/f2)
- f5 = Fractie(3,7)
- print(-f5)
- print(f1**2)
- f3 = Fractie(9,4)
- print(f3.sqrt())
- print(f1 == f2)
- print(f1<f2)
- print(f2>f1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement