给我用C语言写一段中缀表达式转后缀表达式的代码
时间: 2023-03-15 15:15:14 浏览: 58
答案:#include<stdio.h>
int main(){
char infix[100], postfix[100];
int stk[100];
int i, j, top;
printf("请输入中缀表达式:");
scanf("%s", infix);
top = -1;
j = 0;
for (i = 0; infix[i] != '\0'; i++){
if (infix[i] >= 'a' && infix[i] <= 'z'){
postfix[j] = infix[i];
j++;
}
else if (infix[i] == '('){
stk[++top] = '(';
}
else if (infix[i] == ')'){
while (stk[top] != '('){
postfix[j] = stk[top];
j++;
top--;
}
top--;
}
else{
while (top != -1 && stk[top] != '(' &&
(stk[top] == '*' || stk[top] == '/' ||
stk[top] == '+' || stk[top] == '-')){
postfix[j] = stk[top];
j++;
top--;
}
stk[++top] = infix[i];
}
}
while (top != -1){
postfix[j] = stk[top];
j++;
top--;
}
postfix[j]='\0';
printf("后缀表达式:%s", postfix);
return 0;
}