#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);
}
}
把中缀表达式转化为后缀表达式
最新推荐文章于 2020-07-01 16:20:22 发布