codevs5164 逆波兰表达式
2024-10-12 16:09:13
题目描述 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 = ;
ll read(){
ll x=,f=;
char ch=getchar();
while(!(ch>=''&&ch<='')){if(ch=='-')f=-;ch=getchar();};
while(ch>=''&&ch<=''){x=x*+(ch-'');ch=getchar();};
return x*f;
}
stack<double> aa,bb;
stack<char> cc;
char a[],t,op;
double ans;
double get_f(){
int pos = ;
char tmp = a[];
while((tmp>=''&&tmp<='') || 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>=''&&t<=''){
memset(a,,sizeof(a));
a[] = t;
numa = get_f();
}else{
numa = cal(t);
}
t = get_in();
if(t>=''&&t<=''){
memset(a,,sizeof(a));
a[] = 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 ;
}
最新文章
- 4、面向对象以及winform的简单运用(继承与多态、命名空间与类库)
- 解北大OJ1088滑雪问题的记录
- (step 8.2.8)hdu 1079(Calendar Game)
- Oracle Sql优化之日期的处理
- KMP算法求next数组
- C++ Primer 有感(管理类的指针成员)
- 如何高效地写CSS--等以后有空多加总结一下
- flask报错:werkzeug.routing.BuildError: Could not build url for endpoint &#39;index&#39;. Did you mean &#39;single&#39; instead?
- sometimes we should use ";disable fork"; instead of ";disable block_name";
- U3D学习005——输入操作
- GitHub 新手教程 四,Git GUI 新手教程(1),OpenSSH Public Key
- css实现自适应正方形
- Oracle用户权限及死锁
- 关于localStorage 应用总结
- 《Android源码设计模式》--Builder模式
- 全是干货!UI设计的30条黄金准则!
- RxSwift 系列(四)
- python模块之XlsxWriter 详解
- SVN无法Cleanup
- onload函数和自执行函数的区别(jquery API网址:http://jquery.cuishifeng.cn/)
热门文章
- Discuz! X2.5 /source/class/helper/helper_seo.php Remote Code Execution Vul
- js调用刷新
- Unity 联网小测试(WWW)
- Scala高阶函数示例
- oracle创建存储过程
- CentOS 下安装
- Mac &; XCode 使用技巧总结
- Setting up Django and your web server with uWSGI and nginx
- BZOJ2960: 跨平面
- redis auth php操作