#13943: AC_HAHA


terry90209@gmail.com (Mini)

學校 : 不指定學校
編號 : 78019
來源 : [219.71.215.231]
最後登入時間 :
2018-04-03 22:44:44
d016. 後序運算法 | From: [219.71.215.231] | 發表日期 : 2018-05-19 11:40

public class D016{
public static void main(String[] args){
java.util.Scanner sc = new java.util.Scanner(System.in);
while(sc.hasNext()){
Test ts = new Test(sc.nextLine());
System.out.println(ts.calcu());
}
}
}

class Test{
private String[] str;

public Test(String s){ str = s.split(" ");}

public int calcu(){
Digit_Stack ds = new Digit_Stack();

for(int i = 0; i < str.length; i++){

if(ds.size() >= 2){
int n1 = ds.pop();
int n2 = ds.pop();

if(str[i].equals("+")) ds.push(n2 + n1);
else if(str[i].equals("-")) ds.push(n2 - n1);
else if(str[i].equals("*")) ds.push(n2 * n1);
else if(str[i].equals("/")) ds.push(n2 / n1);
else if(str[i].equals("%")) ds.push(n2 % n1);
else{
ds.push(n2);
ds.push(n1);
ds.push(Integer.parseInt(str[i]));
}
}
else
if(!str[i].equals("+") && !str[i].equals("-") && !str[i].equals("*") &&
!str[i].equals("/") && !str[i].equals("%"))
ds.push(Integer.parseInt(str[i]));
}
return ds.pop();
}
}

class Digit_Stack{
private Node first;
private int n;

private class Node{
int num;
Node next;
}

public int pop(){
int ans = first.num;
first = first.next;
n--;
return ans;
}

public void push(int a){
Node oldfirst = first;
first = new Node();
first.next = oldfirst;
first.num = a;
n++;
}

public int size(){ return n;}
public boolean isEmpty(){ return first == null;}
}

 
ZeroJudge Forum