题目大意:给一个中缀表达式,转换成后缀表达式。

  这类题一直不太会,让我想就是建一棵表达式树,然后后续遍历算了,可是建树的过程实在太麻烦了。今天才看到有中缀表达式转换成后缀表达式的算法,可以用栈进行实现,我现在才知道...

算法如下:

  这里假定操作数均为一位数字,操作符只有(、)、+、-、*和/,结果保存到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的更多相关文章

  1. UVA 1661 Equation (后缀表达式,表达式树,模拟,实现)

    题意:给出一个后缀表达式f(x),最多出现一次x,解方程f(x) = 0. 读取的时候用一个栈保存之前的结点,可以得到一颗二叉树,标记出现'X'的路径,先把没有出现'X'的子树算完,由于读取建树的时候 ...

  2. UVa 10006 - Carmichael Numbers

    UVa 10006 - Carmichael Numbers An important topic nowadays in computer science is cryptography. Some ...

  3. 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 ...

  4. UVa 10341 - Solve It【经典二分,单调性求解】

    原题: Solve the equation:         p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0         where  ...

  5. UVa 10905 - Children's Game 排序,题目没有说输入是int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. UVA 10428 - The Roots(牛顿迭代法)

    UVA 10428 - The Roots option=com_onlinejudge&Itemid=8&page=show_problem&category=494& ...

  7. UVA 1412 Fund Management (预处理+状压dp)

    状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...

  8. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  9. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

随机推荐

  1. DHCP详细工作过程(转)

    DHCP客户端通过和DHCP服务器的交互通讯以获得IP地址租约.为了从DHCP服务器获得一个IP地址,在标准情况下DHCP客户端和DHCP服务器之间会进行四次通讯.DHCP协议通讯使用端口UDP 67 ...

  2. PAT (Advanced Level) 1111. Online Map (30)

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  3. YII2 随笔 视图最佳实践

    yii\base\Controller::render(): 渲染一个 视图名 并使用一个 布局 返回到渲染结果. yii\base\Controller::renderPartial(): 渲染一个 ...

  4. Django之路:QuerySet API,后台和表单

    一.Django QuerySet API Django模型中我们学习了一些基本的创建和查询.这里专门讲以下数据库接口相关的接口(QuerySet API),当然你也可以选择暂时跳过这节.如果以后用到 ...

  5. BootStrap详解之(一)

    一.BootStrap简介 BootStrap是一个用来构建网站前段框架的一个插件.无论你是想构建应用程序.博客还是CMS网站,Bootstrap都特别的使用,只要你想得到,它就能行.Bootstra ...

  6. Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? \Android Studio快捷键之代码提示

    问:Android Studio中有没有类似于Eclipse中的ctrl+2+L的快捷键? 答:有,as中的快捷键是Ctrl+Alt+V AndroidStudio和Eclipse常用快捷键对比 功能 ...

  7. shell输出不换行符合换行符

    输出不换行符 例如 echo "Hello\c" echo " World" //Hello World 输出换行符 echo "username\n ...

  8. Android手机fastboot刷机命令

    先进入fastboot文件所在目录 连接硬件命令 fastboot devices 删除recover.boot,system同理 Fastboot erase recovery 重刷,boot,sy ...

  9. filters

    http://www.cloudera.com/content/cloudera/en/documentation/core/latest/topics/admin_hbase_filtering.h ...

  10. 黄聪:基于Asp.net的CMS系统We7架设实验(环境WIN7,SQL2005,.NET3.5)(初学者参考贴)

    http://www.cnblogs.com/huangcong/archive/2010/03/30/1700348.html