中缀表达式转逆波兰式(后缀表达式)求值 C++ Stack
给一个包含小数的中缀表达式 求出它的值
首先转换为后缀表达式然后利用stack求出值
转换规则:
如果字符为'(' push
else if 字符为 ')'
出栈运算符直到遇到‘('
else if 字符为‘+’,’-‘,’*‘,’/‘
{
if 栈为空或者上一个运算符的优先级小于当前运算符
push
else
{
运算符优先级小于等于栈顶运算符的优先级,出栈
然后!将当前运算符入栈!
}
}
代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 1100
#define L 31
#define INF 1000000009
#define eps 0.00000001
/*
1.000+2/4=
((1+2)*5+1)/4=
首先把中缀表达式转换为后缀表达式!(注意点运算符求值)
转换后的结果用一个string vector来表示
然后从前到后求值,pop两个数字 计算结果然后插入到stack中
*/
string str;
vector<string> trans;
stack<char> S;
stack<float> cal;
map<char, int> pri;
void Read()
{
string tmp;
trans.clear();
while (!S.empty())
S.pop();
while (!cal.empty())
cal.pop();
for (int i = ; i < str.size() - ; i++)// 特殊考虑( ) .
{
if (str[i] == '(')
{
if (!tmp.empty())
{
trans.push_back(tmp);
tmp.clear();
}
S.push(str[i]);
}
else if (str[i] == ')')
{
if (!tmp.empty())
{
trans.push_back(tmp);
tmp.clear();
}
while (!S.empty() && S.top() != '(')
{
string ttt = "";
ttt.push_back(S.top());
trans.push_back(ttt);
S.pop();
}
if (!S.empty() && S.top() == '(')
S.pop();
}
else if (str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
if (!tmp.empty())
{
trans.push_back(tmp);
tmp.clear();
}
if (S.empty() || pri[S.top()]<pri[str[i]])
{
S.push(str[i]);
continue;
}
else
{
while (!S.empty() && pri[S.top()] >= pri[str[i]])
{
string ttt = "";
ttt.push_back(S.top());
trans.push_back(ttt);
S.pop();
}
S.push(str[i]);
}
}
else
{
tmp.push_back(str[i]);
}
}
if (!tmp.empty())
{
trans.push_back(tmp);
tmp.clear();
}
while (!S.empty())
{
string ttt = "";
ttt.push_back(S.top());
trans.push_back(ttt);
S.pop();
}
}
float solve()//计算转化出的后缀表达式的值
{
while (!cal.empty())
cal.pop();
for (int i = ; i < trans.size(); i++)
{
if (trans[i] == "+" || trans[i] == "-" || trans[i] == "*" || trans[i] == "/")
{
float a, b;
a = cal.top();
cal.pop();
b = cal.top();
cal.pop();
if (trans[i] == "+")
cal.push(a + b);
else if (trans[i] == "-")
cal.push(b - a);
else if (trans[i] == "*")
cal.push(a * b);
else
cal.push(b / a);
}
else
{
cal.push(atof(trans[i].c_str()));
}
}
return cal.top();
}
int main()
{
int n;
cin >> n;
pri['+'] = pri['-'] = , pri['*'] = pri['/'] = , pri['('] = pri[')'] = -;
while (n--)
{
cin >> str;
Read();
printf("%.2f\n",solve());
}
return ;
}
中缀表达式转逆波兰式(后缀表达式)求值 C++ Stack的更多相关文章
- c# 逆波兰式实现计算器
语文不好,不太会组织语言,希望不要太在意. 如题,先简要介绍一下什么是逆波兰式 通常我们在写数学公式的时候 就是a+b+c这样,这种表达式称为中缀表达式,逆波兰式又称为后缀表达式,例如a+b 后缀 ...
- Java 实现《编译原理》中间代码生成 -逆波兰式生成与计算 - 程序解析
Java 实现<编译原理>中间代码生成 -逆波兰式生成与计算 - 程序解析 编译原理学习笔记 (一)逆波兰式是什么? 逆波兰式(Reverse Polish notation,RPN,或逆 ...
- NYOJ 35 表达式求值(逆波兰式求值)
http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...
- shunting-yard 调度场算法、中缀表达式转逆波兰表达式
中缀表达式 1*(2+3) 这就是一个中缀表达式,运算符在数字之间,计算机处理前缀表达式和后缀表达式比较容易,但处理中缀表达式却不太容易,因此,我们需要使用shunting-yard Algorith ...
- javascript:逆波兰式表示法计算表达式结果
逆波兰式表示法,是由栈做基础的表达式,举个例子: 5 1 2 + 4 * + 3 - 等价于 5 + ((1 + 2) * 4) - 3 原理:依次将5 1 2 压入栈中, 这时遇到了运算符 + ...
- c++实现将表达式转换为逆波兰表达式
https://github.com/Lanying0/lintcode 所属: 数据结构->线性结构->栈 问题: 给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号). ...
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...
- HDU1237 简单的计算器 【堆】+【逆波兰式】
简单的计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- HDU1237 简单计算器 【栈】+【逆波兰式】
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
随机推荐
- @RequestParam 和 @RequestBody 接受的时间格式
这两个接受的时间格式不相同 首先看一下他们的区别 @RequestParam用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容.(Ht ...
- RHEL6.5 设置yum,IP地址,解压缩
系统运维 www.osyunwei.com 温馨提醒:qihang01原创内容©版权所有,转载请注明出处及原文链接 服务器相关设置如下: 操作系统:RHEL 6.5 64位 IP地址:192.168. ...
- Vue项目中如何使用less(添加less依赖)
今天在新工程里添加了一段样式代码代码突然报错了: <!-- Add "scoped" attribute to limit CSS to this component onl ...
- BZOJ 2592 随机化(伪)
思路: 放yousiki大爷题解 http://yousiki.net/index.php/archives/82/ 我写的是随机化 既然gzz证了最终答案的上界是O(N)的 那么我们可以n^2枚举所 ...
- 【BZOJ2595_洛谷4294】[WC2008]游览计划(斯坦纳树_状压DP)
上个月写的题qwq--突然想写篇博客 题目: 洛谷4294 分析: 斯坦纳树模板题. 简单来说,斯坦纳树问题就是给定一张有边权(或点权)的无向图,要求选若干条边使图中一些选定的点连通(可以经过其他点) ...
- android sqlite中判断某个表是否存在
<span style="font-size:18px;">sqlite 中判断某个表是否存在的方法,贴出来供大家参考 /** * 判断某张表是否存在 * @param ...
- Maven+Docker,发布到Registry
1.配置Pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnc ...
- Java&Xml教程(二)使用DOM方式解析XML文件
DOM XML 解析方式是最容易理解的,它將XML文件作为Document对象读取到内存中,然后你可以轻松地遍历不同的元素和节点对象.遍历元素和节点不需要按照顺序进行. DOM解析方式适合尺寸较小的X ...
- 一篇文章告诉你如何使用EF CodeFirst做增删改查
一.修改数据 其实修改涉及的内容挺多的,是相对于其他操作来说比较繁琐.也是本文的重头戏. 虽然都是基础内容,但是也是值得细细品味的. 1.最简单直接的修改数据就是从数据库里检索出数据修改相应的字段即可 ...
- STL之string篇
常用代码整理: #include<iostream> #include<cstdio> #include<cstring> #include<string&g ...