简体   繁体   中英

Convert string expression to mathematical expression?

I'm looking for something that would take a mathematical expression string as an input and would change it by adding parentheses to it.

For examples :

Input 1 : "2*4^7"

Output 1 : "(2*(4^7))"

Input 2 : "sin(x)*3-8^9"

Output 2 : "((sin(x)*3)-(8^9))"

Input 3 : "(2-8)/6"

Output 3 : "((2-8)/6)"

I'm currently trying to code it, but it's taking too much time and I would rather focus on other things, so if you guys know some module that could do that, that would be great.

I'm working with python 3

Thank you!

Using the ast module, you can parse your string and then traverse it to build a string representation you require:

import ast

def represent(tree):
    t = type(tree)
    if t == ast.Module:
        return represent(tree.body)
    elif t == list:
        # arithmetic expression should only be one thing
        return ", ".join(represent(part) for part in tree)
    elif t == ast.Expr:
        return represent(tree.value)
    elif t == ast.BinOp:
        o = type(tree.op)
        if o == ast.BitXor:
            op = "^"
        elif o == ast.Mult:
            op = "*"
        elif o == ast.Add:
            op = "+"
        ...
        return "({}{}{})".format(
                represent(tree.left),
                op,
                represent(tree.right),
                )
    elif t == ast.Num:
        return str(tree.n)
    elif t == ast.Name:
        return tree.id
    elif t == ast.Call:
        return "{}({})".format(
                represent(tree.func),
                represent(tree.args),
                )
    ...

# prints (((sin(x)*2)*x)+4)
print(represent(ast.parse("sin(x)*2*x+4")))

As @user2357112 noted in the comments, this will only work if you limit yourself to use Python's expression syntax, most importantly by using ** instead of ^ for exponentiation.

simply use eval(expression)

Replace character in a = '2*4^7' by b=a.replace('^','**') and so on eval(b)

some variable can be used

ex:

x= 3
eval("3 * x **2")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM