View the step-by-step solution to:

Question

I need help creating a fraction calculator based on the following python templates.


/>

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()




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





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





Sample input/output:

1/2 + 1/4

3/4

Another equation?: (q to quit)y

2/3 * 5/6

5/9

Another equation?: (q to quit)y

2/1 - 3/4

5/4

Another equation?: (q to quit)y

4/1 / 1/2

8/1

Another equation?: (q to quit)q

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Let our 24/7 Python Programming tutors help you get unstuck! Ask your first question.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors