[栈] 表达式相关问题代码(C语言)-计算中缀、后缀、前缀表达式|中缀转后缀、前缀表达式

原文链接:https://www.yuque.com/cppdev/algo/sgow8x

本文内容:C语言实现并附有相关讲解

  1. 计算中缀表达式
  2. 计算后缀表达式
  3. 计算前缀表达式
  4. 中缀转后缀表达式
  5. 中缀转前缀表达式

相关链接:

  1. 表达式求值汇总
  2. 多位数表达求值

【例子】

表达式 例子
表达式(中缀) 2 ∗ 3
前缀表达式中缀表达式换成后缀表达式的方法是类似的,都可以利用来实现。 对于前缀表达式,我们可以从右往左遍历,遇到操作数直接输出到后缀表达式中,遇到运算符则将其压入中,并将顶的两个操作数弹出进行运算后再将结果压入中。最后将中剩余的运算符依次弹出并输出到后缀表达式中即可。 对于中缀表达式,我们可以从左往右遍历,遇到操作数直接输出到后缀表达式中,遇到运算符则判断其与顶运算符的优先级,如果顶运算符的优先级高于或等于当前运算符,则弹出顶运算符并输出到后缀表达式中,直到为空或顶运算符的优先级低于当前运算符,然后将当前运算符压入中。最后将中剩余的运算符依次弹出并输出到后缀表达式中即可。 以下是 C 语言代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_SIZE 100 typedef struct stack { char data[MAX_STACK_SIZE]; int top; } Stack; void push(Stack *s, char c) { if (s->top == MAX_STACK_SIZE - 1) { printf("Stack overflow!\n"); exit(1); } s->data[++s->top] = c; } char pop(Stack *s) { if (s->top == -1) { printf("Stack underflow!\n"); exit(1); } return s->data[s->top--]; } int is_operator(char c) { return c == '+' || c == '-' || c == '*' || c == '/'; } int get_operator_priority(char c) { if (c == '*' || c == '/') { return 2; } else if (c == '+' || c == '-') { return 1; } else { return 0; } } void prefix_to_postfix(char *prefix, char *postfix) { Stack stack; stack.top = -1; int len = strlen(prefix); for (int i = len - 1; i >= 0; i--) { if (is_operator(prefix[i])) { char op1 = pop(&stack); char op2 = pop(&stack); postfix[strlen(postfix)] = op1; postfix[strlen(postfix)] = op2; postfix[strlen(postfix)] = prefix[i]; push(&stack, postfix[strlen(postfix) - 1]); } else { push(&stack, prefix[i]); } } while (stack.top != -1) { postfix[strlen(postfix)] = pop(&stack); } postfix[strlen(postfix)] = '\0'; } void infix_to_postfix(char *infix, char *postfix) { Stack stack; stack.top = -1; int len = strlen(infix); for (int i = 0; i < len; i++) { if (is_operator(infix[i])) { while (stack.top != -1 && get_operator_priority(stack.data[stack.top]) >= get_operator_priority(infix[i])) { postfix[strlen(postfix)] = pop(&stack); } push(&stack, infix[i]); } else if (infix[i] == '(') { push(&stack, infix[i]); } else if (infix[i] == ')') { while (stack.top != -1 && stack.data[stack.top] != '(') { postfix[strlen(postfix)] = pop(&stack); } if (stack.top == -1) { printf("Invalid expression!\n"); exit(1); } pop(&stack); } else { postfix[strlen(postfix)] = infix[i]; } } while (stack.top != -1) { if (stack.data[stack.top] == '(') { printf("Invalid expression!\n"); exit(1); } postfix[strlen(postfix)] = pop(&stack); } postfix[strlen(postfix)] = '\0'; } int main() { char expression[100], postfix[100] = ""; printf("Enter expression: "); gets(expression); infix_to_postfix(expression, postfix); printf("Postfix expression: %s", postfix); return 0; } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geodoer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值