标签:后缀式
#include<iostream> using namespace std; int youxian(char s) { if(s=='+'||s=='-') return 1; else if(s=='*'||s=='/') return 2; else if(s=='(') return 3; else if(s==')') return 4; } int main() { int top=0; char s,b[110]; while(cin>>s && s!='#') { if(s>='a' && s<='z') cout<<s; else { if(top==0) { b[top++]=s; } else { if(youxian(s)>youxian(b[top-1])) { if(youxian(s)==4) { while(b[top-1]!='(') { cout<<b[--top]; } top--; } else { b[top++]=s; } } else { if(b[top-1]=='(') { b[top++]=s; } else { cout<<b[top-1]; b[top-1]=s; } } } } } while(top!=0) { cout<<b[top-1]; top--; } cout<<endl; return 0; }
SDUT OJ 数据结构实验之栈二:一般算术表达式转换成后缀式
标签:后缀式
原文地址:http://blog.csdn.net/r_misaya/article/details/41733491