nyoj 35 表达式求值

[TOC]
题目:表达式求值

nyoj 35 表达式求值_第1张图片
1.png

思路:

Stack ops存操作
Stack vals存操作数

  1. 碰到操作数,如果ops.peek()是否为"*","/",如果是则计算一次;
  2. 碰到"+","-",如果ops.peek()是否为"+","-",如果是则计算一次;
  3. 碰到"(","*","/"直接入符号栈;
  4. 碰到")"计算到"("为止,如果"("出栈后栈顶为"*","/"则驱除两个,计算一次;

测试数据说明:

  1. "*","/" 应该是从前往后计算,而不是从后往前计算
(1+2)*5.156/54*4.154/47852*41463=
((1+2)*5.156/54+4.154)/47852*41463-56*78/32*0.154=
  1. "*","/" 应该是从前往后计算,而不是从后往前计算
1-5+3-2=
  1. 碰到")",计算直到"(",此时当判断"("前是不是为"*","/"若是,则计算一次
5*(1-5*8)-1=

代码

import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack ops = new Stack();
        Stack vals = new Stack();
        Scanner sc = new Scanner(System.in);
        int T;
        T = sc.nextInt();
        while (true) {
            if (T == 0) break;
            T--;
            String str = sc.next();
            str = str.replace("(", " ( ").replace(")", " ) ").replace("+", " + ").replace("-", " - ").replace("*", " * ").replace("/", " / ").replace("="," = ");
            String[] ss = str.split(" ");
            for (String s : ss) {
                //System.out.println(s);
                //处理
                if(s.equals(""));
                else if(s.equals("=")){
                    while(!ops.isEmpty()){
                        double val=vals.pop();
                        String op=ops.pop();
                        if(op.equals("+")) val=val+vals.pop();
                        else val=vals.pop()-val;
                        vals.push(val);
                    }
                }
                else if (s.equals("(") || s.equals("*")||s.equals("/")) ops.push(s);
                else if(s.equals("+")||s.equals("-")){
                    if(!ops.isEmpty()&&(ops.peek().equals("+")||ops.peek().equals("-"))){
                        double val=vals.pop();
                        String op=ops.pop();
                        if(op.equals("+")) val=val+vals.pop();
                        else if(op.equals("-")) val=vals.pop()-val;
                        vals.push(val);
                    }
                    ops.push(s);
                }
                else if (s.equals(")")){
                    while(!ops.isEmpty()&&!ops.peek().equals("(")){
                        double val=vals.pop();
                        String op=ops.pop();
                        if(op.equals("+")) val=val+vals.pop();
                        else if(op.equals("-")) val=vals.pop()-val;
                        vals.push(val);
                    }
                    if(!ops.isEmpty()) ops.pop();
                    //作* /的运算
                    if(!ops.isEmpty()&&(ops.peek().equals("*")||ops.peek().equals("/"))){
                        double val=vals.pop();
                        String op=ops.pop();
                        if(op.equals("*")) val=val*vals.pop();
                        else val=vals.pop()/val;
                        vals.push(val);
                    }
                }else{
                    //碰到数字的时候做* / 运算一次,不做+ - 运算
                    double val=Double.parseDouble(s);
                    if(!ops.isEmpty()&&(ops.peek().equals("*")||ops.peek().equals("/"))){
                        String op=ops.pop();
                        if(op.equals("*")) val=val*vals.pop();
                        else val=vals.pop()/val;
                    }
                    vals.push(val);
                }
            }
            System.out.printf("%.2f\n",vals.pop());
        }
    }
}

你可能感兴趣的:(nyoj 35 表达式求值)