测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
1 + 2 4 + 2 * 5 - 7 / 11 0
3.00 13.36
import java.util.Scanner; import java.util.Stack; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static Double operation(double a, double b, String op) { if ("+".equals(op)) return b + a; else if ("-".equals(op)) return b - a; else if ("*".equals(op)) return b * a; else return b / a; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String str = sc.nextLine(); if ("0".equals(str)) break; String[] data = str.split(" "); Stack<String> op = new Stack<>(); //存储操作符 Stack<Double> num = new Stack<>(); //存储操作数 for (String datum : data) { if ("*".equals(datum) || "/".equals(datum)) { //如果栈为空,或者栈中操作符优先级比该操作符优先级低,则直接入栈 if (op.isEmpty() || "+".equals(op.peek()) || "-".equals(op.peek())) { op.push(datum); } //否则可以进行运算 else { //弹出栈中所有与该运算符优先级相等的运算符并做运算 while (!op.isEmpty()) { if ("+".equals(op.peek()) || "-".equals(op.peek())) break; //做相应运算后将结果入操作数栈 double a = num.pop(); double b = num.pop(); String operator = op.pop(); num.push(operation(a, b, operator)); } //将当前运算符入操作符栈 op.push(datum); } } //如果是+,-,则要看操作符栈是否为空,如果不为空则先弹出所有操作符,并进行运算,否则就直接入栈 else if ("+".equals(datum) || "-".equals(datum)) { //弹出栈中所有运算符并做运算 while (!op.isEmpty()) { //做相应运算后将结果入操作数栈 double a = num.pop(); double b = num.pop(); String operator = op.pop(); num.push(operation(a, b, operator)); } //将当前运算符入操作符栈 op.push(datum); } //将操作数入栈 else { double shu = Double.parseDouble(datum); num.push(shu); } } //最后再做剩余的未计算完的操作 while (!op.isEmpty()) { double a = num.pop(); double b = num.pop(); String operator = op.pop(); num.push(operation(a, b, operator)); } //最后留在操作数栈中的数即为结果 System.out.printf("%.2f\n", num.pop()); } } }
import java.text.DecimalFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ String s = scanner.nextLine(); s=s.replace(" ",""); //中缀表达式转为后缀表达式 List<String> s1=transform(s); //后缀表达式计算求值 double result= calculate(s1); DecimalFormat f = new DecimalFormat("0.00"); System.out.println(f.format(result)); } } private static List<String> transform(String s) { Stack<String> stack = new Stack<>(); ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { String c= String.valueOf(s.charAt(i)); switch (c){ //统一 + - 处理 case "+": case "-": while (!stack.empty()) list.add(stack.pop()); stack.push(c); break; //统一 * / 处理 case "*": case "/": while (!stack.empty()&&(stack.peek().equals("*") || stack.peek().equals("/"))) list.add(stack.pop()); stack.push(c); break; //0-9 处理 default: int num= Integer.parseInt(c); while (i<s.length()-1&& Character.isDigit(s.charAt(i+1))){ num=num*10+s.charAt(i+1)-'0'; i++; } list.add(String.valueOf(num)); } } while (!stack.empty()) list.add(stack.pop()); return list; } private static double calculate(List<String> list) { Stack<Double> stack = new Stack<>(); for (String s : list) { switch (s) { case "+": stack.push(stack.pop() + stack.pop()); break; case "-": double first = stack.pop(); double second = stack.pop(); stack.push(second - first); break; case "*": stack.push(stack.pop() * stack.pop()); break; case "/": double first1 = stack.pop(); double second1 = stack.pop(); stack.push(second1 / first1); break; // num default: stack.push(Double.parseDouble(s)); } } return stack.pop(); } }
package com.test; import java.util.LinkedList; import java.util.Scanner; class Op1{ char operator; int priority; public Op1() { super(); } public Op1(char operator, int priority) { super(); this.operator = operator; this.priority = priority; } } public class Main016_3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = ""; while(sc.hasNext()){ str = sc.nextLine(); if (str.equals("0")) { break; } LinkedList<Double> s1 = new LinkedList<Double>(); //存数字 LinkedList<Op1> s2 = new LinkedList<Op1>(); //存运算符 char[] c = str.toCharArray(); String num =""; for (int i = 0; i < c.length; i++) { if(c[i]>='0'&&c[i]<='9'){ num+=c[i]; continue; }else if(c[i]!=' '){ if(!num.isEmpty()){ s1.add(Double.parseDouble(num)); } Op1 op = new Op1(); op.operator = c[i]; switch (c[i]) { case '+': case '-': op.priority=1; break; case '*': case '/': op.priority=2; break; default: break; } //左是栈底,右是栈顶 //如果栈为空或者栈顶运算符优先级比当前低,压栈 if(s2.isEmpty()||op.priority>s2.getLast().priority) { s2.add(op); }//如果栈不空且栈顶运算符优先级大于等于当前运算符,则运算,直到不满足此条件 else { char yun; while(!s2.isEmpty() && op.priority <= s2.getLast().priority){ double n1 = s1.pollLast(); double n2 = s1.pollLast(); yun = s2.pollLast().operator; switch (yun) { case '+': s1.add(n2+n1); //运算结果进栈 break; case '-': s1.add(n2-n1); break; case '*': s1.add(n2*n1); break; case '/': s1.add(n2/n1); break; default: break; } }//while结束 s2.add(op); } num=""; } } //最后一个整数由于后面没有符号了,未能进栈,也要进栈 if(!num.isEmpty()){ s1.add(Double.parseDouble(num)); } while (!s2.isEmpty()) { double n1 = s1.pollLast(); double n2 = s1.pollLast(); char yun=s2.pollLast().operator; switch (yun) { case '+': s1.add(n2+n1); //运算结果进栈 break; case '-': s1.add(n2-n1); break; case '*': s1.add(n2*n1); break; case '/': s1.add(n2/n1); break; default: break; } } System.out.println(String.format("%1$.2f", s1.getLast())); } } }