【问题标题】:Postfix evaluation (small bug)后缀评估(小错误)
【发布时间】:2014-10-30 19:19:52
【问题描述】:

在这个问题的输入中,有一个以后缀表示法给出的语句,我的任务是评估给定的语句。我完全理解算法,我将在下面发布我的解决方案。但由于某种原因,它适用于这些: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();

}
}

【问题讨论】:

    标签: java data-structures stack postfix-notation


    【解决方案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()来读取下一个令牌。

    例如

    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
            }
        }
    }
    

    【讨论】:

    • 我知道这一点,但问题是我不知道该怎么做,因为我对 java 有点陌生,我需要帮助。 :)
    • @user3075088 我更新了我的答案,解释了如何直接解析表达式String,而不是char[]。
    • 感谢您的帮助:)
    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 2018-03-12
    • 2015-05-03
    • 2012-03-10
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    相关资源
    最近更新 更多