codevs5164 逆波兰表达式

题目描述  Description

逆波兰表达式是一种把运算符前置的算术表达式(又叫前缀表达式),例如普通的表达式2 + 3的逆波兰表示法为+ 2 3。逆波兰表达式的优点是运算符之间不必有优先级关系,也不必用括号改变运算次序,例如(2 + 3) * 4的逆波兰表示法为* + 2 3 4。本题求解逆波兰表达式的值,其中运算符包括+ - * /四个。

输入描述  Input Description

输入为一行,其中运算符和运算数之间都用空格分隔,运算数是浮点数。

输出描述  Output Description

输出为一行,表达式的值。

值应该为浮点数并保留6位小数。

测试数据保证单精度与双精度都可以通过

样例输入  Sample Input

   

* + 11.0 12.0 + 24.0 35.0

 

样例输出  Sample Output

   

1357.000000

 

数据范围及提示  Data Size & Hint

C/C++语言可以使用stdlib.h(C)或cstdlib(C++)里的

double atof( const char *str );

把字符串变为double浮点数

↑这个是函数原型↑

 

pascal的嘛……

我不知道有没有……

所以P的看着办吧

/*
递归计算
*/
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<cstdlib>
#define ll int
#define fo(i,l,r) for(int i = l;i <= r;i++)
#define fd(i,l,r) for(int i = r;i >= l;i--)
using namespace std;
const int maxn = 1050;
ll read(){
    ll x=0,f=1;
    char ch=getchar();
    while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();};
    while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();};
    return x*f;
}
stack<double> aa,bb;
stack<char> cc;
char a[100],t,op;
double ans;
double get_f(){
    int pos = 0;
    char tmp = a[0];
    while((tmp>='0'&&tmp<='9') || tmp == '.'){
        a[pos++] = tmp;
        tmp = getchar();
    }
    return atof(a);
}
inline char get_in(){
    char tmp;
    tmp = getchar();
    while(tmp == ' ') tmp = getchar();
    return tmp;
}
double cal(char col){
    double num,numa,numb;
    t = get_in();
    if(t>='0'&&t<='9'){
        memset(a,0,sizeof(a));
        a[0] = t;
        numa = get_f();
    }else{
        numa = cal(t);
    }
    t = get_in();
    if(t>='0'&&t<='9'){
        memset(a,0,sizeof(a));
        a[0] = t;
        numb = get_f();
    }else{
        numb = cal(t);
    }
    if(col == '+') num = numa + numb;
    if(col == '-') num = numa - numb;
    if(col == '*') num = numa * numb;
    if(col == '/') num = numa / numb;
    return num;
}
int main(){
    freopen("gg.in","r",stdin);
    t = get_in();
    ans = cal(t);
    printf("%.6lf",ans);
    return 0;
} 

 

转载于:https://www.cnblogs.com/hyfer/p/6003733.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值