洛谷P1449 后缀表达式 栈 模拟 字符串

洛谷P1449 后缀表达式

栈 模拟 字符串
栈模拟一下 碰到 . 如果输入的是数字就把数字放进栈中

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <iostream>
 9 using namespace std ; 
10 
11 char s[1011] ; 
12 int l,top,x ; 
13 int st[1011] ; 
14 
15 int main() 
16 {
17     scanf("%s",s+1) ; 
18     l = strlen(s+1) ; 
19     x = -1 ; 
20     for(int i=1;i<=l;i++) 
21     {
22         if(s[ i ]=='.') {
23             if(x!=-1) st[++top] = x ; 
24             x = -1 ; 
25             continue ;
26         }
27         if(s[ i ]>='0'&&s[ i ]<='9') 
28         {
29             if(x==-1) x = 0 ; 
30             x = x*10+s[ i ]-48 ; 
31         } 
32         if(s[ i ]=='+') top-- ,st[top] = st[top] + st[top+1] ; 
33         if(s[ i ]=='-') top-- ,st[top] = st[top] - st[top+1] ; 
34         if(s[ i ]=='*') top-- ,st[top] = st[top] * st[top+1] ; 
35         if(s[ i ]=='/') top-- ,st[top] = st[top] / st[top+1] ; 
36         
37     }
38     printf("%d\n",st[top] ) ; 
39     
40     return 0 ; 
41 }

 

posted @ 2017-06-11 15:34  third2333  阅读(230)  评论(0编辑  收藏  举报