回顾数据结构之栈的应用-表达式求值
来源:互联网 发布:软件概要设计说明 编辑:程序博客网 时间:2024/05/19 00:50
求测试
#include <iostream>#include <map>#include <stack>#include <cstdio>#include <cstring>using namespace std;stack<char> op;stack<double> s;char hash[256][256];char c[1000];int num;char buf[1005];int id;void init(){ hash['+']['+'] = '>';hash['+']['-'] = '>';hash['+']['*'] = '<';hash['+']['/'] = '<'; hash['+']['('] = '<';hash['+'][')'] = '>';hash['+']['#'] = '>'; hash['-']['+'] = '>';hash['-']['-'] = '>';hash['-']['*'] = '<';hash['-']['/'] = '<'; hash['-']['('] = '<';hash['-'][')'] = '>';hash['-']['#'] = '>'; hash['*']['+'] = '>';hash['*']['-'] = '>';hash['*']['*'] = '>';hash['*']['/'] = '>'; hash['*']['('] = '<';hash['*'][')'] = '>';hash['*']['#'] = '>'; hash['/']['+'] = '>';hash['/']['-'] = '>';hash['/']['*'] = '>';hash['/']['/'] = '>'; hash['/']['('] = '<';hash['/'][')'] = '>';hash['/']['#'] = '>'; hash['(']['+'] = '<';hash['(']['-'] = '<';hash['(']['*'] = '<';hash['(']['/'] = '<'; hash['(']['('] = '<';hash['('][')'] = '=';hash['(']['#'] = ' '; hash[')']['+'] = '>';hash[')']['-'] = '>';hash[')']['*'] = '>';hash[')']['/'] = '>'; hash[')']['('] = ' ';hash[')'][')'] = '>';hash[')']['#'] = '>'; hash['#']['+'] = '<';hash['#']['-'] = '<';hash['#']['*'] = '<';hash['#']['/'] = '<'; hash['#']['('] = '<';hash['#'][')'] = ' ';hash['#']['#'] = '=';}double GetNum(double a, char th, double b){ if(th == '+') return a + b; else if(th == '-') return a - b; else if(th == '*') return a * b; else return a / b;}char Precede(char a, char b){ return hash[a][b];}void read(){ while(buf[id] == ' ' || buf[id] == '\t'){ id++; } c[num++] = buf[id++];}int main(){ init(); while(gets(buf)){ int i; for(i = 0; buf[i]; ++i){ if(buf[i] < '0' || buf[i] > '9'){ break; } } if(buf[i] == 0){ printf("%s.0000\n", buf); continue; } int len = strlen(buf); buf[len++]='#'; id = 0; num = 0; while(!op.empty()) op.pop(); while(!s.empty()) s.pop(); op.push('#'); int flag = 0; int tt = 0; read(); while(c[num - 1] != '#' || op.top() != '#'){ if(c[num - 1] >= '0' && c[num - 1] <= '9'){ tt = tt * 10 + c[num - 1] - '0'; flag = 1; read(); } else{ if(flag){ s.push(tt + 0.0); flag = tt = 0; } if(c[num - 1] == '-' || c[num - 1] == '+'){ if(num >= 2 && (c[num - 2] == ')' || (c[num - 2] >= '0' && c[num - 2] <= '9'))) ; else{ s.push(0.0); } } switch(Precede(op.top(), c[num - 1])){ case '<': op.push(c[num - 1]); read(); break; case '=': op.pop(); read(); break; case '>': char th = op.top(); op.pop(); double b = s.top(); s.pop(); double a = s.top(); s.pop(); s.push(GetNum(a, th, b)); break; } } } printf("%.4lf\n", s.top()); } return 0;}
- 回顾数据结构之栈的应用-表达式求值
- 数据结构之栈的应用——四则表达式求值
- 数据结构(6)--栈的应用之中缀表达式求值
- 数据结构 栈的应用-- 表达式求值
- 【数据结构】栈的应用 I :表达式求值
- 栈的应用-表达式求值-数据结构
- 表达式求值(数据结构-栈的应用 )
- 栈的应用之表达式求值code_legend
- STL栈的应用之表达式求值
- javascript栈的应用之表达式求值
- 栈的应用之表达式求值
- 栈的应用之算术表达式求值
- 栈的应用之表达式求值
- 栈的应用之中缀表达式求值
- 11、数据结构笔记之十一栈的应用之表达式求值实现
- 数据结构:栈的典型应用之二:四则运算表达式求值(C++)
- 表达式求值 数据结构 C/C++ 栈的应用
- 表达式求值(数据结构书上栈的应用之一)
- 原来父母没有我们想像那么坚强!
- $.Ajax 调用webService客户端js
- 解决 nginx 环境下的php 上传文件大小限制
- 关于数据恢复,记一次修复SD卡 RAW 之后的修复过程
- APK权限大全 - Android必懂知识
- 回顾数据结构之栈的应用-表达式求值
- 【转】 mysql触发器的实战经验
- [IT 男人帮] 从 4 岁女孩身上看到的未来
- 中国的车祸为什么多?一个”海归”的亲身体验(转)
- dreamweaver随记-----图片热点区域连接
- 敬告某些闲得蛋疼的人,不喜欢就不要看
- 怎么打入公司主流群体
- 转一篇对杨老先生的评价
- 使用URL RewriteFliter实现网站的伪静态