首页 > 其他 > 详细

[洛谷题解]P1449-后缀表达式

时间:2018-09-27 21:03:26      阅读:343      评论:0      收藏:0      [点我收藏+]

0 前言

这里空空如也

1 题目

技术分享图片

跳转

2 思路

就当不存在

代码有注释,懒得打...

3 代码

#include <string>//string
#include <iostream> 
#include <stack> //栈的头文件 
using namespace std;
char g[10000];
int main()
{
    
    cin>>g;
    int a=0; 
    stack <int> s;
    bool flag=false;
    for(int i=0;;i++)
    {
        if(g[i]==@)break;
        
        if(g[i]<0 || g[i]>9)
        {
            if(g[i]==.)//如果a>0 
               s.push(a);
            else
            {
               if(g[i]==*)
               {
                   int stop=s.top();
                   s.pop();
                   stop=stop*s.top();
                   s.pop();
                   s.push(stop);
               }
               if(g[i]==/)
               {
                   int stop=s.top();
                   s.pop();
                   stop=s.top()/stop;
                   s.pop();
                   s.push(stop);
               }
               if(g[i]==-)
               {
                   int stop=s.top();
                   s.pop();
                   stop=s.top()-stop;
                   s.pop();
                   s.push(stop);
               }
               if(g[i]==+)
               {
                   int stop=s.top();
                   s.pop();
                   stop=stop+s.top();
                   s.pop();
                   s.push(stop);
               }    
            }
               
            a=0;
        }
        else
        {
                a=a*10;
                a=a+g[i]-0;
        }
    }
    if(s.empty())s.push(a);
    cout<<s.top();
}

 

 

-The end-

这里!

-sffy

[洛谷题解]P1449-后缀表达式

原文:https://www.cnblogs.com/sffy/p/9715574.html

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