java - 我无法让我的 PostFix Evaluator 正常工作

标签 java post postfix-notation evaluator

我不知道我应该在这里做什么,但我认为我的大部分代码都很好。我只能在 Evaluate() 方法中编辑代码。请帮忙。

这是我的主要方法的类

package labs.lab3;

import java.util.Scanner; // Needed for the Scanner
import java.io.*;         // Needed for the File and IOException

public class TestDriver {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException {
        System.out.printf("%-30s", "Postfix Expression");
        System.out.printf("%-30s", "Evaluation Result");
        System.out.println();
        String filename = "./src/labs/lab3/PostfixExpressions.txt";

        File file = new File(filename);
        Scanner inputFile = new Scanner(file);

        while (inputFile.hasNext())
        {

            String expression = inputFile.nextLine();
            System.out.printf("%-30s", expression);
            PostfixEvaluator evaluator = new PostfixEvaluator(expression);
            System.out.printf("%-30s" , evaluator.Evaluate());
            System.out.println();
        }

        inputFile.close();
    }

}

这是我的 Post Fix Evaluator 类:

package labs.lab3;

import java.util.Stack;
import java.util.EmptyStackException;
import java.util.StringTokenizer;

public class PostfixEvaluator
{
    private Stack<Integer> stack;
    private String expression;
    private String token;

    public PostfixEvaluator(String e)
    {
        stack = new Stack<Integer>();
        expression = e;
    }

        // Evaluate the postfix expression and return the evaluation result
    public int Evaluate()
    {
        int op1,op2;
        int result;
        StringTokenizer st = new StringTokenizer(expression);//split the expression into tokens
        String token=st.nextToken();

            while (st.hasMoreTokens()){

                if (Character.isDigit(token.charAt(0))) {
                 int value = Integer.parseInt(token);
                 stack.push(value);             
                }

                else if (!Character.isDigit(token.charAt(0))) {
                    op1=stack.pop();
                    op2=stack.pop();
                    result = Calculate(op1,op2,token.charAt(0));
                    stack.push(result);

                }

            }
            int answer = stack.pop();
            return answer;








    }

    // Perform an operation on the two operands
    public int Calculate(int operand1, int operand2, char operation)
    {
        int result = 0;

        switch (operation)
        {
        case '+':
            result = operand1 + operand2;
            break;
        case '-':
            result = operand1 - operand2;
            break;
        case '/':
            result = operand1 / operand2;
            break;
        case '*':
            result = operand1 * operand2;
            break;
        case '%':
            result = operand1 % operand2;
            break;
        }
        return result;
    }
}

谢谢

最佳答案

我看不到你在分词器中前进。您只需调用 nextToken 一次,在循环之外。其余代码似乎表明评估 应该消耗整个表达式,所以需要在里面调用nextToken 循环。

关于java - 我无法让我的 PostFix Evaluator 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22215797/

相关文章:

Python 前缀 后缀 中缀,无括号

java - Maven Surefire 插件未运行同一原始类的第二个测试类

java - 我无法向我自制的 txt 文件写入内容

java - 如何判断 Windows 批处理文件中的 JAVA_HOME 是否包含 JDK?

php - 在加密的 SSL 连接上使用 HTTP POST 发送信用卡信息是否足够安全?

node.js - Curl 正在获取 POST 响应,但 Node.js 未获取 POST 响应

php - 是否可以通过页面参数自动按下提交按钮?

java - 需要修复的Infix程序的后缀

将中缀表达式转换为后缀表达式,关联性总是从左到右吗?

java - 无法在 ListView 中跳过一行