东师附中 1185 栈 中缀转后缀
来源:互联网 发布:淘宝网的营销策略 编辑:程序博客网 时间:2024/04/29 15:38
1185: 栈 中缀转后缀
时间限制: 1 Sec 内存限制: 128 MB提交: 9 解决: 5
[提交][状态][讨论版]
题目描述
请编写程序将一个中缀表达式转换为后缀表达式。
输入
仅一行,是一个中缀表达式。输入的符号中只有这些基本符号“0123456789+-*/()”,并且不会出现形如2*-3的格式,所有数字都是个位数,“/”表示整除运算。
输出
仅一行,是转换后的后缀表达式。数字之间、运算符之间、数字和运算符之间都用一个空格隔开
样例输入
8-(3+2*6)/5+4
样例输出
8 3 2 6 * + 5 / - 4 +
提示
表达式长度<=200
来源
基本的中缀表达式转后缀表达式,代码如下:
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>#include<stack>using namespace std;stack<char > op;queue<char > num;queue<char > last;char ch[210];bool is_op(char a){ if(a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')') return true; else return false;}int fun(char a){ switch(a) { case '+': case '-':return 1;break; case '*': case '/':return 2;break; case '(':return 3;break; default:return 0;break; }}int main(){ int i,len; char c; scanf("%s",ch); len = strlen(ch); for(i = 0;i < len;i++) { if(!is_op(ch[i]))//if a number num.push(ch[i]); else//if a symbol { if(op.empty()) op.push(ch[i]); else { if(ch[i] != ')')//the new one is not a ')' { if(op.top() == '('||fun(op.top()) < fun(ch[i])) op.push(ch[i]); else { while(!num.empty()) { last.push(num.front()); num.pop(); } if(!op.empty()) { last.push(op.top()); op.pop(); } --i; } } else//the new one is a ')' { while(!num.empty()) { last.push(num.front()); num.pop(); } while(!op.empty()) { if(op.top() != '(') { c = op.top(); last.push(op.top()); op.pop(); } else { op.pop(); if(!op.empty()) { if(op.top() != '('&&fun(op.top()) > fun(c)) { last.push(op.top()); op.pop(); break; } else break; } } } } } } } while(!num.empty()) { last.push(num.front()); num.pop(); } while(!op.empty()) { last.push(op.top()); op.pop(); } while(last.size()>=2) { cout<<last.front()<<" "; last.pop(); } cout<<last.front()<<endl; last.pop(); return 0;}
0 0
- 东师附中 1185 栈 中缀转后缀
- 中缀转后缀 栈
- 栈-中缀转后缀及后缀计算
- 中缀转后缀&后缀求值
- 中缀转后缀表达式(栈实现)
- 数据结构栈之中缀表达式转后缀
- 栈的应用->中缀转后缀
- 中缀转后缀(利用栈)
- 栈-中缀式转后缀式
- 【栈/中缀转后缀】HDU1237简单计算器
- c 中缀转后缀
- 中缀转后缀
- 中缀转后缀表达式
- 中缀转后缀
- 中缀转后缀
- 中缀转后缀求值
- 表达式中缀转后缀
- Nyoj467【中缀转后缀】
- Java 10进制转2、8、16进制转换 / 2、8、16进制转10进制转换
- java 分布式
- jar文件导出及Linux平台下运行
- #算法排序(Java版)#冒泡排序、选择排序
- 自定义安卓控件
- 东师附中 1185 栈 中缀转后缀
- Exchange 03、07、10、13服务器的退信失败原因
- maven打包 \Application.java:[8,15] 警告:sun.misc.Signal 是 Sun 的专用 API,可能会在未来版本中删除
- unrecognized selector sent to class
- 网络编程中的完成端口
- typeof(undefined) == undefined 成立吗?
- PHP 类的自动加载详细介绍 leyangjun
- [Tab选项卡菜单(一)]位于顶部的tab选项卡菜单
- Jedis2.5.1编译时遇到的问题