您的位置:首页 > 运维架构

【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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: