TechArena Community Design calculator with HashMap

#1
25-03-2010
 Member Join Date: Mar 2010 Posts: 202
Design calculator with HashMap

Hello
I started in java and I developed a calculator, then I put my affairs in a stringBuffer
Code:
`gender (45 * 34 + 89)`
I want to send this StringBuffer implements a tree in the order of precedence of operations, then I saw the HashMap. Now, my question is that, Is it a good ideas to using the hashmap in designing calculator. Any help is appreciated. Thanks in advance.
#2
25-03-2010
 Member Join Date: Dec 2009 Posts: 178
Re: Design calculator with HashMap

Hello,
I am also thinking of using the hashmap in designing something like calculator. I watched how the operations walked into a binary tree but I do not see how walking with a hashmap. Please guide me if I am wrong. Value takes a number and then successively operator
Here is an example
Code:
```could(key,25)
could(key +)
could(key,44)
could(key *)
could(key,8)```
but I do not know what can match the key. Any help will be great. Thanks
#3
25-03-2010
 Member Join Date: Nov 2009 Posts: 446
Re: Design calculator with HashMap

Hello,
I thought for a moment that you would just take the operators as keys and ordered by increasing priority.There are several ways to proceed:
One way is to determine the algorithm used to route to the operations. It has such expression prefix or inverted post-fix. Then it will just Use (Class Stack) to stack and unstack the operands. Otherwise there is the TreeMap class that implements binary trees. An elegant way is to use a composite design pattern. I do not know if any of my proposals you agree.
#4
25-03-2010
 Member Join Date: Nov 2009 Posts: 333
Re: Design calculator with HashMap

Hello,
You can try the following example
Code:
```2*3+3*2
gives
Root
/
+
/ \
* *
/ \ / \
2  3  3   2

2*(3+3)*2
gives
Root
/
*
/ \
*      2
/ \
2  +
/ \
3   3```
#5
25-03-2010
 Member Join Date: Nov 2009 Posts: 330
Re: Design calculator with HashMap

Hello,
I agree with the above code and it is the perfect way to proceed. To calculate: each node is a leaf if it has value, otherwise it is an operator, we take the result of a recursive subtree.
- Priority and therefore found the lowest in the tree.
Therefore a binary tree structure: Either tables Object [2], a class 2 attributes, one TreeNode (even if in Swing and it has not been integrated util). Hope this will help you.

 Tags: