码迷,mamicode.com
首页 > 编程语言 > 详细

Java表达式求值

时间:2016-09-19 17:55:59      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

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表达式求值

标签:

原文地址:http://www.cnblogs.com/ht-beyond/p/5885668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!