2015-04-01 47 views
0

好吧,我正在Java中構建一個簡單的PostFix計算器,並且我被要求爲它創建一對功能,我正在努力處理的是內存。我聽說你可以用HashMap來做,我也研究過它,但我不認爲我很瞭解如何將它實現到我的程序中。該計劃的工作方式是,用戶將啓動它,它會說,它的後綴計算器,將被提示這樣的輸入:如何使用散列表爲計算器創建內存?

java PostfixCalc 
Integer Postfix calculator with memory 
> 

但他有一個變量分配給他的輸入選項,例如:

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

這是我的代碼到目前爲止。我想我應該標記輸入並將其放入數組列表中以獲取變量名稱,除非是更好的方法。

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 a = 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 
+0

Map接口和HashMap實現已經是Java的一部分。 Map resultsMap = new HashMap <>();然後,當您確定 = 時,如果您需要查找的值,請使用resultsMap.get()(如果不存在,則返回null,請參閱上面的c),並使用resultsMap.put(對的列表。 – JimW 2015-04-01 19:23:48

回答

1

我認爲哈希映射內存的想法是,你會插入鍵值對,其中關鍵是變量名(字符串)和值是變量(整數)的值。

例如,在評估a = 3 5 + 1 -之後,您會將("a", 7)添加到您的內存哈希映射中。然後,當你想評估bee = a 3 *時,你可以在散列表中查找a的值,該值爲7,然後用這個值進行計算。在計算完成後,您可以將("bee", 21)添加到您的內存哈希映射中。

就這樣。