UVa 727 - Equation
题目大意:给一个中缀表达式,转换成后缀表达式。
这类题一直不太会,让我想就是建一棵表达式树,然后后续遍历算了,可是建树的过程实在太麻烦了。今天才看到有中缀表达式转换成后缀表达式的算法,可以用栈进行实现,我现在才知道...
算法如下:
这里假定操作数均为一位数字,操作符只有(、)、+、-、*和/,结果保存到postfix数组里,还要用到一个栈来保存运算符。
从左向右扫描表达式,如果当前字符为:
(1)数字,将该数字添加到postfix末尾。
(2)(,将(压入栈中。
(3)),如果当前栈顶元算不是(,将栈中的运算符逐个弹出并追加到postfix末尾,知道栈顶为(,弹出但不追加。
(4)+、-、*、/。这里假定(优先级为0,+、-优先级为1,*和/优先级为2。如果栈为空 或者 当前字符的优先级高于栈顶元素的优先级时,将当前字符压栈;否则将栈中大于等于当前字符优先级的元素弹出并追加到postfix末尾,然后将当前字符压栈。
当扫描完成后,如果栈非空,逐个弹出栈中元素并追加到postfix尾部。
#include <cstdio>
#include <cctype>
#include <stack>
using namespace std; int priority(char ch)
{
if (ch == '+' || ch == '-') return ;
else if (ch == '*' || ch == '/') return ;
return ;
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
getchar();
char tmp[], infix[], postfix[];
gets(tmp);
while (T--)
{
int n = ;
while (gets(tmp))
{
if (tmp[] == '\0') break;
infix[n++] = tmp[];
}
infix[n] = ;
stack<char> op;
int k = ;
for (int i = ; i < n; i++)
{
if (isdigit(infix[i])) postfix[k++] = infix[i];
else if (infix[i] == '(') op.push('(');
else if (infix[i] == ')')
{
while (op.top() != '(')
{
postfix[k++] = op.top();
op.pop();
}
op.pop();
}
else
{
if (op.empty() || priority(infix[i]) > priority(op.top())) op.push(infix[i]);
else
{
while (!op.empty() && priority(op.top()) >= priority(infix[i]))
{
postfix[k++] = op.top();
op.pop();
}
op.push(infix[i]);
}
}
}
while (!op.empty())
{
postfix[k++] = op.top();
op.pop();
}
postfix[k] = ;
printf("%s\n", postfix);
if (T) printf("\n");
}
return ;
}
UVa 727 - Equation的更多相关文章
- UVA 1661 Equation (后缀表达式,表达式树,模拟,实现)
题意:给出一个后缀表达式f(x),最多出现一次x,解方程f(x) = 0. 读取的时候用一个栈保存之前的结点,可以得到一颗二叉树,标记出现'X'的路径,先把没有出现'X'的子树算完,由于读取建树的时候 ...
- UVa 10006 - Carmichael Numbers
UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...
- Uva 01124, POJ 3062 Celebrity jeopardy
It's hard to construct a problem that's so easy that everyone will get it, yet still difficult enoug ...
- UVa 10341 - Solve It【经典二分,单调性求解】
原题: Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where ...
- UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 10428 - The Roots(牛顿迭代法)
UVA 10428 - The Roots option=com_onlinejudge&Itemid=8&page=show_problem&category=494& ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
随机推荐
- AJAX(XMLHttpRequest)进行跨域请求方法详解(二)
注意:以下代码请在Firefox 3.5.Chrome 3.0.Safari 4之后的版本中进行测试.IE8的实现方法与其他浏览不同. 2,预检请求 预检请求首先需要向另外一个域名的资源发送一个 HT ...
- Mysql 随机查询数据
SELECT * FROM tablename ORDER BY RAND() LIMIT 10
- 转载 C++学习第9篇---类和类的封装
http://blog.csdn.net/zuheyawen/article/details/7324340
- android 线程池
http://blog.csdn.net/wangwenhui11/article/details/6760474 http://blog.csdn.net/cutesource/article/de ...
- hihocoder网络流一·Ford-Fulkerson算法
网络流一·Ford-Fulkerson算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho住在P市,P市是一个很大很大的城市,所以也面临着一个大城市都会遇 ...
- android脚步---将layout和其输出一起保存成图片
public void convertViewToBitmap(View view) { //View view = getLayoutInflater().inflate(R.layout.test ...
- SMB/CIFS协议解析
1.SMB协议与CIFS协议的区别 139端口是一种TCP端口,该端口在通过网上邻居访问局域网中的共享文件或共享打印机时就能发挥作用.445端口也是一种TCP端口,该端口在 Windows 2 ...
- (转)java 排序算法
排序算法汇总(java实现,附源代码) 整理系统的时候发现了原来写的各种算法的总结,看了一下,大吃一惊,那时候的我还如此用心,具体的算法,有的已经模糊甚至忘记了,看的时候就把内容整理出来,顺便在熟 ...
- over-float清除浮动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 解决Only the original thread that created a view hierarchy can touch its views
这种异常出现在子线程中处理UI操作产生的异常,将UI操作放在主线程中就OK了