#include <stdio.h>

// 声明五个逻辑联结词的函数
int and_op(int p, int q) {
    return p && q;
}

int or_op(int p, int q) {
    return p || q;
}

int not_op(int p) {
    return !p;
}

int implies_op(int p, int q) {
    return !p || q;
}

int iff_op(int p, int q) {
    return p == q;
}

// 计算命题公式的真值表
void calculate_truth_table(int (*op)(int, int), int p, int q, int r) {
    printf('p	q	r	result
');
    for (int i = 0; i <= 1; i++) {
        for (int j = 0; j <= 1; j++) {
            for (int k = 0; k <= 1; k++) {
                int result = op(p, q);
                printf('%d	%d	%d	%d
', i, j, k, result);
            }
        }
    }
}

int main() {
    int p = 1, q = 1, r = 0;

    // 计算 and_op 的真值表,并判断命题公式类型
    calculate_truth_table(and_op, p, q, r);
    printf('Type: Conjunction

');

    // 计算 or_op 的真值表,并判断命题公式类型
    calculate_truth_table(or_op, p, q, r);
    printf('Type: Disjunction

');

    // 计算 not_op 的真值表,并判断命题公式类型
    calculate_truth_table(not_op, p, q, r);
    printf('Type: Negation

');

    return 0;
}

这段代码实现了五个逻辑联结词的基本功能,并计算了至少3个命题变元的命题公式的真值表,代码示例展示了如何判断命题公式的类型。

C语言实现逻辑联结词真值表:AND、OR、NOT、蕴含、等价

原文地址: https://www.cveoy.top/t/topic/rTr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录