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的更多相关文章

  1. [LeetCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  2. 【leetcode】Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...

  3. leetcode150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  4. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  5. Leetcode Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. 11. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. LeetCode OJ 150. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  10. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. Spring Cloud Zuul实现动态路由

    1.添加依赖 2.启动类上添加注解 3.配置文件 zuul.ignored-services配置需要忽略的服务,多个用逗号分隔 注释zuul.ignored-services 前: 注释zuul.ig ...

  2. 【OpenCV-python】CV2和PIL按box信息实现图像裁剪

    # 用cv2实现裁剪 import cv2 import os img = cv2.imread("./test_and_verification/1406170100001.jpg&quo ...

  3. Nginx 配置跨域权限

    今天设置静态资源服务器时发现 Font from origin 'http://start.fbzl.org' has been blocked from loading by Cross-Origi ...

  4. 在Spark shell中基于Alluxio进行wordcount交互式分析

    Spark是一个分布式内存计算框架,可部署在YARN或者MESOS管理的分布式系统中(Fully Distributed),也可以以Pseudo Distributed方式部署在单个机器上面,还可以以 ...

  5. css样式查找遇到的问题汇总

    利用css简单排除元素的第一个子元素 例如:排除表格的第一行 /*除了表格的第一行其他都显示为红色*/ table tr+tr{ background-color:red;/*除了表格的第一行其他都显 ...

  6. 部署项目到远程tomcat的413 Request Entity Too Large报错处理

    当项目jar包过多时,部署项目会报错而错误原因很清楚了,文件太大了. 因为用了nginx代理,而nginx默认文件大小有限,所以需要设置nginx上传文件大小限制 client_max_body_si ...

  7. BNU29140——Taiko taiko——————【概率题、规律题】

    Taiko taiko Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class ...

  8. django 将表数据通过API展示到页面上(转)

    需求: 我在learn这个app下创建了两张表,其中一个表为user,我希望通过API,在页面是展示这些数据,当用户访问指定的url时,将表中所有对象展示到页面上. 先看learn/models.py ...

  9. 【Linux】time+dd测试硬盘读写速度

    dd 是 Linux/UNIX 下的一个非常有用的命令,作用是用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. dd 命令通用语法格式如下: dd if=path/to/input_file ...

  10. ThreadPoolExecutor实现原理

    转载:https://blog.csdn.net/yanyan19880509/article/details/52718497 前言 做java开发的,一般都避免不了要面对java线程池技术,像to ...