Java表达式求值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java表达式求值相关的知识,希望对你有一定的参考价值。

import java.math.BigInteger;
import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext())
        {
            String str1 = scan.next();
            
            Stack<BigInteger> num = new Stack<BigInteger>();
            Stack<Character> op = new Stack<Character>();
            // +,-,*,/,^,()
            for(int i=0;i<str1.length();i++)
            {
                char ch = str1.charAt(i);
                switch(ch)
                {
                case ‘+‘:
                case ‘-‘:
                    // 这些优先级都大于等于当前操作符
                    while(!op.empty() &&
                            (op.peek()==‘+‘ ||
                            op.peek()==‘-‘ ||
                            op.peek()==‘*‘ ||
                            op.peek()==‘/‘ ||
                            op.peek()==‘^‘))
                    {
                        calc_stack(num, op);
                    }
                    op.push(ch);
                    break;
                case ‘*‘:
                case ‘/‘:
                    while(!op.empty() &&
                            (op.peek()==‘*‘ ||
                            op.peek()==‘/‘ ||
                            op.peek()==‘^‘))
                    {
                        calc_stack(num, op);
                    }
                    op.push(ch);
                    break;
                case ‘^‘:
                    while(!op.empty() && op.peek()==‘^‘)
                    {
                        calc_stack(num, op);
                    }
                    op.push(ch);
                    break;
                case ‘(‘:
                    op.push(ch);
                    break;
                case ‘)‘:
                    while(op.peek()!=‘(‘)
                    {
                        calc_stack(num, op);
                    }
                    // ‘(‘出栈
                    op.pop();
                    break;
                default:
                    String number = String.valueOf(ch);
                    while(i+1<str1.length() && Character.isDigit(str1.charAt(i+1)))
                    {
                        number +=String.valueOf(str1.charAt(i+1));
                        i++;
                    }
                    BigInteger big = new BigInteger(number);
                    num.push(big);
                    break;
                }
            }
            while(!op.empty())
            {
                calc_stack(num, op);
            }
            System.out.println(num.pop());
        }
        scan.close();
    }
    
    private static void calc_stack(Stack<BigInteger>num, Stack<Character>op)
    {
        char operator = op.pop();
        BigInteger num1 = num.pop();
        BigInteger num2 = num.pop();
        switch(operator)
        {
        case ‘+‘: num.push(num2.add(num1));break;
        case ‘-‘: num.push(num2.subtract(num1));break;
        case ‘*‘: num.push(num2.multiply(num1));break;
        case ‘/‘: num.push(num2.divide(num1));break;
        case ‘^‘: num.push(num2.pow(num1.intValue()));break;
        }
    }
}

 

以上是关于Java表达式求值的主要内容,如果未能解决你的问题,请参考以下文章

Java 表达式中子表达式的求值顺序

JAVA练习99-逆波兰表达式求值

Java 计算数学表达式(字符串解析求值工具)

中缀表达式转后缀表达式(Java代码实现)

java实现算术表达式求值

Java表达式求值