给一个包含小数的中缀表达式 求出它的值

首先转换为后缀表达式然后利用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的更多相关文章

  1. c# 逆波兰式实现计算器

    语文不好,不太会组织语言,希望不要太在意. 如题,先简要介绍一下什么是逆波兰式  通常我们在写数学公式的时候  就是a+b+c这样,这种表达式称为中缀表达式,逆波兰式又称为后缀表达式,例如a+b 后缀 ...

  2. Java 实现《编译原理》中间代码生成 -逆波兰式生成与计算 - 程序解析

    Java 实现<编译原理>中间代码生成 -逆波兰式生成与计算 - 程序解析 编译原理学习笔记 (一)逆波兰式是什么? 逆波兰式(Reverse Polish notation,RPN,或逆 ...

  3. NYOJ 35 表达式求值(逆波兰式求值)

    http://acm.nyist.net/JudgeOnline/problemset.php?typeid=4 NYOJ 35 表达式求值(逆波兰式求值) 逆波兰式式也称后缀表达式. 一般的表达式求 ...

  4. shunting-yard 调度场算法、中缀表达式转逆波兰表达式

    中缀表达式 1*(2+3) 这就是一个中缀表达式,运算符在数字之间,计算机处理前缀表达式和后缀表达式比较容易,但处理中缀表达式却不太容易,因此,我们需要使用shunting-yard Algorith ...

  5. javascript:逆波兰式表示法计算表达式结果

    逆波兰式表示法,是由栈做基础的表达式,举个例子: 5 1 2 + 4 * + 3 -  等价于   5 + ((1 + 2) * 4) - 3 原理:依次将5 1 2 压入栈中, 这时遇到了运算符 + ...

  6. c++实现将表达式转换为逆波兰表达式

    https://github.com/Lanying0/lintcode 所属: 数据结构->线性结构->栈 问题: 给定一个表达式字符串数组,返回该表达式的逆波兰表达式(即去掉括号). ...

  7. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  8. HDU1237 简单的计算器 【堆】+【逆波兰式】

    简单的计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. HDU1237 简单计算器 【栈】+【逆波兰式】

    简单计算器 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

随机推荐

  1. jsp jquery js 的基本路径获取

    引子:js中需要当前页面的基础路径,获取不到request,可以通过如下方法来解决!   1.jsp基础路径,在jsp头部加上,获取基础路径http://localhost:8080/project/ ...

  2. sql数据库CHECKDB时报x个分配错误和x个一致性错误

    --1.在SQL查询分析器中执行以下语句:(注以下所用的POS为数据库名称,请用户手工改为自己的数据库名) use pos dbcc checkdb --2.查看查询结果,有很多红色字体显示,最后结果 ...

  3. struts2什么情况用#和EL表达示

    1:struts2标签使用中,什么时候用#,什么时候可以不用# 值栈中的对象的不使用#,非值栈中的对象使用#如果不理解值栈的作用,简单点理解:当前action,或者处于action链中的action所 ...

  4. thinkphp调试手段

    使用ThinkPHP应该掌握的调试手段经常看到有人问到findAll的返回数据类型是什么之类的问题,以及出错了不知道什么原因的情况,其实还是没有熟悉ThinkPHP内置的调试手段和方法,抛开IDE本身 ...

  5. Java使用Player播放mp3

    大家平时闲了都会听听歌,散散心,于是很多人就问,在Java里边如何播放歌曲呢,唉,别说,在Java里边还真能歌曲,下面我为大家揭晓. 我们都知道Java里边做什么都需要对应的jar包,首先贴上mave ...

  6. ACM_出题人这样不好吧

    出题人这样不好吧 Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为编协的第一次月赛,肯定是要有防AK(ALL KILL)的题目 ...

  7. MySql(二):常见的那些个约束

    今天总结一下mysql当中的常见约束吧! 那什么是约束呢?通俗点讲,约束就是限定指定字段的存放规则! ● 主键约束(Primary Key) ● 外键约束(Foreign Key) ● 非空约束(No ...

  8. JS——tab函数封装

    1.为li标签添加index属性,这个属性正好就是span标签数组的index值 2.函数封装适合页面有多个tab切换,需要注意的在获取的li标签和span标签对象时,必须将对应div对象作为参数传入 ...

  9. PHP递归复制文件夹以及传输文件夹到其他服务器。

    项目中需要复制整个文件夹,有时候还需要将整个文件夹传输到远程服务器. 这里就要递归遍历整个文件夹了,想看递归遍历文件夹的代码. function deepScanDir($dir) { $fileAr ...

  10. 170925_2 Python socket 创建UDP的服务器端和客户端

    [python版本]3.6 UDP服务器端: from socket import * from time import ctime host = '' port = 21567 buf_size = ...