1

Ok so basically I have an assignment in my first comp sci class in high school and I was able to figure out how to do most of it. We are supposed to make a calculator that can do basic functions with the PEMDAS operators and do calculations that cross over the different operators. I was able to get most of it down, but I can't figure out how to get the division, subtraction, or exponentiation to work with more than 2 numbers. Please help and thank you!

Assignment Instructions:

In the following exercise, you will be creating a program that uses recursion to solve any basic equation (addition, subtraction, multiplication, division, and exponents). Below is a framework for your program.

equation = input("Please enter an equation: ") 
    
def solveIt(e): 
    # loop through e
        # if you find a plus sign
            # return recursive call @param part of e before the plus sign + 
                    # recursive call @param part of e after the plus sign
    return e
print(solveIt(equation))

Do not change any of the existing code. The instructions provided will help you get your code to solve equations with addition. Test your code with a compound equation like 343 + 87 + 5, and verify the correct result before you move on to the next step.

Now here is the code that I have which I can use to do 8/2, but I cannot do 8/2/2:

expression = input("Please enter an equation: ") 
equation = expression.replace(' ', '')
res=0
total=0
def solveIt(e): 
    global res, total
    for i in range(len(e)):
        if e[i]=="/":
            e1 = e[:i]
            e2 = e[i+1:]
            total=total+solveIt(e1) / solveIt(e2)
            return total
    return int(e)

(solveIt(equation))
print("total:",total)

Thanks for the help!

SamLang
  • 11
  • 1

1 Answers1

0

You're going to have all sorts of problems with the general approach you're using for evaluating numeric expressions. You will have problems with operator precedence, proper grouping of terms, and the proper order of evaluation.

With that said, the way to fix your code for just the one case of a series of divisions is to work on the input expression in the other direction. That is, look for each '/' starting at the end of the expression and working back towards the beginning. Here's just that one small change to your code that causes expressions like 12/3/2 to evaluate correctly:

expression = input("Please enter an equation: ")
equation = expression.replace(' ', '')
res=0
total=0
def solveIt(e):
    global res, total
    for i in range(len(e)-1, -1, -1):
        if e[i]=="/":
            e1 = e[:i]
            e2 = e[i+1:]
            total=total+solveIt(e1) / solveIt(e2)
            return total
    return int(e)

(solveIt(equation))
print("total:",total)

Sample input:

10000/10/10/5/4

Resulting output:

total: 5.0
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Shouldn't we try to group the elements before we do division operation. Or else the answers might be different right? – Stan11 Dec 16 '20 at 05:52
  • @Steve Thank you for the help, I appreciate it. – SamLang Dec 16 '20 at 05:54
  • @nithin11 - we are grouping the elements by the very nature of what the code is doing. The reason that the OPs version of the code fails and this version works is that working on the expression in the opposite direction causes the terms of the expression to be grouped differently. It's just a fact that working on the expression from the end to the beginning leads to a proper grouping using this very simple approach to the problem. – CryptoFool Dec 16 '20 at 05:57