Question: How do i complete this code for a fraction calculator? class Fraction: # constructor for Fraction # default def __init__(self, n=0, d=1): # set private
How do i complete this code for a fraction calculator?
class Fraction: # constructor for Fraction # default def __init__(self, n=0, d=1): # set private numerator & denominator fields and reduce the fraction
def __str__(self): # return a string of how the fraction should look
def reduce(self): #code to reduce itself based on the gcf method
def add(self, other): result = Fraction() # adds self and other return result
def mult(self, other): result = Fraction() #multiplies self and other return result
def div(self, other): result = Fraction() #divides self and other return result
def sub(self, other): result = Fraction() #subtracts self and other return result
def gcf(self, num1, num2): # returns greatest common factor of num1 & num2
------------------------------------------------(Different file)
import fraction
class Equation: def __init__(self, f1 = fraction.Fraction(), f2 = fraction.Fraction(), op=''): #sets data fields to the parameters if values are given
def __str__(self): # returns string how it should look
def read_fraction(self, equ): num = 0 i = 0 while equ[i] in ['0','1','2', '3', '4','5','6','7','8','9', '10']: num = num * 10 + int(equ[i]) i += 1
# Remove the / character equ = equ[i+1:]
i = 0 denom = 0 while equ[i] in ['0','1','2', '3', '4','5','6','7','8','9', '10']: denom = denom * 10 + int(equ[i]) i += 1 frac = fraction.Fraction(num,denom) return frac, equ[i:].lstrip()
def read_equation(self): inputed_equ = input() inputed_equ += ' '
frac1, inputed_equ = self.read_fraction(inputed_equ)
oper = inputed_equ[0]
inputed_equ = inputed_equ[1:].lstrip() frac2, inputed_equ = self.read_fraction(inputed_equ) self.__frac1 = frac1 self.__frac2 = frac2 self.__oper = oper
def compute_equation(self): #returns fraction of computed equation
-------------------------------------------(Different file)
import equation
def main(): equ = equation.Equation() again = 'y' while again != 'q': equ.read_equation() frac = equ.compute_equation() print(frac) again = input('Another equation?: (q to quit)')
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
