中缀表达式转换后缀表达式
来源:互联网 发布:武汉美工培训多少钱 编辑:程序博客网 时间:2024/04/29 15:25
中缀表达式是最自然、最易被人类理解的表达方式,但是计算机处理起来并不显得方便,这时,后缀表达式就发挥作用了
例如:计算表达式(a + b) * (c + d)的值,程序里需要不断的判断运算符的优先级,先计算括号里的子表达式
假如我们将上述表达式转换为后缀表达式:a b + c d + *,你会发现现在是不需要括号了
这时我们只需要扫描表达式,碰到数据就入栈,碰到运算符就出栈两个数据,计算完成之后将结果再入栈,最后栈中保存的是表达式的最终结果
这就是后缀表达式的用途之一
本算法只完成中缀表达式到后缀表达式的转换,且假设表达式中只会出现空白符 数据 + - * /四种运算符和括号,栈作为辅助工具
#include <stdio.h>#include <string.h>#define MAX 100#define push(stk, val)stk->data[stk->size++] = val#define pop(stk) stk->data[--stk->size]#define top(stk) stk->data[stk->size - 1]#define empty(stk) stk->size == 0#define init(stk) stk->size = 0struct stack{int data[MAX];int size;};void convert(char *exp){char str[MAX];int i = 0, j = 0;struct stack stk;struct stack *pt = &stk;init(pt);while (exp[i] != '\0'){switch (exp[i]){case ' ':case '\t':case '\n':break;case '+':case '-':while (!empty(pt) && top(pt) != '('){str[j++] = pop(pt);}push(pt, exp[i]);break;case '*':case '/':if (!empty(pt) && (top(pt) == '*' || top(pt) == '/')){str[j++] = pop(pt);}push(pt, exp[i]);break;case '(':push(pt, exp[i]);break;case ')':while (!empty(pt) && top(pt) != '('){str[j++] = pop(pt);}pop(pt);break;default:str[j++] = exp[i];break;}++i;}while (!empty(pt)){str[j++] = pop(pt);}str[j] = '\0';strcpy(exp, str);}int main(void){char exp[MAX] = "a + b * c - (d + e / f) * g";convert(exp);printf("%s\n", exp);return 0;}
0 0
- 表达式 中缀 后缀 转换
- 中缀表达式转换后缀表达式
- 中缀表达式转换后缀表达式
- 中缀表达式转换后缀表达式
- 中缀表达式转换后缀表达式
- 中缀表达式转换后缀表达式
- 中缀表达式转换后缀表达式
- 中缀表达式 前缀表达式 后缀表达式 转换
- C++ 后缀转换中缀表达式
- 中缀后缀表达式的转换
- 中缀表达式转换成后缀
- 前缀,中缀,后缀表达式转换
- 前缀、中缀、后缀表达式转换
- 前缀,中缀,后缀表达式转换
- 中缀表达式转换为后缀表达式(C)
- 中缀表达式转换为后缀表达式算法
- 中缀表达式转换为后缀表达式
- 将中缀表达式转换成后缀表达式
- JS实现文本框内容全复制
- com.ibm.ws.webcontainer.webapp.WebAppErrorReport.
- Oracle 执行计划的获取-2
- 写出高质量的程序
- 常用的方法
- 中缀表达式转换后缀表达式
- 铜仁“廉政短信平台”加强干部预防教育
- 纪念新建博客分类
- Windows下常用指令
- poi 实现下拉框级联选择省市
- Javascript:this用法整理
- Springmvc 应用Mongodb分页实现
- sqlserver2012还原失败的原因(错误代码3154)
- 为CentOS配置静态的IP地址