首页 > 其他 > 详细

Stack,( Aizu - ALDS1_3_A)

时间:2018-03-08 00:47:33      阅读:270      评论:0      收藏:0      [点我收藏+]

题目链接 : 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)

原文:https://www.cnblogs.com/Dicer/p/8525690.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!