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? 如果有的话,输出字典序最小的路径 ...
随机推荐
- 深入理解.net多线程(一)
多线程开发要理解的几个基本概念:进程.应用程序域.对象上下文 进程:进程是一个操作系统级别的概念,用来描述一组资源和程序运行所必需的内存分配.简单的理解,可以认为进程就是一个运行程序.对于每一个被加载 ...
- json optString getString
optString 和 getString 区别. optString 当接收到的为空时候 不会报错
- 2.1Android界面View及ViewGroup(转)
2.1Android界面View及ViewGroup 2.1.0 View及ViewGroup类关系Android View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,V ...
- SDAU课程练习--problemO(1014)
题目描述 Before bridges were common, ferries were used to transport cars across rivers. River ferries, u ...
- java fx example
http://www.java2s.com/Tutorials/Java/JavaFX/1500__JavaFX_WebEngine.htm
- Eclipse开发工具的使用之-使用Eclipse的Debug调试Android程序
1.设置断点,双击Eclipse编辑界面的边界,或者右击编辑界面的边界,快捷键Ctrl+Shift+B. 2.F11键开始调试程序,程序安装到手机之后,并不会自动运行,需要你手动运行到断点处. 3.运 ...
- @Resource @Autowired 区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- JS——基础知识(二)
1.变量提升问题 <script> var num=10; fun(); function fun(){ console.log(num); var num=20; } </scri ...
- The 2014 ACMICPC Asia Regional Beijing Online
[A]极角排序+树状数组 [B]计算几何,凸包(队友已出) [C]-_-///不懂 [D]数论,概率密度 [E]图的连通性+Floyed传递闭包+bitset [F]贪心 [G]签到题 [H]区间维护 ...
- .NET的对象映射工具AutoMapper使用笔记
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...