## Stack实现表达式的转换

1.Stack实现中缀转后缀

void infixToPostFix(char infix[],char s2[],int &top2){
char s1[MaxSize];
int top1=-1;
int i=0;
while(infix[i] !='\0'){
if('0'<= infix[i] && infix[i]<='9'){
s2[++top2]=infix[i];
++i;
}else if(infix[i] == '('){
s1[++top1]='(';
++i;
}else if(infix[i] == '+'||infix[i]=='-'||infix[i]=='*'||infix[i]=='/'){
if(top1 == -1||s1[top1]=='('||getPriority(infix[i])>getPriority(s1[top1])){
s1[++top1]=infix[i];
++i;
}else{
s2[++top2]=s1[top1--];
}
}else if(infix[i] == ')'){
while(s1[top1] !='(')
{
s2[++top2]=s1[top1--];
}
--top1;
++i;
}
while(top1 != -1){
s2[++top2]=s1[top1--];
}
}
}

2.Stack实现中缀转前缀

void infixToPreFix(char infix[],int len,char s2[],int &top2){
char s1[MaxSize];
int top1=-1;
int i=len-1;
while(i>=0){
if('0'<=infix[i] && infix[i]<='9'){
s2[++top2]=infix[i];
i--;
}else if(infix[i] == ')'){
s1[++top1]=')';
--i;
}else if(infix[i]=='+'||infix[i]=='-'||infix[i]=='*'||infix[i]=='/'){
if(top1==-1||s1[top1]==')'||getPriority(infix[i])>=s1[top1]){
s1[++top1]=infix[i];
--i;
}else{
s2[++top2]=s1[top1--];
}
}else if(infix[i] == '('){
while(s1[top1] !=')'){
s2[++top2]=s1[top1--];
}
--top1;
--i;
}
}
while(top1 !=-1){
s2[++top2]=s1[top1--];
}
}

posted on 2020-06-24 10:09  二进制dd  阅读(165)  评论(0编辑  收藏  举报