# -*- coding: utf-8 -*- """ Created on Mon Jan 26 13:56:26 2015 @author: Sam """ def gcd(m,n): while m%n != 0: oldm = m oldn = n m = oldn n = oldm%oldn return n def reduce(fraction): GcD = gcd(fraction.num, fraction.den) if GcD == 1 or GcD == -1: if fraction.sign == 1: return Fraction(-fraction.num,fraction.den) else: return Fraction(fraction.num,fraction.den) else: if fraction.sign == 1: return Fraction(-fraction.num/GcD,fraction.den/GcD) else: return Fraction(fraction.num/GcD,fraction.den/GcD) def posneg(fraction): if fraction.sign == 0 or fraction.sign == 2: if fraction.num < 0 or fraction.den < 0: if fraction.den < 0 and fraction.num < 0: fraction.num = -fraction.num fraction.den = -fraction.den fraction.sign = 2 return fraction # The returned fraction is made of positive integers # and has no .sign else: if fraction.den < 0: fraction.num = fraction.num fraction.den = -fraction.den fraction.sign = 1 return fraction # The returned fraction is made of positive integers # and has a sign if fraction.num < 0: fraction.num = -fraction.num fraction.den = fraction.den fraction.sign = 1 return fraction # The returned fraction is made of positive integers # and has a sign else: fraction.sign = 2 return fraction else: if fraction.num < 0 or fraction.den < 0: if fraction.den < 0 and fraction.num < 0: fraction.num = -fraction.num fraction.den = -fraction.den fraction.sign = 1 return fraction # The returned fraction is made of positive integers # and has no .sign else: if fraction.den < 0: fraction.num = fraction.num fraction.den = -fraction.den fraction.sign = 2 return fraction # The returned fraction is made of positive integers # and has a sign if fraction.num < 0: fraction.num = -fraction.num fraction.den = fraction.den fraction.sign = 2 return fraction # The returned fraction is made of positive integers # and has a sign else: fraction.sign = 1 return fraction class Fraction: def __init__(self,top,bottom): self.num = top self.den = bottom self.sign = 0 #0 is none 1 is on 2 is off #Just to start out with something # Sort out the sign on the fraction and simplify self = posneg(self) def __str__(self): if self.sign == 1: return "-" + str(self.num)+"/"+ str(self.den) else: return str(self.num)+"/"+ str(self.den) def __add__(self,otherfraction): newfrac1 = Fraction(1,1) newfrac2 = Fraction (1,1) if self.sign == 1: newfrac1.sign = 1 if self.sign == 2: newfrac1.sign = 2 if otherfraction.sign == 2: newfrac2.sign = 2 if otherfraction.sign == 1: newfrac2.sign = 1 newfrac1.den = self.den * otherfraction.den newfrac1.num = self.num * otherfraction.den newfrac2.den = otherfraction.den * self.den newfrac2.num = otherfraction.num * self.den # Temporarily declare a new fraction newfrac3 = Fraction(1,1) newfrac3.den = newfrac1.den newfrac3.sign = 0 if newfrac1.sign == 2 and newfrac2.sign == 1: newfrac3.num = newfrac1.num - newfrac2.num newfrac3 = posneg(reduce(newfrac3)) return newfrac3 if newfrac1.sign == 1 and newfrac2.sign == 2: newfrac3.num = newfrac2.num - newfrac1.num newfrac3 = posneg(reduce(newfrac3)) return newfrac3 if newfrac1.sign == 1 and newfrac2.sign == 1: newfrac3.num = newfrac1.num + newfrac2.num newfrac3.sign = 1 newfrac3 = reduce(newfrac3) newfrac3 = posneg(newfrac3) return newfrac3 if newfrac1.sign == 2 and newfrac2.sign == 2: newfrac3.num = newfrac2.num + newfrac1.num newfrac3.sign = 2 newfrac3 = posneg(reduce(newfrac3)) return newfrac3 def __mul__ (self,otherfraction): newfraction = Fraction(1,1) newfraction.num = otherfraction.num * self.num newfraction.den = otherfraction.den * self.den if self.sign == 1 and otherfraction.sign == 1: newfraction.sign = 2 if self.sign == 2 and otherfraction.sign == 1: newfraction.sign = 1 if self.sign == 1 and otherfraction.sign == 2: newfraction.sign = 1 if self.sign == 2 and otherfraction.sign == 2: newfraction.sign = 2 return newfraction def __sub__(self,otherfraction): newfraction = Fraction (1,1) negativeone = Fraction(1,1) negativeone.sign = 1 newfraction = self + (negativeone * otherfraction) return newfraction def __div__ (self,otherfraction): newfraction = Fraction (1,1) #Initializing New Fraciton inverseotherfraction = Fraction(otherfraction.num,otherfraction.den) inverseotherfraction.sign = otherfraction.sign newfraction = self*newfraction return newfraction print('Fraction') x1 = Fraction(1,3) #1/2 x2 = Fraction(1,-4) #-1/2 x3 = Fraction(-1,-5) #1/2 x4 = Fraction(-1,6) #-1/2 print(x1) print(x2) print(x3) print(x4) s1 = x1+x2 # 0 s2 = x1+x3 # 1 s3 = x1+x4 # 0 s4 = s1+x3 # 1/2 print('Sum') print(s1) print(s2) print(s3) print(s4) m1= x1*x2 m2= x1*x3 m3= x1*x4 print('multiplication') print(m1) print(m2) print(m3) sub1= x2-x1 sub2= x2-x3 sub3= x1-x4 print('Subtraction') print(sub1) print(sub2) print(sub3) d1= x2/x1 d2= x2/x3 d3= x1/x4 print('Division') print(d1) print(d2) print(d3)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question