Reverse Polish notation
Reverse Polish notation is a notation where every operator follows all of its operands. For example, an expression (1+2)*(5+4) in the conventional Polish notation can be represented as 1 2 + 5 4 + * in the Reverse Polish notation. One of advantages of the Reverse Polish notation is that it is parenthesis-free.
Write a program which reads an expression in the Reverse Polish notation and prints the computational result.
An expression in the Reverse Polish notation is calculated using a stack. To evaluate the expression, the program should read symbols in order. If the symbol is an operand, the corresponding value should be pushed into the stack. On the other hand, if the symbols is an operator, the program should pop two elements from the stack, perform the corresponding operations, then push the result in to the stack. The program should repeat this operations.
Input
An expression is given in a line. Two consequtive symbols (operand or operator) are separated by a space character.
You can assume that +, - and * are given as the operator and an operand is a positive integer less than 106
Output
Print the computational result in a line.
Constraints
2 ≤ the number of operands in the expression ≤ 100
1 ≤ the number of operators in the expression ≤ 99
-1 × 109 ≤ values in the stack ≤ 109
Sample Input 1
1 2 +
Sample Output 1
3
Sample Input 2
1 2 + 3 4 - *
Sample Output 2
-3 一开始出错 [Error] void value not ignored as it ought to be 原因是a = s.pop() 使用的一个函数的返回值类型是void,而对它进行了赋值处理; 使用到的C库函数atoi, 注意atoi()里传入的参数(atoi函数原型见下)
标准C库函数
#include <stdlib.h>
原型 : int atoi( const char *str );
功能:将字符串str转换成一个整数并返回结果。参数str 以数字开头,当函数从str
中读到非数字字符则结束转换并将结果返回。
例如:int num = atoi(“1314.012”);
int值为1314
解题思路: 边输入边处理, 输入的字符若是数字, 则入栈, 若是(+, -, *)其中一个, 便从栈中弹出两个元素进行相应的运算(注意运算的顺序, 次栈顶元素 运算符 栈顶元素), 再将运算结果入栈, 依次循环
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
using namespace std; int main()
{
stack<int> s;
int a, b;
char ch[110]; while(scanf("%s", ch) != EOF)
{
if(ch[0] == '+')
{
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(a + b);
}
else if(ch[0] == '-')
{
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(b - a);
}
else if(ch[0] == '*')
{
a = s.top();
s.pop();
b = s.top();
s.pop();
s.push(a * b);
}
else
{
s.push(atoi(ch));
}
} printf("%d\n", s.top()); while(!s.empty())
{
s.pop();
} return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int top, S[1000]; void push(int x)
{
S[++top] = x; // top 加1之后将元素插入top所在位置
} int pop()
{
top --;
return S[top + 1]; // 返回top所指的元素
} int main()
{
int a, b;
top = 0;
char s[100];
while(scanf("%s", s) != EOF)
{
if(s[0] == '+')
{
a = pop();
b = pop();
push(a + b);
}
else if(s[0] == '-')
{
a = pop();
b = pop();
push(b - a);
}
else if(s[0] == '*')
{
a = pop();
b = pop();
push(a * b);
}
else
{
push(atoi(s));
}
} printf("%d\n", pop()); return 0;
}
Reverse Polish notation的更多相关文章
- [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- leetcode150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 11. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode OJ 150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- linux无敌权限之chattr
chattr 修改文件或者目录的文件属性,添加超级权限 -R 递归修改文件机子目录 -V 显示详细修改的内容 + 在原有的基础上追加参数 — 减参数 = 跟新为指定参数 a append 设定改参数后 ...
- (转)Linux基础------Shell数值计算的几种方法
Linux基础------Shell数值计算的几种方法 原文:http://blog.csdn.net/fu_wayne/article/details/21620639 在Linux下总会遇到数值计 ...
- 牛客网Java刷题知识点之HashMap的实现原理、HashMap的存储结构、HashMap在JDK1.6、JDK1.7、JDK1.8之间的差异以及带来的性能影响
不多说,直接上干货! 福利 => 每天都推送 欢迎大家,关注微信扫码并加入我的4个微信公众号: 大数据躺过的坑 Java从入门到架构师 人工智能躺过的坑 ...
- Immutable-不变模式与不变类
不变模式的定义 一个对象在创建之后就不再变化,就是所谓的不变模式(Immutable Pattern): 一般来讲,一个对象要么就是可变对象(Mutable Object),要么就是不变模式(Immu ...
- Java学习第二十四天
1:多线程(理解) (1)JDK5以后的针对线程的锁定操作和释放操作 Lock锁 (2)死锁问题的描述和代码体现 (3)生产者和消费者多线程体现(线程间通信问题) 以学生作为资源来实现的 资源类:St ...
- java XML解析成Map
1.需要解析的文件.xml <?xml version="1.0" encoding="UTF-8"?> <request> <r ...
- MySQL -U防止人为误操作
在很多时候操作数据库的时候,可能领导或DBA登陆了数据库,在执行update和delete时,忘记了加where,可能会导致清空表的悲剧,所以-U的好处就体现了. 1.mysql -U的帮助说明 -U ...
- HTML 提高页面加载速度的方法
(1)减少 HTTP 的请求.(合并资源文件 和 使用图片精灵 : (2)把CSS 放头部,把 JavaScript 放到 body 标签尾部: (3)定义图片的宽和高: (4)定义字符集: (5) ...
- 初入门 HTML
---恢复内容开始--- 1.h标签(标题标签) h1~h62.br标签(换行标签) <br/>3.hr标签(水平线标签) <hr/>4.strong(加粗) em(倾斜)5. ...
- 避免console错误,console兼容
背景:写js代码时写了很多console.log进行日志打印,最后上生产时不想删除日志输出, 但是ie在不打开控制台时,日志输出会导致后续js不执行,所以需要适时屏蔽js日志输出 (IE等不支持con ...