中缀表达式转后缀表达式优先级(利用栈将中缀转换为后缀)

中缀表达式转为后缀表达式并求值

  • 在计算机上用的 1、利用栈来实现 2、利用语法树来实现 先把中缀表达式用二叉树表示出来,再后序遍历该二叉树就得迹鸡管课攮酒归旬害莫到相应的后缀表达式了 在草稿上手工转换 3、加括号法

中缀表达式A+(B-CD)*E 转换成后缀表达式,并写出详细过程(包括()+ – * 的优先级)!

  • ABCD-E*+

将一个中缀表达式转换为一个后缀表达式,用栈的方法,能显示,输入之后就死循环了

  • #include "stdafx.h"#include stdio.h#includestdlib.htypedef struct node{ int num; char cha; struct node *next;}stack;敞甫搬晃植浩邦彤鲍廓int judge(char c, char c1)c将要进栈,c1栈顶符号{ int temp1 = 2, temp2 = 2; switch (c) { case +: case -:temp1 = 1;break; default: break; } switch (c1) { case +: case -:temp2 = 1;break; default: break; } if (temp1 temp2)return 1; return 0;}int IsEmpty(stack *head){ if (head-num == NULL&&head-num == NULL)return 0; return 1;}stack *MakeHead(stack *head){ head = (stack*)malloc(sizeof(stack)); head-cha = NULL; head-num = NULL; head-next = NULL; return head;}stack *InS_Stack(stack *s_stack, int k){ stack *p; p = (stack*)malloc(sizeof(stack)); p-cha = NULL; p-num = k; p-next = s_stack-next; s_stack-next = p; return s_stack;}stack *InC_Stack(stack *s_stack, char c){ stack *p; p = (stack*)malloc(sizeof(stack)); p-cha = c; p-num = NULL; p-next = s_stack-next; s_stack-next = p; return s_stack;}stack* Out_stack(stack* s_stack){ stack *p; p = s_stack-next; free(s_stack-next); s_stack = p-next; return s_stack;}int main(){ int i = 0, m = 0, k = 0, f = 0; char temp; char c[100]; stack *s_stack, *p, *s_stack1; s_stack = (stack*)malloc(sizeof(stack)); s_stack1 = (stack*)malloc(sizeof(stack)); s_stack = MakeHead(s_stack); s_stack1 = MakeHead(s_stack1); printf("请输入算式,=结束:n"); while (1) { scanf_s("%c", c[i]); i++; } i = 0; while (1) { if (c[i] == =) break; if (c[i] = 0&&c[i] = 9) { m = 10 * m + c[i]; k = m; } else { if (c[i – 1] = 0&&c[i – 1] = 9) { s_stack = InS_Stack(s_stack, k); } if (c[i] == + || c[i] == – || c[i] == * || c[i] == ) { p = s_stack-next; while (p) { if (p-cha) { temp = p-cha; f = judge(c[i], temp); if (!f) s_stack = InC_Stack(s_stack, c[i]); else { while (s_stack-next-cha&&judge(c[i], s_stack-next-cha)) { if (s_stack-next-cha) InC_Stack(s_stack1, s_stack-next-cha); else InS_Stack(s_stack1, s_stack-next-num); s_stack = Out_stack(s_
  • 你代码写成这样大大降低了可读性,没有人愿意看

如何用js实现中缀表达式转后缀表达式然后求值

  • 在计算机上用的 1、利用栈来实现 2、利用语法树来实现 先把中缀表达式用二叉树表示出来,发础篡飞诂读磋嫂单讥再后序遍历该二叉树就得到相应的后缀表达式了 在草稿上手工转换 3、加括号法
版权声明