用C语言将表达式转化成后缀表达式
时间: 2024-05-26 11:10:00 浏览: 15
Sure, I can help you with that! Basically, to convert an infix expression to postfix expression using C language, we can use the stack data structure. Here's the basic algorithm:
1. Create a stack and push a left parenthesis '(' onto it.
2. Scan the infix expression from left to right.
3. If the current token is an operand, append it to the postfix expression.
4. If the current token is a left parenthesis, push it onto the stack.
5. If the current token is a right parenthesis, pop operators from the stack and append them to the postfix expression until a left parenthesis is found. Discard the left parenthesis.
6. If the current token is an operator, pop operators from the stack and append them to the postfix expression until an operator with lower precedence or a left parenthesis is found. Push the current operator onto the stack.
7. After the infix expression is scanned, pop any remaining operators from the stack and append them to the postfix expression.
And here's an example C program that implements this algorithm:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_EXPR_LEN 100
#define MAX_STACK_SIZE 100
int is_operator(char c) {
return (c == '+' || c == '-' || c == '*' || c == '/');
}
int get_precedence(char c) {
switch (c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
void infix_to_postfix(char *infix_expr, char *postfix_expr) {
int len = strlen(infix_expr);
char *stack = malloc(MAX_STACK_SIZE * sizeof(char));
int top = 0;
int i, j;
stack[top] = '(';
j = 0;
for (i = 0; i < len; i++) {
char c = infix_expr[i];
if (c == ' ') {
continue;
} else if (isdigit(c)) {
postfix_expr[j++] = c;
} else if (is_operator(c)) {
while (get_precedence(c) <= get_precedence(stack[top]) && stack[top] != '(') {
postfix_expr[j++] = stack[top--];
}
stack[++top] = c;
} else if (c == '(') {
stack[++top] = c;
} else if (c == ')') {
while (stack[top] != '(') {
postfix_expr[j++] = stack[top--];
}
top--;
}
}
while (top > 0) {
postfix_expr[j++] = stack[top--];
}
postfix_expr[j] = '\0';
free(stack);
}
int main() {
char infix_expr[MAX_EXPR_LEN];
printf("Please enter the infix expression: ");
fgets(infix_expr, MAX_EXPR_LEN, stdin);
infix_to_postfix(infix_expr, infix_expr);
printf("The postfix expression is: %s\n", infix_expr);
return 0;
}
And here's an example of how to use the program:
Please enter the infix expression: (1+2)*3-4/5
The postfix expression is: 12+3*45/-
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)