import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Stack<Integer> stack = new Stack<>();
int n = sc.nextInt();
for(int i = 0; i < n; i++){
String s = sc.next();
if(s.equals("+")){
stack.add(stack.pop() + stack.pop());
}else if(s.equals("-")){
int a = stack.pop();
int b = stack.pop();
stack.add(b - a);
}else if(s.equals("*")){
stack.add(stack.pop() * stack.pop());
}else if(s.equals("/")){
int a = stack.pop();
int b = stack.pop();
if(a != 0){
stack.add(b / a);
}else{
stack.add(0);
}
}else{
stack.add(Integer.parseInt(s));
}
}
System.out.println(stack.peek());
}
}
}
Java Emacs计算器
最新推荐文章于 2024-08-30 18:40:43 发布