pat02-线性结构3. 求前缀表达式的值(25)
02-线性结构3. 求前缀表达式的值(25)
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。前缀表达式指二元运算符位于两个运算数之前,例如2+3*(7-4)+8/4的前缀表达式是:+ + 2 * 3 - 7 4 / 8 4。请设计程序计算前缀表达式的结果值。
输入格式说明:
输入在一行内给出不超过30个字符的前缀表达式,只包含+、-、*、\以及运算数,不同对象(运算数、运算符号)之间以空格分隔。
输出格式说明:
输出前缀表达式的运算结果,精确到小数点后1位,或错误信息“ERROR”。
样例输入与输出:
序号 | 输入 | 输出 |
1 |
+ + 2 * 3 - 7 4 / 8 4 |
13.0 |
2 |
/ -25 + * - 2 3 4 / 8 4 |
12.5 |
3 |
/ 5 + * - 2 3 4 / 8 2 |
ERROR |
4 |
+10.23 |
10.2 |
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int main(){
//freopen("D:\\input.txt","r",stdin);
stack<double> fi;
stack<string> input;
string s; //bool can=true;
while(cin>>s){
input.push(s);
}
while(!input.empty()){
s=input.top();
input.pop();
if(s.length()==&&!(s[]>=''&&s[]<='')){
double a=fi.top();
fi.pop();
double b=fi.top();
fi.pop();
switch(s[]){
case'+':{
//cout<<"+: "<<a+b<<endl;
fi.push(a+b);
break;
}
case'-':{
//cout<<"-: "<<a-b<<endl;
fi.push(a-b);
break;
}
case'*':{
//cout<<"*: "<<a*b<<endl;
fi.push(a*b);
break;
}
case'/':{
if(b==){
printf("ERROR\n");
//can=false;
return ;
}
//cout<<"/: "<<a/b<<endl;
fi.push(a/b);
break;
}
default:{
printf("ERROR\n");
return ;
}
}
}else if((s[]>=''&&s[]<='')||(s.length()>&&!(s[]>=''&&s[]<='')&&(s[]>=''&&s[]<=''))){
double cal=;
int i=;
if(s[i]=='+'||s[i]=='-'){
if(s[i]=='-'){
cal=-;
}
i++;
}
double intpart=,decpart=;
while(i<s.length()&&s[i]!='.'){//注意换算
intpart*=;
intpart+=s[i++]-'';
}
i++;
int j;
for(j=s.length()-;j>=i;j--){
decpart+=s[j]-'';
decpart*=0.1;
}
fi.push(cal*(intpart+decpart));
}else{
printf("ERROR\n");
return ;
}
}
//cout<<fi.top()<<endl; printf("%.1lf\n",fi.top());
return ;
}
pat02-线性结构3. 求前缀表达式的值(25)的更多相关文章
- PTA笔记 堆栈模拟队列+求前缀表达式的值
基础实验 3-2.5 堆栈模拟队列 (25 分) 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数: int IsFull(Sta ...
- 【Zhejiang University PATest】02-3. 求前缀表达式的值
算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.前缀表达式指二元运算符位于两个运算数之前,例如2+3*(7-4)+8/4的前缀表达式是:+ + 2 * 3 - 7 4 / 8 4.请设计程序计算 ...
- 3-07. 求前缀表达式的值(25) (ZJU_PAT数学)
题目链接:http://pat.zju.edu.cn/contests/ds/3-07 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.前缀表达式指二元运算符位于两个运算数之前,比如2+3*(7 ...
- 信息竞赛进阶指南--递归法求中缀表达式的值,O(n^2)(模板)
// 递归法求中缀表达式的值,O(n^2) int calc(int l, int r) { // 寻找未被任何括号包含的最后一个加减号 for (int i = r, j = 0; i >= ...
- openjduge 求简单表达式的值
表达式求值 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB 给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值. 输入 输入仅有一行 ...
- K:双栈法求算术表达式的值
相关介绍: 该算法用于求得一个字符串形式的表达式的结果.例如,计算1+1+(3-1)*3-(21-20)/2所得的表达式的值,该算法利用了两个栈来计算表达式的值,为此,称为双栈法,其实现简单且易于理 ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- 求以下表达式的值,写出您想到的一种或几种实现方法: 1-2+3-4+……+m
private static int fun(int m) { ; ; i <= m; i++) { == ) temp = temp + i; else temp = temp - i; } ...
随机推荐
- React + Python 七月小说网 功能设计(二)
概述 在通过对世面上的各种小说网站简单了解之后(PS:好多盗版网站真的好丑哦.),去除花里胡哨的功能,保留实用功能. 初步制定了以下几个功能需求,当然,所有需求功能都是我自己设计.自己评审,大不了到时 ...
- jquery.validate弹窗验证
$(document).ready(function () { //开始验证 $("#form1").validate({ submitHan ...
- 离线安装 python 第三方库
离线安装 python 第三方库 首先你需要在联网的服务器上已经安装了一个第三方库,比如是paramiko,也就是说你已经执行了 pip install paramiko ,小提示: 如果在安 ...
- Invalid App Store Icon. The App Store Icon in the asset catalog in 'xxx.app' can’t be transparent nor contain an alpha channel.
1.向appstore上传应用的时候,报了这样一个错误 ERROR ITMS-90717: "Invalid App Store Icon. The App Store Icon in th ...
- centos7 yum安装配置redis
1.设置Redis的仓库地址 yum install epel-release 2.安装redis yum install redis 修改配置文件,监听所有的IP地址 vim /etc/redis. ...
- 杀死进程命令 kill
一般kill命令和ps命令结合使用, 例:现在想杀死telnet的进程 1.在所有进程中查看telnet命令 ps -ef |grep telnet 2.根据上面命令查到的进程id,如pid 是 xx ...
- Linux 安装python3.7.3 提示已经自动安装了pip和setuptools 可是使用时bash提示没有找到pip
Linux 安装python3.7.3 提示已经自动安装了pip和setuptools 可是使用时bash提示没有找到pip 今天的任务就是找到解决办法 另外就是用布置好python3的路径
- docker记录
# docker run 运行程序 # docker stop (docker kill) 终止容器. (首先应该执行 docker stop!!) #停止所有容器 docker stop $(doc ...
- js 随机数组
生成指定num-start 个数组长度,值为start---num 的随机数组,不包括num这个值 function rand_arr(num,start) { // 验证值 if(!argument ...
- 洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)
题面 洛谷 题解 首先,我们知道求最长公共前缀可以用二分答案+hash来求 因为有修改操作, 考虑将整个字符串的hash值放入splay中 接着就是splay的基本操作了 Code #include& ...