中缀表达式a+b*c+(d*e+f)*g,其转换成后缀表达式则为abc*+de*f+g*用栈实现代码c语言
时间: 2023-11-30 11:04:04 浏览: 164
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
char stack[MAX];
int top = -1;
void push(char item) {
if (top >= MAX - 1) {
printf("Stack Overflow\n"); return;
}
stack[++top] = item;
}
char pop() {
if (top < 0) {
printf("Stack Underflow\n");
exit(1);
}
return stack[top--];
}
int is_operator(char symbol) {
if (symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol == '-') {
return 1;
} else {
return 0;
}
}
int precedence(char symbol) {
if (symbol == '^') {
return (3);
} else if (symbol == '*' || symbol == '/') {
return (2);
} else if (symbol == '+' || symbol == '-') {
return (1);
} else {
return (0);
}
}
void infix_to_postfix(char infix[], char postfix[]) {
int i, j;
char item;
char x;
push('(');
strcat(infix, ")");
i = 0;
j = 0;
item = infix[i];
while (item != '\0') {
if (item == '(') {
push(item);
} else if (isdigit(item) || isalpha(item)) {
postfix[j] = item;
j++;
} else if (is_operator(item) == 1) {
x = pop();
while (is_operator(x) == 1 && precedence(x) >= precedence(item)) {
postfix[j] = x;
j++;
x = pop();
}
push(x);
push(item);
} else if (item == ')') {
x = pop();
while (x != '(') {
postfix[j] = x;
j++;
x = pop();
}
} else {
printf("Invalid infix Expression.\n");
exit(1);
}
i++;
item = infix[i];
}
if (top > 0) {
printf("Invalid infix Expression.\n");
exit(1);
}
postfix[j] = '\0';
}
int main() {
char infix[MAX], postfix[MAX];
printf("Enter Infix expression : ");
gets(infix);
infix_to_postfix(infix, postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
```
--相关问题--:
1. 什么是中缀表达式和后缀表达式?
2. 除了栈,还有
阅读全文