数据结构实验之栈二:一般算术表达式转换成后缀式
2016-07-29 20:45
344 查看
#include<iostream> #include<algorithm> using namespace std; const int maxn=10000+10; typedef struct { char *data; int top; int sizestack; }STACK; void initstack(STACK &S) { S.data=new char [maxn]; S.top=-1; S.sizestack=maxn; } void push(STACK &S, char s) { S.data[++S.top]=s; } void pop(STACK &S) { S.top--; } int cmp(char s) { if(s=='+'||s=='-') return 1; if(s=='*'||s=='/') return 2; if(s=='(') return 3; if(s==')') return 4; return 0; } int main() { ios::sync_with_stdio(false); char s; STACK S; initstack(S); while(cin>>s&&s!='#') { if(s>='a'&&s<='z') { cout<<s; } else { if(S.top==-1) { push(S, s); } else if(cmp(s)>=cmp(S.data[S.top])) { if(cmp(s)==4) { while(S.data[S.top]!='(') { cout<<S.data[S.top--]; } pop(S); } else { push(S, s); } } else { if(S.data[S.top]!='(') { cout<<S.data[S.top]; S.data[S.top] = s ; } else push(S, s); } } } while(S.top>=0) { cout<<S.data[S.top]; S.top--; } cout<<endl; return 0; }
相关文章推荐
- sdut oj2134 数据结构实验之栈四:括号匹配
- SDUT 3348 数据结构实验之数组二:稀疏矩阵
- 数据结构之栈进制转换 (sdut oj 1252)
- 数据结构实验之栈二:一般算术表达式转换成后缀式(栈)
- 2119数据结构实验之链表四:有序链表的归并
- 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
- SDUT 3361 数据结构实验之图论四:迷宫探索
- 数据结构实验之二叉树一:树的同构
- 线段树专题#4_蒟蒻训练历程记录_HDU1698_ 延迟标记、区间更新
- HDU 3849 双联通+Trie树+并查集
- 2118数据结构实验之链表三:链表的逆置
- 数据结构实验之栈:行编辑器
- soot基础 -- 相关数据结构SootClass,SootMethod,SootBody,Unit的进一步说明
- hdu 5763 The All-purpose Zero (贪心/数据结构+dp)
- 【Caffe】Blobs数据结构的Python表示
- 线段树专题#3_蒟蒻训练历程记录_HDU1754_I hate It
- 数据结构实验之栈二:一般算术表达式转换成后缀式
- 数据结构实验之栈七:出栈序列判定
- 重温数据结构(二)
- 数据结构学习笔记之Java实现二叉查找树