王浩算法实现

· · 个人记录

感觉是上大学之后唯一的课内代码练习,记录一下。

写了个中缀转后缀转表达式树(以及判断格式合法),感觉还可以,就是有点史。

/*
Example:

Input:
(!Q&(P->Q))->!P

Output:
Init: 
(0.1) ((!Q)&(P->Q))=>(!P)

Step 1: (&=>)
(1.1) (!Q),(P->Q)=>(!P)

Step 2: (!=>)
(2.1) (P->Q)=>(!P),Q

Step 3: (->=>)
(3.1) Q=>(!P),Q
(3.2) =>(!P),Q,P

Step 4: (=>!)
(4.1) =>(!P),Q,P
(4.2) Q,P=>Q

Step 5: (=>!)
(5.1) Q,P=>Q
(5.2) P=>Q,P

The proposition is TRUE.
*/
#include <iostream>
#include <functional>
#include <string>
#include <queue>
#include <stack>

const int MAXN = 100;

int tree_cnt;
struct Tree_node {
    int isleaf, value, ch[2];
} info[MAXN + 5];

std::queue< std::pair< std::vector<int>, std::vector<int> > > Queue;

int New_treenode(int type, int value) {
    tree_cnt++;
    info[tree_cnt].isleaf = type;
    info[tree_cnt].value = value;
    return tree_cnt;
}

void Put_invalid() {
    std::cout << "The input proposition is invalid.\n";
    exit(0);
}

// to check my code, this function can print infix expression of a expression tree
// notice that, there will be extra brackets to ensure the priority correctness
void Output_expression_tree(int u) { 
    if (!u) return ; 
    if (info[u].isleaf == 0) {
        std::cout << '('; 
        Output_expression_tree(info[u].ch[0]);
        int v = info[u].value;
        if (v == 2) std::cout << "<->";
        else if (v == 3) std::cout << "->";
        else if (v == 4) std::cout << "|";
        else if (v == 5) std::cout << "&";
        else if (v == 6) std::cout << "!";
        else std::cout << v;
        Output_expression_tree(info[u].ch[1]);
        std::cout << ')'; 
    } else {
        std::cout << char(info[u].value + 'A');
    }
}

// use two stacks to convert infix expression to postfix expression while building expression tree
int Build_expression_tree(std::string statement) {
    int len_statement = statement.length();
    std::stack<int> stack_value, stack_oper;
    std::function<bool(char)> Is_variable = [&](char c) {
        return 'A' <= c && c <= 'Z';
    } ; 
    std::function<void(int)> Add_oper_to_stack_value = [&](int now_nd) {
        int x = 0, y = 0; 
        if (info[now_nd].value == 6) {
            if (stack_value.size() == 0) Put_invalid(); 
            y = stack_value.top(), stack_value.pop(); 
        } else {
            if (stack_value.size() <= 1) Put_invalid(); 
            y = stack_value.top(); stack_value.pop();
            x = stack_value.top(); stack_value.pop();
        }
        info[now_nd].ch[0] = x, info[now_nd].ch[1] = y;
        stack_value.emplace(now_nd);
    } ;
    bool last_is_variable = 0;
    for (int i = 0; i < len_statement; i++) {
        if (Is_variable(statement[i])) {
            if (last_is_variable == 1) {
                Put_invalid();
                // two consecutive positions should not both contain variables.
            }
            last_is_variable = 1;
            stack_value.emplace(New_treenode(1, statement[i] - 'A'));
        } else {
            last_is_variable = 0;
            if (statement[i] == '(') {
                stack_oper.emplace(New_treenode(0, 1));
            } else if (statement[i] == ')') {
                if (!stack_oper.size()) {
                    Put_invalid();
                }
                int now_nd = -1, now_op = -1;
                while (stack_oper.size() && (now_op = info[(now_nd = stack_oper.top())].value) != 1) {
                    stack_oper.pop();
                    Add_oper_to_stack_value(now_nd);
                }
                if (now_nd == -1 || now_op != 1) {
                    Put_invalid();
                }
                stack_oper.pop(); // remove the '(' on the top
            } else {
                int operid = -1;
                int nxt = 0;
                if (statement[i] == '!') {
                    operid = 6; 
                } else if (statement[i] == '&') {
                    operid = 5;
                } else if (statement[i] == '|') {
                    operid = 4;
                } else if (statement[i] == '-') {
                    if (i + 1 > len_statement || statement[i + 1] != '>') {
                        Put_invalid();
                    }
                    operid = 3; nxt = 1;
                } else if (statement[i] == '<') {
                    if (i + 2 > len_statement || statement[i + 1] != '-' || statement[i + 2] != '>') {
                        Put_invalid();
                    }
                    operid = 2; nxt = 2;
                } else {
                    Put_invalid();
                }
                int now_nd = -1, now_op = -1;
                while (stack_oper.size() && (now_op = info[now_nd = stack_oper.top()].value) >= operid) {
                    stack_oper.pop();
                    Add_oper_to_stack_value(now_nd);
                }
                stack_oper.emplace(New_treenode(0, operid));
                i += nxt; // next position will be i + nxt + 1
            }
        }
    }
    int now_nd = -1; 
    while (stack_oper.size()) {
        int now_op = info[now_nd = stack_oper.top()].value; 
        stack_oper.pop(); 
        if (now_op == 1) Put_invalid(); 
        Add_oper_to_stack_value(now_nd); 
    }
    if (stack_oper.size() != 0 || stack_value.size() != 1) {
        Put_invalid();
    }
    return stack_value.top();
}

void Input_work() {
    std::string statement, antecedent, consequence;
    std::cin >> statement;
    int root = Build_expression_tree(statement);
    std::vector<int> antevec, convec;
    if (info[root].isleaf == 1 || info[root].value != 3) {
        // add "=> in front of statement 
        convec.emplace_back(root); 
        Queue.emplace(antevec, convec);
    } else { 
        // use the topest "->" as "=>"
        int ante_root = info[root].ch[0]; 
        int con_root = info[root].ch[1];
        antevec.emplace_back(ante_root);
        convec.emplace_back(con_root);
        Queue.emplace(antevec, convec);
    }
}

void WangHao_Algorithm() {
    int Step = 0; 
    std::string last_rule = ""; 
    std::function<int()> Check_and_output = [&]() {
        if (Queue.size() == 0) return 1; 
        if (Step == 0) std::cout << "\nInit: \n"; 
        else std::cout << "Step " << Step << ": (" << last_rule << ")\n"; 
        Step++; 
        auto Qcopy = Queue; 
        int False = 0; 
        int id_cnt = 0; 
        while (!Qcopy.empty()) {
            auto P = Qcopy.front(); Qcopy.pop(); 
            auto antevec = P.first, convec = P.second; 
            // first, print 
            std::cout << "(" << Step - 1 << "." << ++id_cnt << ") "; 
            if (antevec.size()) {
                Output_expression_tree(antevec[0]);
                for (int i = 1; i < antevec.size(); i++) {
                    std::cout << ","; 
                    Output_expression_tree(antevec[i]); 
                }
            } 
            std::cout << "=>"; 
            if (convec.size()) {
                Output_expression_tree(convec[0]);
                for (int i = 1; i < convec.size(); i++) {
                    std::cout << ","; 
                    Output_expression_tree(convec[i]); 
                }
            }
            std::cout << '\n'; 
            // second, check
            int find_operation = 0; 
            int occur[30] = {}, find_same = 0; 
            for (auto Root : antevec) {
                if (!info[Root].isleaf) find_operation = 1; 
                else occur[info[Root].value] = 1; 
            }
            for (auto Root : convec) {
                if (!info[Root].isleaf) find_operation = 1; 
                else if (occur[info[Root].value]) find_same = 1; 
            }
            if (!find_operation && !find_same) {
                False = 1; 
            }
        }
        std::cout << "\n"; 
        if (False) return 0; 
        return 1; 
    }; 
    while (!Queue.empty()) {
        if (!Check_and_output()) {
            std::cout << "The proposition is FALSE. \n" << '\n'; 
            return ; 
        }
        auto Front = Queue.front(); Queue.pop();
        auto antevec = Front.first, convec = Front.second; // roots 
        bool find_operation = 0; 
        for (auto iter = antevec.begin(); iter != antevec.end(); iter++) {
            int Root = *iter; 
            if (info[Root].isleaf) continue ; 
            find_operation = 1; 
            int now_op = info[Root].value; 
            antevec.erase(iter); 
            int x = info[Root].ch[0], y = info[Root].ch[1]; 
            if (now_op == 6) {
                convec.emplace_back(y); 
                Queue.emplace(antevec, convec); 
                last_rule = "!=>"; 
            } else if (now_op == 5) {
                antevec.emplace_back(x); 
                antevec.emplace_back(y); 
                Queue.emplace(antevec, convec); 
                last_rule = "&=>"; 
            } else if (now_op == 4) {
                std::vector<int> antevec1, antevec2; 
                antevec1 = antevec2 = antevec; 
                antevec1.emplace_back(x); 
                antevec2.emplace_back(y); 
                Queue.emplace(antevec1, convec); 
                Queue.emplace(antevec2, convec); 
                last_rule = "|=>"; 
            } else if (now_op == 3) {
                std::vector<int> antevec1, convec1; 
                antevec1 = antevec, antevec1.emplace_back(y); 
                convec1 = convec, convec1.emplace_back(x); 
                Queue.emplace(antevec1, convec); 
                Queue.emplace(antevec, convec1); 
                last_rule = "->=>"; 
            } else if (now_op == 2) {
                std::vector<int> antevec1, convec1; 
                antevec1 = antevec, antevec1.emplace_back(x), antevec1.emplace_back(y); 
                convec1 = convec, convec1.emplace_back(x), convec1.emplace_back(y); 
                Queue.emplace(antevec1, convec); 
                Queue.emplace(antevec, convec1); 
                last_rule = "<->=>"; 
            }
            break ;
        }
        if (find_operation) continue ; 
        for (auto iter = convec.begin(); iter != convec.end(); iter++) {
            int Root = *iter; 
            if (info[Root].isleaf) continue ; 
            find_operation = 1; 
            int now_op = info[Root].value; 
            convec.erase(iter); 
            int x = info[Root].ch[0], y = info[Root].ch[1]; 
            if (now_op == 6) {
                antevec.emplace_back(y); 
                Queue.emplace(antevec, convec); 
                last_rule = "=>!"; 
            } else if (now_op == 5) {
                std::vector<int> convec1, convec2; 
                convec1 = convec2 = convec; 
                convec1.emplace_back(x); 
                convec2.emplace_back(y); 
                Queue.emplace(antevec, convec1); 
                Queue.emplace(antevec, convec2); 
                last_rule = "=>&"; 
            } else if (now_op == 4) {
                convec.emplace_back(x); 
                convec.emplace_back(y); 
                Queue.emplace(antevec, convec); 
                last_rule = "=>|"; 
            } else if (now_op == 3) {
                std::vector<int> antevec1, convec1; 
                antevec1 = antevec, antevec1.emplace_back(x); 
                convec1 = convec, convec1.emplace_back(y); 
                Queue.emplace(antevec1, convec); 
                Queue.emplace(antevec, convec1); 
                last_rule = "=>->"; 
            } else if (now_op == 2) {
                std::vector<int> antevec1, antevec2, convec1, convec2; 
                antevec1 = antevec2 = antevec; 
                convec1 = convec2 = convec; 
                antevec1.emplace_back(x), convec1.emplace_back(y); 
                antevec2.emplace_back(y), convec2.emplace_back(x); 
                Queue.emplace(antevec1, convec1); 
                Queue.emplace(antevec2, convec2); 
                last_rule = "=><->"; 
            }
            break ; 
        }
        if (find_operation) continue ;
        std::cout << "The proposition is TRUE. \n"; 
        return ; 
    }
}

int main() {
    Input_work();
    WangHao_Algorithm(); 
    return 0;
}