Last active
June 30, 2016 20:26
-
-
Save AbraaoAlves/9f40c611f087cf9eada850c6ac70dc9b to your computer and use it in GitHub Desktop.
Python Racional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class Racional: | |
def __init__(self, dividendo, divisor): | |
self.divisor = divisor | |
self.dividendo = dividendo | |
def __str__(self): | |
return str(self.dividendo) + '/' + str(self.divisor) | |
def __mul__(self, outro): | |
divisor = self.divisor * outro.divisor | |
dividendo = self.dividendo * outro.dividendo | |
return new Racional(dividendo, divisor) | |
def __add__(self, outro): | |
commonMultiple = Racional.leastCommonMultiple(self.divisor. outro.divisor) | |
dividendo1 = self.dividendo * (commonMultiple/self.divisor) | |
dividendo2 = outro.dividendo * (commonMultiple/outro.divisor) | |
return new Racional(dividendo1+dividendo2, commonMultiple) | |
def highestCommonFactor(a, b): | |
return a if b == 0 else Racional.highestCommonFactor(b, (a%b)) | |
def leastCommonMultiple(a,b): | |
return a * b / Racional.highestCommonFactor(a,b) | |
a = Racional(1,2) | |
b = Racional(3,4) | |
c = a*b | |
d = a+b | |
print("Seja o racional a = ",a) | |
print("Seja o racional b = ",b) | |
print("Seja o racional c (a * b) = ",c) | |
print("Seja o racional c (a + b) = ",d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment