java - 如何使用 HashMap 为计算器创建内存?

标签 java arraylist hashmap calculator stringtokenizer

好吧,我正在用 Java 构建一个简单的 PostFix 计算器,我被要求为其创建几个函数,其中一个令我困扰的是内存。我听说你可以用 HashMap 来做到这一点,我对它进行了研究,但我认为我还不太明白如何将它实现到我的程序中。该程序的工作方式是用户启动它,它会说它是一个 postFix 计算器,并提示输入如下:

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

最佳答案

我认为内存 HashMap 的想法是,您将插入键值对,其中键是变量名称(字符串),值是变量的值(整数)。

例如,在评估 a = 3 5 + 1 - 后,您可以将 ("a", 7) 添加到内存 HashMap 中。然后当你想要计算bee = a 3 *时,你可以在hashmap中查找a的值,它是7,然后用它进行计算。计算完成后,您将添加 ("bee", 21) 到内存 HashMap 中。

就这样。

关于java - 如何使用 HashMap 为计算器创建内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29397508/

相关文章:

java - int 无法转换为 charsequence

javascript - 如何正确实现 HashMap /我是否需要一个

java - 如何获取 HashMap<String, Integer> 中可能值的列表?

java - 将 HashMap 与对象进行比较

java - 未命名类的垃圾收集

Java ForkJoinPool 线程限制还是 Java 流替代方案?

java - JAXB - 来自 XmlAttribute 的标志 'required' 在原始类型上被忽略

java - AutoCompleteTextView 不显示结果

java - 检查字符串是否包含确切的关键字

java - bitmap.compress(Bitmap.compressFormat.JPEG,100, baos) 给我 java.lang.OutOfMemoryError