您的位置:首页 > 其它

把中缀表达式转化为后缀表达式

2017-12-13 11:18 239 查看
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define size 100
typedef struct stack{
char a[size];
int top;
}stack;
stack st;
char get[size];
void tran(char *a){
int t=strlen(a),j=0;
for(int i=0;i<t;i++){
if(a[i]<='9'&&a[i]>='0')
get[j++]=a[i];
else if(a[i]=='(')
st.a[++st.top]=a[i];
else if(a[i]==')'){
while(st.a[st.top]!='(')
get[j++]=st.a[st.top--];
st.top--;
}
else if(a[i]=='-'||a[i]=='+')
st.a[++st.top]=a[i];
else {
while(st.a[st.top]!='('&&st.top!=0&&st.a[st.top]!='*'&&st.a[st.top]!='/')
get[j++]=st.a[st.top--];
st.a[++st.top]=a[i];
}
}
while(st.top!=0)
get[j++]=st.a[st.top--];
get[j]='\0';
}
int main(){
char a[100];
while(scanf("%s",a)!=EOF){
tran(a);
printf("%s",get);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  表达式树