【Openjudge】表达式·表达式树·表达式求值
2017-11-07 15:39
447 查看
代码写的比较烂,有时间再看看别人代码,重写一个好了。
#include<iostream> #include<stack> #include<map> #include<queue> #include<string> using namespace std; bool compare(char c, char c_in_stack){ if (c == '*' || c == '/'){ if (c_in_stack == '+' || c_in_stack == '-') return true; } return false; } int main() { string s, revs; map<char, int> variable_dic; int variable_num; cin >> s; cin >> variable_num; for (int i = 0; i < variable_num; i ++){ char c; int t; cin >> c >> t; variable_dic[c] = t; } //make the string to a RPN(reverse polish notation) int length = s.length(); stack<char> trans; for (int i = 0; i < length; i ++){ if (s[i] >= 'a' && s[i] <= 'z'){ revs.push_back(s[i]); } else if (s[i] == '(' || trans.empty()){ trans.push(s[i]); } else if (s[i] == ')'){ while(trans.top() != '('){ revs.push_back(trans.top()); trans.pop(); } trans.pop(); } else{ if (compare(s[i], trans.top())){ trans.push(s[i]); } else{ while (!trans.empty() && trans.top() != '(' && !compare(s[i], trans.top())){ revs.push_back(trans.top()); trans.pop(); } trans.push(s[i]); } } } while(!trans.empty()){ revs.push_back(trans.top()); trans.pop(); } cout << revs << endl; //compute the depth and build the tree and compute the answer //node of the tree struct _node{ char data; _node *left; _node *right; int depth; _node(char d = 0, _node *l = NULL, _node *r = NULL):data(d), left(l), right(r){ if (left){ if (right){ depth = left->depth > right->depth ? left->depth:right->depth; } else{ depth = left->depth; } } else{ if (right){ depth = right->depth; } else{ depth = -1; } } depth ++; }; }; class computer{ public: int operator()(int a, int b, char c){ switch(c){ case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': return a / b; default: return 1; } } }; //breadth first traversal class bftravel{ public: int power(int a, int b){ int ans = 1; for (int i = 0; i < b ; i ++){ ans *= a; } return ans; } void operator()(_node* root){ struct node_with_depth{ _node *pointer; int pos; node_with_depth(_node* p = NULL, int d = 0):pointer(p), pos(d){}; }; int max_depth = root->depth; queue<node_with_depth> draw; node_with_depth draw_ele(root, power(2, max_depth)); draw.push(draw_ele); while (!draw.empty()){ string s(power(2, max_depth + 1) + 1, ' '); string sbar(power(2, max_depth + 1) + 1, ' '); int depth = draw.front().pointer->depth; while(!draw.empty() && depth == draw.front().pointer->depth){ draw_ele = draw.front(); draw.pop(); s[draw_ele.pos - 1] = draw_ele.pointer->data; if (draw_ele.pointer->left){ sbar[draw_ele.pos - 2] = '/'; draw_ele.pointer->left->depth = depth - 1; draw.push(node_with_depth(draw_ele.pointer->left, draw_ele.pos - power(2,draw_ele.pointer->depth - 1))); } if (draw_ele.pointer->right){ sbar[draw_ele.pos] = '\\'; draw_ele.pointer->right->depth = depth - 1; draw.push(node_with_depth(draw_ele.pointer->right, draw_ele.pos + power(2, draw_ele.pointer->depth - 1))); } } s[draw_ele.pos] = 0; if (draw.empty()){ sbar[0] = 0; } else{ int i = draw_ele.pos + 2; while (sbar[i] == ' ') sbar[i --] = 0; sbar[i + 1] = '\n'; } cout << s.c_str() << endl << sbar.c_str(); } } }; stack<_node*> compute_stack; stack<int> compute_ans; _node *pointer; length = revs.length(); //build the tree and compute the answer for the formula for (int i = 0; i < length; i ++){ if (revs[i] >= 'a' && revs[i] <= 'z'){ pointer = new _node(revs[i]); compute_stack.push(pointer); compute_ans.push(variable_dic[revs[i]]); } else{ _node *right_child = compute_stack.top(); compute_stack.pop(); _node *left_child = compute_stack.top(); compute_stack.pop(); pointer = new _node(revs[i], left_child, right_child); compute_stack.push(pointer); int b = compute_ans.top(); compute_ans.pop(); int a = compute_ans.top(); compute_ans.pop(); compute_ans.push(computer()(a, b, revs[i])); } } bftravel()(pointer); cout << compute_ans.top() << endl; return 0; }
相关文章推荐
- 二叉树应用-表达式·表达式树·表达式求值(数据结构基础 第6周)
- Openjudge-计算概论(A)-简单算术表达式求值
- 数据结构之---C语言实现栈的表达式求值(表达式树)
- 表达式·表达式树·表达式求值
- Openjudge-NOI题库-简单算术表达式求值
- C,C++表达式求值顺序超详解
- NYOJ35 表达式求值 ,后缀法求解
- 表达式求值的实现
- C/C++计算器(利用栈表达式求值,支持函数运算)
- MYSQL表达式求值和MYSQL类型转换
- C++ 实现 算数表达式求值
- 关于C++表达式求值的困惑
- hdu 1237 简单计算器【最简单的表达式求值】
- 表达式求值2
- 把后缀表达式转换为表达式树
- nyoj 35 表达式求值(栈)
- 九度OJ 1101 表达式求值(栈的应用)
- 数据结构课程设计---------用栈来实现表达式求值
- 华为oj中级 表达式求值
- 求解表达式的值 表达式树的运用