Expression Expression tree Expression evaluation

来源:互联网 发布:快速学英语软件 编辑:程序博客网 时间:2024/05/21 16:18

题目地址

http://dsalgo.openjudge.cn/binarytree/7/

题目大意

表达式·表达式树·表达式求值
描述
众所周知,任何一个表达式,都可以用一棵表达式树来表示。例如,表达式a+b*c,可以表示为如下的表达式树:

+
/ \
a *
/ \
b c

现在,给你一个中缀表达式,这个中缀表达式用变量来表示(不含数字),请你将这个中缀表达式用表达式二叉树的形式输出出来。

输入
输入分为三个部分。
第一部分为一行,即中缀表达式(长度不大于50)。中缀表达式可能含有小写字母代表变量(a-z),也可能含有运算符(+、-、*、/、小括号),不含有数字,也不含有空格。
第二部分为一个整数n(n < 10),表示中缀表达式的变量数。
第三部分有n行,每行格式为C x,C为变量的字符,x为该变量的值。
输出
输出分为三个部分,第一个部分为该表达式的逆波兰式,即该表达式树的后根遍历结果。占一行。
第二部分为表达式树的显示,如样例输出所示。如果该二叉树是一棵满二叉树,则最底部的叶子结点,分别占据横坐标的第1、3、5、7……个位置(最左边的坐标是1),然后它们的父结点的横坐标,在两个子结点的中间。如果不是满二叉树,则没有结点的地方,用空格填充(但请略去所有的行末空格)。每一行父结点与子结点中隔开一行,用斜杠(/)与反斜杠(\)来表示树的关系。/出现的横坐标位置为父结点的横坐标偏左一格,\出现的横坐标位置为父结点的横坐标偏右一格。也就是说,如果树高为m,则输出就有2m-1行。
第三部分为一个整数,表示将值代入变量之后,该中缀表达式的值。需要注意的一点是,除法代表整除运算,即舍弃小数点后的部分。同时,测试数据保证不会出现除以0的现象。
样例输入
a+b*c
3
a 2
b 7
c 5
样例输出
abc*+
+
/ \
a *
/ \
b c
37

Code

#include <iostream>#include <stack>#include <stdlib.h>#include <string.h>#include <math.h>#include <map>#include <algorithm>#define INF 0x3fffffff#define MAXROW 70#define MAXCOL 300using namespace std;int layers;char bl[128];char buf[512][512];map<char, int> mci;struct Node {    char v;    Node* l;    Node* r;    Node() {        l = NULL;        r = NULL;    }    Node(char c) {        v = c;        l = NULL;        r = NULL;    }};void get_bl(string s) {    memset(bl, 0, sizeof(bl));    int j = 0;    stack<char> sc;    for (int i = 0; i < s.size(); i++) {        if (isalpha(s[i])) {            bl[j++] = s[i];        } else if (s[i] == '(') {            sc.push(s[i]);        } else if (s[i] == ')') {            while ((!sc.empty()) && (sc.top() != '(')) {                bl[j++] = sc.top();                sc.pop();            }            sc.pop();        } else if (s[i] == '+' || s[i] == '-') {            while ((!sc.empty()) && (sc.top() != '(')) {                bl[j++] = sc.top();                sc.pop();            }            sc.push(s[i]);        } else {            while ((!sc.empty()) && (sc.top() == '*' || sc.top() == '/')) {                bl[j++] = sc.top();                sc.pop();            }            sc.push(s[i]);        }    }    while (!sc.empty()) {        bl[j++] = sc.top();        sc.pop();    }    bl[j] = '\0';}Node* build_tree() {    stack<Node*> sn;    for (int i = 0; i < strlen(bl); i++) {        if (isalpha(bl[i])) {            Node* node = new Node(bl[i]);            sn.push(node);        } else {            Node* node = new Node(bl[i]);            node->r = sn.top(); sn.pop();            node->l = sn.top(); sn.pop();            sn.push(node);        }    }    return sn.top();}int calculate(Node* root){    if (isalpha(root->v)) return mci[root->v];    if (root->v=='+') return calculate(root->l)+calculate(root->r);    if (root->v=='-') return calculate(root->l)-calculate(root->r);    if (root->v=='*') return calculate(root->l)*calculate(root->r);    if (root->v=='/') return calculate(root->l)/calculate(root->r);}int cal(int a, int b, char c) {    switch (c) {        case '+': return a+b; break;        case '-': return a-b; break;        case '*': return a*b; break;        case '/': return a/b; break;    }}void calculate2() {    // cal2    stack<int> si;    for (int i = 0; i < strlen(bl); i++) {        if (isalpha(bl[i])) {            si.push(mci[bl[i]]);        } else {            int x = si.top(); si.pop();            int y = si.top(); si.pop();            int z = cal(y, x, bl[i]);            si.push(z);        }    }    cout << si.top();}int get_layer(Node *root) {    if (root == NULL) {        return 0;    }    int left = get_layer(root->l);    int right = get_layer(root->r);    return max(left+1, right+1);}void print_tree(Node * root, int root_x, int root_y, int space){    int left_child, right_child;    if (!root) return;    buf[root_y][root_x-1] = root->v;    if (root->l){        buf[root_y+1][root_x-2] = '/';        print_tree(root->l,root_x-space,root_y+2,space>>1);    }    if (root->r){        buf[root_y+1][root_x] = '\\';        print_tree(root->r,root_x+space,root_y+2,space>>1);    }}void print(Node* root) {    layers = get_layer(root);    memset(buf, ' ', sizeof(buf));    int b = INF;    print_tree(root, pow(2,layers-1), 0, pow(2,layers-2));    for (int i = 0; i < 2*layers-1; i++) {        for (int j = 300; j >= 0; j--) {            if (buf[i][j] != ' ') {                buf[i][j+1] = '\0';                break;            }        }    }    for (int i = 0; i < 2*layers-1; i++) {        printf("%s\n", buf[i]);    }}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);#endif    string s;    int n;    char c;    int v;    cin >> s;    cin >> n;    for (int i = 0; i < n; i++) {        cin >> c >> v;        mci[c] = v;    }    get_bl(s);    printf("%s\n", bl);    Node *root = build_tree();    print(root);    int ans = calculate(root);    //cout << ans << endl;    calculate2();    return 0;}

参考

  • http://blog.csdn.net/u014391294/article/details/50221977
原创粉丝点击