Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote this calculator for +,-,/,* and I am trying to apply the () to it.
e.g. 5+(3*2(2+3))


This is the calculator I wrote, which works perfectly without the parenthesis:
class Node(object):
    def __init__(self, kappa):
        i = -1
        if kappa.rfind("+") != -1:
            i = kappa.rfind("+")
        elif kappa.rfind("-") != -1:
            i = kappa.rfind("-")
        elif kappa.rfind("*") != -1:
            i = kappa.rfind("*")
        elif kappa.rfind("/") != -1:
            i = kappa.rfind("/")
        
        if i != -1:
            self.data = kappa[i]
            self.left = Node(kappa[:i])
            self.right = Node(kappa[i + 1:])
        else:
            self.data = float(kappa)
            self.left = None
            self.right = None
            
    def calculate(self):
        if self.data == "+":
            return self.left.calculate() + self.right.calculate()
        elif self.data == "-":
            return self.left.calculate() - self.right.calculate()
        elif self.data == "*":
            return self.left.calculate() * self.right.calculate()
        elif self.data == "/":
            return self.left.calculate() / self.right.calculate()
        else:
            return self.data


What I have tried:

I am not really sure where is the ideal place for the implementation of the parenthesis here, should I make a new method or should I add it in the __init__ method part?

I wrote this function that changes the values bounded by brackets to $:

<pre>def parenthesis_replace(kappa):
    s = ""
    level = 0
    for c in kappa:
        if c == "(":
            level += 1
        if level > 0:
            s += "$"
        else:
            s += c
        if c == ")":
            level -= 1
    return s
    

print parenthesis_replace("(1-2)-3")  


I get :
$$$$$-3

And this one works perfectly for the intended purpose, I was thinking maybe I can use this to neglect the values in between two brackets until I find the operators outside them and put the rest in the left or right node accordingly. But I am still very confused how to do that, tried many ways but always stuck.

Also, it came to me that this way if the brackets are between the whole input then it fails to calculate anything.

Any hints or advice greatly Appreciated
Posted
Updated 11-May-18 20:12pm
Comments
Member 13814092 11-May-18 19:07pm    
This is how you call the function :

def calculator(node):
return node.calculate()
print calculator(Node("5+(3*2(2+3))"))

1 solution

I will not write the full code for that, but here are the steps you have to take:

If your input contains parentheses, you should find the parentheses that do not have parentheses inside them. You can do this using the re module, with re.search:
Python
# re.search example
import re

expression = "(6+(7+8))"
match = re.search("\\(([^()]+)\\)", expression)
print(match.group(0)) # prints (7+8)
print(match.group(1)) # prints 7+8

So you perform that on your actual input. .group(1) will give you the simple expression without parentheses, .group(0) will give you the expression with its parentheses. You then need to evaluate the expression of .group(1) (which isn't hard, you have a function for that already) and then, in the original string, you have to replace the value of .group(0) with the result of the evaluation of .group(1), using the .replace method on strings.

But there may be more parentheses pairs in one expression, so you have to repeat the above process until there are no more parentheses in the expression.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900