Postfix评估(小bug)

时间:2014-10-30 19:19:52

标签: java data-structures stack postfix-notation

在输入的这个问题上有一个给出后缀表示法的语句,我的任务是评估给定的语句。我完全理解算法,我将在下面发布我的解决方案。但由于某种原因,它适用于这些:1 2 3 * + 5 - ,1 1 1 - - 1 + 1 +,1 2 + ...等但是当有多个数字的数字时它不起作用像这样:100 20 - 。我的解决方案(java):

public class PostFixEvaluation {

public static void main(String[] args) throws Exception{

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String expression = br.readLine();
    char exp[] = expression.toCharArray();

    ArrayStack<Integer> op = new ArrayStack<Integer>(100);

    int vrednost = 0;
    int dolzina = 0;
    int suma = 0;
    int res=0;
    int convert = 0;
    char ch;

    for(int i=0; i < exp.length; i++){
        ch = exp[i];
        if(Character.isDigit(ch)){
            convert = (int)ch-'0';
            op.push(convert);
            convert = 0;
        }
        else if(exp[i] == '+'){
                int x = op.pop();
                int y = op.pop();
                vrednost = x + y;
                op.push(vrednost);
                vrednost = 0;
        }
        else if(exp[i] == '-'){
                int x = op.pop();
                int y = op.pop();
                vrednost = y - x;
                op.push(vrednost);
                vrednost = 0;
        }
        else if(exp[i] == '*'){
                int x = op.pop();
                int y = op.pop();
                vrednost = y * x;
                op.push(vrednost);
                vrednost = 0;
        }
        else if(exp[i] == '/'){
                int x = op.pop();
                int y = op.pop();
                vrednost = y/x;
                op.push(vrednost);
                vrednost = 0;
        }
    } 

    res = op.pop();

    System.out.println(res);


    br.close();

}
}

1 个答案:

答案 0 :(得分:1)

你在这做什么......

for(int i=0; i < exp.length; i++){
    ch = exp[i];
    if(Character.isDigit(ch)){
        convert = (int)ch-'0';
        op.push(convert);
        convert = 0;
    }

...正在阅读单个字符,然后将其推送到您的数字堆栈中。问题是你没有检查这个字符是否只是一个数字的第一个数字。

如果您的代码遇到"100 20 -",它会将"1""0""0""2""0"推送到您的数字堆栈,而不是"100""20"

您需要重写解析代码以获取整个数字,而不是单独检查每个字符。

更新

关于更好的解析方法,Java有一些非常好的内置String解析工具。

在这种情况下,我建议使用Scanner来解析expression,而不是将其转换为char[]。您可以使用hasNext()hasNextInt()等方法检查是否有更多输入,以及该输入是int还是next()nextInt()来读取下一个标记。

e.g。

Scanner scanner = new Scanner(expression);
while (scanner.hasNext()) {                     // while there more input...
    if (scanner.hasNextInt()) {                 // is the next token an int?
        int number = scanner.nextInt();         // read the next token as an integer
        op.push(number);
    } else {                                    // the next token is NOT an int
        String operator = scanner.next();       // read the next token as a String
        if (operator.equals("+")) {
            // same as before
        } else if (operator.equals("-")) {
            // same as before
        } else if (operator.equals("*")) {
            // same as before
        } else if (operator.equals("/")) {
            // same as before
        }
    }
}