1. 程式人生 > >Stack,( Aizu - ALDS1_3_A)

Stack,( Aizu - ALDS1_3_A)

pro color iostream 轉化 col log blog tps ostream

題目鏈接 : https://vjudge.net/problem/Aizu-ALDS1_3_A

註 :剛學STL就用STL寫了,STL還是很方便的

 1 #include<iostream>
 2 #include<stack>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     stack<int> S;
 9     int a, b, x;
10     string s;
11     
12     while( cin >> s)                      //emmmm...需要ctrl + Z作為結束標記 
13 { 14 if( s[0] == +){ //棧內存儲數字,符號運算直接讀取 15 a = S.top() ; S.pop() ; 16 b = S.top() ; S.pop() ; 17 S.push(a + b); 18 }else if( s[0] == - ){ 19 b = S.top() ; S.pop() ; 20 a = S.top() ; S.pop() ; 21 S.push(a - b);
22 }else if( s[0] == *){ 23 a = S.top() ; S.pop() ; 24 b = S.top() ;S.pop() ; 25 S.push(a * b); 26 }else{ //如果讀入非字符,就壓入棧中 27 S.push(atoi(s.c_str())); //atoi()將字符型數據轉化為數字,c_str()將string類型轉化為char
28 } 29 } 30 31 cout << S.top() <<endl; 32 33 return 0; 34 }

Stack,( Aizu - ALDS1_3_A)