试图打印Postfix计算器的结果

时间:2015-03-30 23:51:58

标签: java methods arraylist printing stack

我正在为我的类构建一个简单的RPN计算器,我创建了一个字符串方法,将输入分解为标记并将它们推入堆栈。该方法已经完成并且编译完成,我的问题是我不知道如何打印结果,是否有人可以帮助我?这是我的代码。

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);
       compute(scan.nextLine());

   }



   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();
   }
}

1 个答案:

答案 0 :(得分:0)

修正了它:这里。我忘了你可以打印方法....

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(">");