问题
How do you, given any string expression, populate a tree?
I have an assignment that I am stuck on.
I need to evaluate the following expression (( 5 * 10 ) /2 - (( 2 + 3) + 6)) using any data structure.
Using a stack, I am able to verify that the string is well formed. But how can I add the various values into a tree and then evaluate them in order.
Please give me any hints you may have on how I can read the string ((( 490 * 9 ) / 2)/5/6 - (( 2/4 + 3) + 6 * 5))
For instance, How do I get the (-) to be the root of the three when it is the 15th substring in the input expression? How do I make sure that the (/)6 expression happens after (/)5 etc.
回答1:
What you are looking for is provided here: http://www.codeproject.com/Articles/88435/Simple-Guide-to-Mathematical-Expression-Parsing
回答2:
evaluate any string [representing a mathematical] expression
You need to do three things:
- define a type for the abstract syntax tree representing your language
- write a recursive descent parser for your syntax
- write an interpreter for your syntax
You can do this very easily.
I'll use Haskell notation, since it is concise, but you can translate it if you wish.
A type for your language:
data Exp = Op BinOp Exp Exp
| Lit Int
data BinOp = Plus
| Times
| Div
| Sub
now, we can write an interpreter for expressions in this language:
eval :: Exp -> Int
eval (Lit n) = n
eval (Op o e1 e2) =
let v1 = eval e1
v2 = eval e2
in case o of
Plus -> v1 + v2
Times -> v1 * v2
Sub -> v1 - v2
Div -> v1 / v2 -- note, may fail!
Ok. So that's the evaluator for terms in your language. Now write a recursive descent parser...
回答3:
The parenthesis denote the order of operations so similarly to how you used the stack to make sure everything matched you can parse out those matches and use them as elements in the tree to create the hierarchy for evaluation where the operators are the children of each breadth-wise level of parenthesis matches.
回答4:
The other answers here cover reading the expression tree into memory, but they don't cover evaluating the tree once you've built it.
The general strategy for evaluating the tree is that the tree should consist of nodes and leaves, where nodes are operators with children, and leaves are the constant values. For example, the expression (((490 * 9) / 2) / 5 / 6 - ((2 / 4 + 3) + 6 * 5)) converts to the tree
-
/ \
/ \
div +
/ \ / \
div 6 + *
/ \ / \ / \
div 5 div 3 6 5
/ \ / \
* 2 2 4
/ \
450 9
To evaluate this tree, you start at the root. You first recursively evaluate the left subtree and the right subtree, and then apply the operator to the results of the two subtree evaluations. Each leaf (number) evaluates to itself.
In this case, you would start at the root (-), evaluate the left subtree down to 73.5, evaluate the right subtree down to 33.5, and subtract to get a final result of 40.
As a more precise example, let's look at the nodes on the far left of the tree, after several layers of recursive calls have been made. The leaf 450 evaluates to 450, the leaf 9 evaluates to 9, and the node (* 450 9) (writing nodes of the tree in Lisp-like prefix notation) evaluates to 4050. So, from the node (/ (* 450 9) 2), the left recursive call evaluates to 4050 and the right recursive call evaluates to 2, meaning that the overall node evaluates to 2025. As long as you make the recursive calls from the nodes before combining the values, and make sure each leaf evaluates to itself, tree evaluation is straightforward.
回答5:
Edit the second: op clarified, see other answers
来源:https://stackoverflow.com/questions/10642959/how-to-evaluate-any-given-expression