spoj ONP - Transform the Expression 中缀转后缀
题目链接
将中缀表达式转化为后缀表达式。
数字的话直接放到答案的字符串里。
如果是左括号就进栈, 右括号就让栈里的符号都出来直到第一个左括号。
否则的话比较当前符号的优先级和栈顶符号的优先级。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
map <char, int> sign;
vector <char> ans;
void solve() {
ans.clear();
stack <char> st;
string s;
cin>>s;
int last = 6;
for(int i = 0; i < s.size(); i++) {
char c = s[i];
if(isalnum(c)) {
ans.pb(c);
} else {
if(c == '(') {
st.push(c);
last = 6;
} else if(c == ')') {
char tmp;
tmp = st.top();
st.pop();
do {
ans.pb(tmp);
tmp = st.top();
st.pop();
} while (tmp != '(');
last = 6;
} else {
if(sign[c]>=last) {
ans.pb(c);
} else {
st.push(c);
last = sign[c];
}
}
}
}
for(auto &i: ans) {
cout<<i;
}
cout<<endl;
}
void init() {
sign['+'] = 1;
sign['-'] = 2;
sign['*'] = 3;
sign['/'] = 4;
sign['^'] = 5;
}
int main()
{
init();
int t;
cin>>t;
while(t--) {
solve();
}
return 0;
}
spoj ONP - Transform the Expression 中缀转后缀的更多相关文章
- SPOJ #4 Transform the Expression
Not hard to know it is simply transform from in-order to post-order.My first idea is to build a tree ...
- C语言 中缀转后缀
给定字符串型的算术表达式,实现中缀转后缀并运算得出结果: #ifndef STACK_H_INCLUDED #define STACK_H_INCLUDED #include <stdio.h& ...
- 数据结构之栈—强大的四则复杂运算计算器(超过windows自带的科学计算器)【中缀转后缀表达式】
比windows自带计算器还强的四则复杂运算计算器! 实测随机打出两组复杂算式:-7.5 * 6 / ( -2 + ( -6.5 - -5.22 ) )与7.5+-3*8/(7+2) windows ...
- 栈的应用1——超级计算器(中缀与后缀表达式)C语言
这里要学的程序主要用来实现一个功能——输入表达式输出结果,也就是一个计算器.效果如下: 这个程序主要有两个步骤:1.把中缀表达式转换为后缀表达式:2.计算后缀表达式的结果. 首先先明白几个问题: 1. ...
- Java数据结构和算法(六)——前缀、中缀、后缀表达式
前面我们介绍了三种数据结构,第一种数组主要用作数据存储,但是后面的两种栈和队列我们说主要作为程序功能实现的辅助工具,其中在介绍栈时我们知道栈可以用来做单词逆序,匹配关键字符等等,那它还有别的什么功能吗 ...
- 【SPOJ】Longest Common Substring II (后缀自动机)
[SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...
- C++ 中缀转后缀表达式并求值
//中缀转后缀 #include<iostream> #include<stack> using namespace std; int prio(char x){ ; ; ; ...
- Java数据结构和算法(六):前缀、中缀、后缀表达式
前面我们介绍了三种数据结构,第一种数组主要用作数据存储,但是后面的两种栈和队列我们说主要作为程序功能实现的辅助工具,其中在介绍栈时我们知道栈可以用来做单词逆序,匹配关键字符等等,那它还有别的什么功能吗 ...
- java四则运算----前缀、中缀、后缀表达式
接到一个新需求,需要实现可配置公式,然后按公式实现四则运算. 刚拿到需求,第一反应就是用正则匹配‘(’,‘)’,‘+’,‘-’,‘*’,‘/’,来实现四则运算,感觉不复杂. 然后开始coding.发现 ...
随机推荐
- Android环境开发搭建
今天第一次接触安卓,从开发环境的配置到程序的运行,足足搞了一天,也没有整出来. 1.安装JDK 在JDK官网上下载了最新的JDK,安装成功后进行环境的配置.JAVA_HOME:C:\Program F ...
- (七)Android中AIDL的应用与理解
一.跨应用启动Service Intent serviceIntent=new Intent();serviceIntent.setComponent(new ComponentName(" ...
- Oracle触发器trigger3利用时间限制用户输入
--触发器的应用限制用户写入 --具体功能:在写入一个表之前,限制必须要在周一到周5和工作时间8:00~18:00 create or replace trigger tri3 before inse ...
- 解决Qt4.8.6+VS2010运行程序提示 FTH: (6512): *** Fault tolerant heap shim applied to current process. This is usually due to previous crashes
这个问题偶尔碰到两次,现在又遇上了,解决办法如下: 打开注册表,设置HKLM\Software\Microsoft\FTH\Enabled 为0 打开CMD,运行Rundll32.exe fthsvc ...
- Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII
一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...
- Vijos 1121 马拦过河卒
首先要看清题目,卒只能向右或者向下走.而不是四周转.这样的话就无解了. 定义f[i][j],表示走到(i,j)这个点时的总步数.这样就写出了一个递推公式f[i][j]=f[i-1]+f[i][j-1] ...
- jquery读取本地文件
<html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...
- C语言程序设计(翁恺)--第三周课件中的三个遗留点
刚刚写完第二周遗留点,下面写第三周的 第三周:判断 1.if和else后面也可以没有{}而是一条语句.如果if后不带{},但是后面跟了两条语句,并且后面还有else语句,那么程序会怎么执行? 在Dev ...
- Windows 下 Django/python 开发环境配置
1.安装 Aptana/Eclipse Aptana是在eclipse上二次开发的一个开源的集成开发环境,内置python编译器 http://www.aptana.com/ 2. 安装python ...
- Delphi下重载窗体CreateParams翻转关闭按钮
type TForm1 = class(TForm) private { Private declarations } public { Public declarations } ...