简体   繁体   English

我不能使我的PostFix评估器正常工作

[英]I can't make my PostFix Evaluator work correctly

I have no clue what I am supposed to be doing here, but I think I have most of my code good. 我不知道我应该在这里做什么,但我认为我的大部分代码都很好。 I can only edit code inside the Evaluate() method. 我只能在Evaluate()方法中编辑代码。 Pls help. 请帮忙。

Here is my class with my main method 这是我的主要方法课程

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

}

And Here is my Post Fix Evaluator class: 这是我的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;
    }
}

Thanks 谢谢

I can't see you move forward in the tokenizer. 我看不到你在tokenizer中前进了。 You call nextToken just once, outside the loop. 你只需要在循环外调用nextToken一次。 The rest of the code seems to indicate that evaluate should consume the entire expression, So nextToken needs to be called inside the loop. 代码的其余部分似乎表明evaluate应该使用整个表达式,因此需要在循环内调用nextToken

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM