简体   繁体   English

后缀评估器

[英]Postfix Evaluator

I want to create a Postfix Evaluator that works with multidigits and decimal numbers.我想创建一个适用于多位数字和十进制数字的后缀评估器。 The program works with multidigits but it does not with decimals.该程序适用于多位数,但不适用于小数。 How can I make this?我怎样才能做到这一点? I have used the infix expression: "10 + 20 * ( 50 / 3 ) + 4", which in postfix is "10 20 50 3 / * + 4 +".我使用了中缀表达式:“10 + 20 * (50 / 3) + 4”,在后缀中是“10 20 50 3 / * + 4 +”。 As a result I have got 347.33333333333337, which is correct.结果我得到了 347.33333333333337,这是正确的。 I just need the evaluator to work with decimal numbers.我只需要评估器处理十进制数。

public class EvaluarExpresion {
public static double evaluaExpresion (String postfija) {
    MyStack<Double> stack = new MyStack<Double>();
    //String postfija= expresionPostFijo();
    for(int i = 0; i < postfija.length(); i++) { 
        char c = postfija.charAt(i); 
        if(c == ' ') {
            continue; 
        }else if(Character.isDigit(c) || c == '.') { 
            int n = 0; 
            int divider =1;
            boolean hasDecimal = false;

            while(Character.isDigit(c) || c == '.') { 
                if(c == '.') {
                    hasDecimal = true;
                } else {
                    if(hasDecimal) {
                        divider *=10;
                    }

                n = n*10 + (int)(c-'0'); 
                c = postfija.charAt(i); 
                }
                i++; 
            } 
            i--; 
            stack.push((double) n); 
        } else {
               Double val1 = stack.pop(); 
               Double val2 = stack.pop(); 
               switch(c) 
                { 
                    case '+': 
                    stack.push(val2+val1); 
                    break; 

                    case '-': 
                    stack.push(val2- val1); 
                    break; 

                    case '/': 
                    stack.push(val2/val1); 
                    break; 

                    case '*': 
                    stack.push(val2*val1); 
                    break; 
                    case '^': 
                    stack.push(Math.pow(val2, val1)); 
                    break; 
              } 
           }      
    }
    return stack.pop();
}

    public static void main(String[] args) {
        // This is an example of an infix expression  String dato = "10 + 20 * ( 50 / 3 ) + 4"; 
        //The expression provided below is a postfix expression
        System.out.println(evaluaExpresion("10 20 50 3 / * + 4 +")); 
       //The result is 347.33333333333337 which is correct


    }
}

There are a number of ways this can be done.有多种方法可以做到这一点。 What you need to do is look for the ' .您需要做的是寻找 ' . ' character, and start some process for dividing by powers of ten once you find a decimal point. ' 字符,并在找到小数点后开始一些除以十的幂的过程。 The following way should work, and gets all of the division done in one step, reducing round-off error:以下方法应该可行,并且可以一步完成所有除法,从而减少舍入误差:

        } else if (Character.isDigit(c) || c == '.') { 
            int n = 0; 
            int divider = 1;
            boolean hasDecimal = false;

            while (Character.isDigit(c) || c == '.') {
                if (c == '.') {
                    hasDecimal = true;
                } else {
                    if (hasDecimal) {
                        divider *= 10;
                    }

                    n = n * 10 + (int) (c - '0'); 

                    c = postfija.charAt(i); 
                }
                i++;
             }

            i--; 
            stack.push((double) n / divider); 
        } else { // etc...

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

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