Hey there,
I'm trying to write a calculator from blank (without using any Math. commands).
I get a string as an input and need to calculate it and print the result.
I am having a bit of a writers block if you will, as to how properly deal with brackets and math operations order (mul/div before add/sub).
So far, my calculator can deal very well with normal operations - I build a sort of a tree data structure whose father is the operators, and the sons are the operands.
Now I need to deal with something like this:
5 * 6 + 2 * (5 + 67 - 2 / (2 + 3))
Also a big plus is having it calculate it without going over the equation over and over. One time would be perfect.
Note that the difficulty is not the coding but rather the logic behind it :)
Thanks,
PoOoZaQ
2 replies to this topic
#1
Posted 04 March 2011 - 02:56 AM
|
|
|
#2
Posted 05 March 2011 - 08:51 AM
This isn't a very difficult problem, especially considering you've already built a parsing tree. I assume what you meant was you're having trouble processing the parentheses.
When your scanner encounters an open paren, use a count variable to count the number of open paren's you've encountered. Open paren gives your count variable a ++, and a close paren gives it a --. This will help to make sure all the parens are balanced. If the variable is not zero at the end of scanning the input, yield an unmatched paren error.
When you find the matching close paren for a particular open paren, you treat the entire entity as one operand for the next operator up on the tree. In your example, your tree would consist of the + operator at the top, descending down to two * operators. The one on the left would branch off to the 5 and the 6, and the one on the right would branch off to the 2 and the (5+67-2/(2+3). Of course, everything in between those parentheses would be represented as their own parse tree. The root of this tree would be the /, branching to the left with the + and to the right with the (2+3).
You would recursively parse each set of parentheses, issuing its root node as the operand to the operator that came before it. The actual paren symbols would not be stored in the tree structure whatsoever, since the tree structure is unambiguous toward the order of operations, naturally.
Does that answer your question? I can draw a picture of the final parse tree if you don't see it if you'd like.
When your scanner encounters an open paren, use a count variable to count the number of open paren's you've encountered. Open paren gives your count variable a ++, and a close paren gives it a --. This will help to make sure all the parens are balanced. If the variable is not zero at the end of scanning the input, yield an unmatched paren error.
When you find the matching close paren for a particular open paren, you treat the entire entity as one operand for the next operator up on the tree. In your example, your tree would consist of the + operator at the top, descending down to two * operators. The one on the left would branch off to the 5 and the 6, and the one on the right would branch off to the 2 and the (5+67-2/(2+3). Of course, everything in between those parentheses would be represented as their own parse tree. The root of this tree would be the /, branching to the left with the + and to the right with the (2+3).
You would recursively parse each set of parentheses, issuing its root node as the operand to the operator that came before it. The actual paren symbols would not be stored in the tree structure whatsoever, since the tree structure is unambiguous toward the order of operations, naturally.
Does that answer your question? I can draw a picture of the final parse tree if you don't see it if you'd like.
#3
Posted 05 March 2011 - 10:49 AM
i've saw how to make that program on microsoft e-learning while i was there but i can' find my notebook where i did write code for it...try to search on google c# calculator microsoft e-learning
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









