03 利用栈进行中缀表达式计算

运算符优先级

​​​​请添加图片描述
栈内运算符加减乘除取模优先级比栈外优先级大1,例如当2+3-5时我们往往从左到右计算,即先算+再算-,使用中缀表达式两个栈计算就是栈外-优先级低于栈顶+,故会弹出+运算符和两个操作数进行计算。

中缀表达式计算(需要两个栈)

  • 需要使用两个栈,一个数字栈,用来存数字,另一个是运算符栈,用于存运算符
  • 数字栈出栈:只有运算符栈中右运算符出栈时,数字才会弹出两个操作数
  • 运算符栈出栈:比较新运算符与栈顶运算符优先级高低,当新运算符优先级高于站顶运算符时,新运算符入栈。当新运算符优先级低于栈顶元素时,栈顶运算符出栈(数字栈弹出两个操作数,计算结果入数字栈),然后再和新的栈顶元素进行比较,直到优先级高于栈顶元素或优先级相等(即栈内左括号,栈外右括号)
  • 计算:当数字栈弹出两个操作数,运算符栈出栈运算符时计算,计算结果重新入数字栈

以A+B×(C-D)-E/F为例

  1. 一开始数字栈和符为空,数字栈进了A和B,符号栈进了+
  2. ×运算符优先级高于+,进栈
  3. ( 优先级在入栈前是最高的(入栈后优先级变为最低),当然高于×,进栈
  4. - 优先级高于站内( ,入栈,D入栈
  5. 栈外 ) 优先级低于 - ,- 出栈,计算C-D设为r1,让r1入栈,继续比较,栈外)优先级等于栈内(,把( 扔掉,出栈结束
  6. 后续步骤如图:
    在这里插入图片描述

后缀表达式计算(需要一个栈)

中缀表达式转后缀表达式

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是利用中缀表达式转换成后缀表达式并进行运算的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct stack_node { char data; struct stack_node *next; } stack_node; typedef struct { stack_node *top; } stack; stack *create_stack() { stack *s = (stack *)malloc(sizeof(stack)); s->top = NULL; return s; } void push(stack *s, char c) { stack_node *node = (stack_node *)malloc(sizeof(stack_node)); node->data = c; node->next = s->top; s->top = node; } char pop(stack *s) { if (s->top == NULL) { printf("Error: stack is empty\n"); exit(1); } char c = s->top->data; stack_node *temp = s->top; s->top = s->top->next; free(temp); return c; } char peek(stack *s) { if (s->top == NULL) { printf("Error: stack is empty\n"); exit(1); } return s->top->data; } int is_empty(stack *s) { return (s->top == NULL); } int is_operator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/'); } int precedence(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': return 2; default: return 0; } } char *infix_to_postfix(char *infix) { stack *s = create_stack(); char *postfix = (char *)malloc(sizeof(char) * (strlen(infix) + 1)); int i, j; for (i = 0, j = 0; infix[i] != '\0'; i++) { if (isdigit(infix[i])) { postfix[j++] = infix[i]; } else if (is_operator(infix[i])) { while (!is_empty(s) && is_operator(peek(s)) && precedence(peek(s)) >= precedence(infix[i])) { postfix[j++] = pop(s); } push(s, infix[i]); } else if (infix[i] == '(') { push(s, infix[i]); } else if (infix[i] == ')') { while (!is_empty(s) && peek(s) != '(') { postfix[j++] = pop(s); } if (is_empty(s)) { printf("Error: mismatched parentheses\n"); exit(1); } pop(s); } else { printf("Error: invalid character\n"); exit(1); } } while (!is_empty(s)) { if (peek(s) == '(') { printf("Error: mismatched parentheses\n"); exit(1); } postfix[j++] = pop(s); } postfix[j] = '\0'; return postfix; } int evaluate_postfix(char *postfix) { stack *s = create_stack(); int i, operand1, operand2, result; for (i = 0; postfix[i] != '\0'; i++) { if (isdigit(postfix[i])) { push(s, postfix[i] - '0'); } else if (is_operator(postfix[i])) { operand2 = pop(s); operand1 = pop(s); switch (postfix[i]) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; case '*': result = operand1 * operand2; break; case '/': if (operand2 == 0) { printf("Error: division by zero\n"); exit(1); } result = operand1 / operand2; break; } push(s, result); } else { printf("Error: invalid character\n"); exit(1); } } result = pop(s); if (!is_empty(s)) { printf("Error: invalid expression\n"); exit(1); } return result; } int main() { char infix[100], *postfix; int result; printf("Enter an infix expression: "); scanf("%s", infix); postfix = infix_to_postfix(infix); printf("Postfix expression: %s\n", postfix); result = evaluate_postfix(postfix); printf("Result: %d\n", result); free(postfix); return 0; } ``` 该程序中,`stack_node`结构体表示中的节点,包含一个字符类型的数据`data`和一个指向下一个节点的指针`next`。`stack`结构体表示,包含一个指向顶节点的指针`top`。`create_stack`函数用于创建一个空,`push`函数用于将一个字符压入中,`pop`函数用于弹出顶字符,`peek`函数用于获取顶字符而不弹出,`is_empty`函数用于判断是否为空。 `is_operator`函数用于判断一个字符是否为运算符,`precedence`函数用于获取运算符的优先级。`infix_to_postfix`函数用于将中缀表达式转换成后缀表达式,采用经典的算法:从左到右遍历中缀表达式,如果是数字直接输出到后缀表达式中,如果是左括号入,如果是右括号则弹出中的元素直到遇到左括号,如果是运算符则弹出中优先级大于等于它的元素并输出到后缀表达式中,然后将自己入。最后将中剩余的元素弹出并输出到后缀表达式中。在转换过程中需要处理一些错误情况,如不合法的字符、不匹配的括号等。 `evaluate_postfix`函数用于计算后缀表达式的值,也是采用经典的算法:从左到右遍历后缀表达式,如果是数字则入,如果是运算符则弹出中的两个操作数进行运算,并将结果入。最后中剩下的元素就是表达式的值。 在`main`函数中,首先读入中缀表达式,然后调用`infix_to_postfix`函数将其转换成后缀表达式,并输出后缀表达式。接着调用`evaluate_postfix`函数计算后缀表达式的值,并输出结果。最后释放动态分配的内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值