import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); String line; while (scan.hasNextLine()) { line = scan.nextLine().trim(); // please write your code here System.out.println(Main.calculate(line)); } } private static String calculate(String s){ LinkedList<Integer> list = new LinkedList(); char [] c = new char[s.length()+1]; for(int i=0;i<c.length-1;i++) c[i] = s.charAt(i); c[c.length-1] = ‘+‘;///为下面运算准备,当前运算符的运算要检测到下一个运算符才运算 int num=0; char pre=‘X‘;///初始化 int temp; boolean flag = false; for(int i=0;i<c.length;i++){ if(c[i]==‘ ‘) continue; if(flag) break; if(c[i]>=‘0‘&&c[i]<=‘9‘) num = num*10+c[i]-‘0‘;///一位位地把数字符转换为数字 else{///,运算符进行运算的时间是在检测到下一个运算符才能运算 switch (pre){///这个运算符是之前的 case ‘+‘: list.push(num); break; case ‘-‘: list.push(num*(-1)); break; case ‘*‘: temp=(list.pop()*num); list.push(temp); break; case ‘/‘: if(num==0) {flag=true;break;}///分母为0 temp=(list.pop()/num); list.push(temp); break; default: list.push(num); break; } pre = c[i];///存下当前的符号,为下一次运算 num=0;///num归0 } } if(flag) return "err"; int sum=0; while(list.size()!=0) sum+=list.pop(); return sum+""; } }