离散数学真值表的计算


大一菜鸡肝了近两个小时的成果,用于计算真值表;
拿来水一篇博客(并不);
代码中比较重要的两部分是原式向后缀式的转换,遍历所有原子命题的可能取值;
具体的细节看代码吧,尽量添加了注释;

#include

using namespace std;

const int maxn = 105;
const int maxstr = 1e5 + 10;
char s[maxstr],str[maxstr],Vstr[maxn];//依次为原式,后缀式,存放原子命题的符号
bool var[maxn];//不同原子命题的真值
mapv;//储存原子命题对应的编号

void Print_true(bool b){//打印bool对应的真值符号
    if(b) printf("T\t");
    else printf("F\t");
}

int Priority(char c){//返回运算符的优先级
    int p;
    switch (c) {
        case '!': p = 5; break;
        case '&': p = 4; break;
        case '|': p = 3; break;
        case '-': p = 2; break;
        case '=': p = 1; break;
        default : p = 0; break;
    }
    return p;
}

bool ToPostfix(){//原式转化为后缀表达式
    int cnt = 0;
    int len = strlen(s);
    stackope;
    for(int i=0; i Priority(ope.top())){
                    ope.push(s[i]);
                }
                else{
		    while(!ope.empty() && ope.top() != '(' && Priority(s[i]) <= Priority(ope.top())) {
                        str[cnt++] = ope.top();
                        ope.pop();
		    }
                    ope.push(s[i]);
                }
            }
        }
    }
    while(!ope.empty()){
        str[cnt++] = ope.top();
        ope.pop();
    }
    str[cnt] = 0;
    return true;
}

bool Calculate(bool a, bool b, char ope){//进行真值的运算
    bool ans;
    if(ope == '&'){
        if(a == true && b == true) ans = true;
        else ans = false;
    }
    else if(ope == '|'){
        if(a == true || b == true) ans = true;
        else ans = false;
    }
    else if(ope == '-'){
        if(a == true && b == false) ans = false;
        else ans = true;
    }
    else if(ope == '='){
        if(a == b) ans = true;
        else ans = false;
    }
    return ans;
}
void init_var(int n, int sum){//对var数组初始化
    while(sum>0){
        bool x = sum%2;
        var[n--] = x;
        sum /= 2;
    }
    do{
        var[n--] = false;
    }while(n > 0);
}
bool Result(){//对后缀式进行计算
    stackres;
    int len = strlen(str);
    for(int i=0; i