Results 1 to 5 of 5

Thread: Design calculator with HashMap

  1. #1
    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. #2
    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. #3
    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. #4
    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. #5
    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.

Similar Threads

  1. Associated values with HashMap
    By Miles Runner in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 03:50 AM
  2. Sorting a hashmap
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 10-02-2010, 05:06 AM
  3. Iteration over a hashmap
    By New ID in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 03:40 AM
  4. Replace calculator with Command Line Calculator
    By Eric B in forum Windows Software
    Replies: 3
    Last Post: 07-05-2009, 04:32 PM
  5. What is HashMap and Map?
    By TAIPA in forum Software Development
    Replies: 4
    Last Post: 27-02-2009, 11:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,567,102.93909 seconds with 17 queries