java - How do I create a memory for a calculator with hashmap? -


okay building simple postfix calculator in java , asked create couple functions it, 1 struggling memory. heard hashmap, , did research on it, don't think quite understand how implement program yet. way program work user start , postfix calculator , prompted input this:

java postfixcalc integer postfix calculator memory > 

but has option assign variable input, example:

> = 3 5 + 1 - 7 > bee = 3 * 21 > bee + 28 > bee 3 % 0 > = 4 4  > 57 57 > 2 c + c not found > mem a: 4 bee: 21 > exit 

here's code far. im figuring should tokenize input , put in array list first in order variable name, unless theres better way.

import java.util.*; import java.io.*; public class program6 {     public static void main(string args[])     {         system.out.println("servando hernandez");         system.out.println("rpn command line calculator");         scanner scan = new scanner(system.in);         system.out.print(">");         while(scan.hasnextline())         {              system.out.print("> ");              string = scan.nextline();               string b = "quit";              string c = "mem";              string d = "clear";              if(a.equals(b))              {                  system.exit(0);              }              else              {                   system.out.println(compute(a));              }              system.out.print(">");           }        }          public static string compute(string input)       {           list<string> processedlist = new arraylist<string>();           if (!input.isempty())            {               stringtokenizer st = new stringtokenizer(input);               while (st.hasmoretokens())               {                  processedlist.add(st.nexttoken());               }           }            else           {                return "error";           }          stack<string> templist = new stack<string>();           iterator<string> iter = processedlist.iterator();           while (iter.hasnext())          {             string temp = iter.next();             if (temp.matches("[0-9]*"))             {                  templist.push(temp);                 }                 else if (temp.matches("[*-/+]"))                  {                      if (temp.equals("*"))                      {                         int rs = integer.parseint(templist.pop());                         int ls = integer.parseint(templist.pop());                         int result = ls * rs;                         templist.push("" + result);                     }                      else if (temp.equals("-"))                      {                         int rs = integer.parseint(templist.pop());                         int ls = integer.parseint(templist.pop());                         int result = ls - rs;                         templist.push("" + result);                     }                      else if (temp.equals("/"))                      {                         int rs = integer.parseint(templist.pop());                         int ls = integer.parseint(templist.pop());                         int result = ls / rs;                         templist.push("" + result);                     }                      else if (temp.equals("+"))                      {                         int rs = integer.parseint(templist.pop());                         int ls = integer.parseint(templist.pop());                         int result = ls + rs;                         templist.push("" + result);                     }                  }                 else                 {                     return "error";                 }             }           return templist.pop();         }       }        private static string hashmap(string q)      {          list<string> memory = new arraylist<string>();          if(!q.isempty())          {              stringtokenizer var = new stringtokenizer(q);              while(q.hasmoretokens())              {                   memory.add(q.nexttoken());              }          }              hashmap h = new hashmap();      }   }//end of class 

i think idea of hash map memory insert key-value pairs key variable name (a string) , value value of variable (an integer).

e.g., after evaluating a = 3 5 + 1 - add ("a", 7) memory hash map. when want evaluate bee = 3 * can value of a in hashmap, 7, , calculation that. after calculation add ("bee", 21) memory hash map.

and on that.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -