栈的应用——四则表达式
来源:互联网 发布:淘宝网买东西怎么付款 编辑:程序博客网 时间:2024/05/21 22:35
#include <stdio.h>#define MAX 100struct operand{ int data[MAX]; int top;};struct operator_ch{ int top; char data[MAX];};typedef struct operand OPND;typedef struct operator_ch OPCH;void init_OPND_stack(OPND *stack){stack->top = -1;}void init_OPCH_stack(OPCH *stack){stack->top = -1;}void is_empty_OPND(OPND *stack){if(stack->top == -1){return -1;}return 0;}void is_empty_OPCH(OPCH *stack){if(stack->top == -1){return -1;}return 0;}int get_OPND_top(OPND *stack){if(is_empty_OPND(stack) == -1){return -1;}return stack->data[stack->top];}int get_OPCH_top(OPCH *stack){if(is_empty_OPCH(stack) == -1){return -1;}return stack->data[stack->top];}int push_OPND(OPND *stack, int num){ stack->data[++(stack->top)] = num;}int push_OPCH(OPND *stack, char ch){ stack->data[++(stack->top)] = ch;}int pop_OPND(OPND *stack){if(is_empty_OPND(stack) == -1){return -1;} int num = stack->data[stack->top];(stack->top)--;return num;} char pop_OPCH(OPCH *stack){if(is_empty_OPCH(stack) == -1){return -1;} char ch = stack->data[stack->top];(stack->top)--;return ch;} int get_priority(char ch){if(ch == '+' || ch == '-'){return 1;}if(ch == '*' || ch == '/'){return 2;}}int compare_priority(char op_ch, char ch){if(get_priority(op_ch) > get_priority(ch)){return 1;}return -1;}int count(int num1, int num2, char ch){ int result;switch(ch){case '+':{ result = a + b;break;}case '-':{result = a - b;break;}case '*': 2{result = a * b;break;}case '-':{result = a / b;break;}}return result;}int main(){ char ch;char op_ch;while((ch = getchar()) != '\n'){ if(ch >= '0' && ch <= '9'){ push_stack1();}if(ch == '+' || ch == '-' || ch == '*' || ch == '/'){op_ch = pop_stack2();if(compare(op_ch,ch) > 0){push_stack2(ch);}else{count(a,b,op_ch);}}}return 0;}
0 0
- 栈的应用——四则表达式
- 栈的应用——四则表达式求值
- 栈的应用——四则表达式求值
- 数据结构之栈的应用——四则表达式求值
- 栈的应用——四则表达式求值
- 后缀表达式+栈的应用=四则表达式运算
- 大话数据结构——栈的应用四则元算表达式求值(后缀表达式计算结果)
- 堆栈的应用:四则表达式运算
- 数据结构 栈(四则表达式)
- 四则表达式
- C++计算四则表达式的模板
- C++计算四则表达式的模板
- 计算四则混合运算表达式(不用栈)
- 正则表达式在网页处理中的应用四则
- 正则表达式在网页处理中的应用四则
- 正则表达式在网页处理中的应用四则
- 正则表达式在网页处理中的应用四则
- 正则表达式在网页处理中的应用四则
- 使用iso文件创建本地安装源 - centos7环境
- NodeJS-糗事百科段子爬虫
- 基于IAP和Keil MDK的远程升级设计
- [solr]查询权重排序
- 超详细Linux搭建Android开发环境
- 栈的应用——四则表达式
- Cookie基础知识
- 设置ListBox选中项的背景颜色
- JavaScript_异常
- dyld: Library not loaded
- git 服务搭建 windows
- 【Visual Studio】Stopwatch类计算程序运行所需时间
- MySQL 存储表情字符
- RXJAVA响应式编程