算数表达式转DAG

咸鱼上接的单,赚了80

码农不管再怎么被黑,也是学以致用率最高的一个行业。

#include
using namespace std;

vector suffix_expression;
vector operator_stack;

vector opt_table;
map opt_id_map;
map opt_priority;


struct node {
    int l, r;
    int opt;
    string label;
    bool operator < (const node a) const {
        return opt <= a.opt;
    }
    bool operator == (const node a) const {
        return l == a.l && r == a.r && opt == a.opt && label == a.label;
    }
};

vector node_table;  
map node_id_map;

int new_opt(string s, int priority)
{
    opt_table.push_back(s);
    opt_id_map[s] = opt_table.size() - 1;
    opt_priority[s] = priority;
    return opt_table.size() - 1;
}

void print_opt(int opt_id)
{
    cout << "opt: " << opt_table[opt_id] << " id: " << opt_id << endl;
}

int new_node(struct node nd)
{
    node_table.push_back(nd);
    node_id_map[nd] = node_table.size() - 1;
    return node_table.size() - 1;
}

void init()
{
    int opt_id;
    new_opt("#", 0);
    opt_id = new_opt("+", 1);
    print_opt(opt_id);
    opt_id = new_opt("-", 1);
    print_opt(opt_id);
    opt_id = new_opt("*", 2);
    print_opt(opt_id);
    opt_id = new_opt("/", 2);
    print_opt(opt_id);
    opt_id = new_opt("%", 2);
    print_opt(opt_id);
    opt_id = new_opt("!", 3);
    print_opt(opt_id);
    opt_id = new_opt("~", 3);
    print_opt(opt_id);

    new_node(node{0, 0, 0});
}

void emtpy_opt_stack()
{
    while (operator_stack.back() != "(") {
        suffix_expression.push_back(operator_stack.back());
        operator_stack.pop_back();
    }
    operator_stack.pop_back();
}

void generate_suffix_expression()
{
    string s;
    cout<<"please input expression: ";
    cin >> s;
    int last = 0;
    operator_stack.push_back("(");
    for (int i = 0; i < s.length(); ++i) {
        if (opt_id_map[string{s[i]}]) {
            suffix_expression.push_back(s.substr(last, i-last));
            last = i + 1;
            while (operator_stack.back() != "(" && opt_priority[operator_stack.back()] > opt_priority[string{s[i]}]) {
                suffix_expression.push_back(operator_stack.back());
                operator_stack.pop_back();
            }
            operator_stack.push_back(string{s[i]});
        } else if (s[i] == '(') {
            suffix_expression.push_back(s.substr(last, i-last));
            last = i + 1;
            operator_stack.push_back("(");
        } else if (s[i] == ')') {
            suffix_expression.push_back(s.substr(last, i-last));
            last = i + 1;
            emtpy_opt_stack();
        }
    }
    suffix_expression.push_back(s.substr(last, s.length()-1));
    emtpy_opt_stack();
}

int get_node_id(struct node nd)
{
    if (node_id_map[nd] > 0) {
        return node_id_map[nd];
    } else {
        return new_node(nd);
    }
}

void dfs(int id)
{
    if (id == 0) {
        return;
    }
    cout << "node_id: " << id << " left_node_id: " << node_table[id].l << " right_node_id: " << node_table[id].r\
     << " opt_id: " << node_table[id].opt << " label: " << node_table[id].label << endl;
    dfs(node_table[id].l);
    dfs(node_table[id].r);
}

int generate_dag()
{
    vector s;
    for (int i=0; i

 

你可能感兴趣的:(算数表达式转DAG)