C语言实现命题逻辑计算器:生成真值表并判断公式类型
#include <stdio.h> #include <stdlib.h>
// 定义逻辑联结词的枚举类型 typedef enum { AND, OR, NOT, IMPLIES, EQUIVALENT } Connective;
// 定义命题变元的结构体 typedef struct { int value; int isAssigned; } PropositionVariable;
// 定义命题公式的结构体 typedef struct { Connective connective; struct Proposition* left; struct Proposition* right; } Proposition;
// 初始化命题变元 void initPropositionVariable(PropositionVariable* pv) { pv->value = 0; pv->isAssigned = 0; }
// 获取命题变元的值 int getPropositionVariableValue(PropositionVariable* pv) { if (pv->isAssigned) { return pv->value; } else { printf("命题变元未赋值!\n"); exit(1); } }
// 初始化命题公式 void initProposition(Proposition* prop) { prop->left = NULL; prop->right = NULL; }
// 释放命题公式的内存 void freeProposition(Proposition* prop) { if (prop != NULL) { freeProposition(prop->left); freeProposition(prop->right); free(prop); } }
// 获取命题公式的结果 int getPropositionResult(Proposition* prop, PropositionVariable* p, PropositionVariable* q, PropositionVariable* r) { if (prop == NULL) { printf("命题公式为空!\n"); exit(1); }
switch (prop->connective) {
case AND:
return getPropositionResult(prop->left, p, q, r) && getPropositionResult(prop->right, p, q, r);
case OR:
return getPropositionResult(prop->left, p, q, r) || getPropositionResult(prop->right, p, q, r);
case NOT:
return !getPropositionResult(prop->left, p, q, r);
case IMPLIES:
return !getPropositionResult(prop->left, p, q, r) || getPropositionResult(prop->right, p, q, r);
case EQUIVALENT:
return getPropositionResult(prop->left, p, q, r) == getPropositionResult(prop->right, p, q, r);
default:
printf("无效的逻辑联结词!\n");
exit(1);
}
}
// 输出真值表 void printTruthTable(Proposition* prop, PropositionVariable* p, PropositionVariable* q, PropositionVariable* r) { int i, j, k; printf("P\tQ\tR\t结果\n"); for (i = 0; i < 2; i++) { p->value = i; for (j = 0; j < 2; j++) { q->value = j; for (k = 0; k < 2; k++) { r->value = k; printf("%d\t%d\t%d\t%d\n", p->value, q->value, r->value, getPropositionResult(prop, p, q, r)); } } } }
// 判断命题公式类型 void determinePropositionType(Proposition* prop, PropositionVariable* p, PropositionVariable* q, PropositionVariable* r) { int isTautology = 1; // 永真式 int isContradiction = 1; // 不可满足式
int i, j, k;
for (i = 0; i < 2; i++) {
p->value = i;
for (j = 0; j < 2; j++) {
q->value = j;
for (k = 0; k < 2; k++) {
r->value = k;
int result = getPropositionResult(prop, p, q, r);
if (result == 0) {
isTautology = 0;
} else {
isContradiction = 0;
}
}
}
}
if (isTautology) {
printf("命题公式为永真式。\n");
} else if (isContradiction) {
printf("命题公式为不可满足式。\n");
} else {
printf("命题公式为可满足式。\n");
}
}
// 获取逻辑联结词 Connective getConnective() { int choice; printf("请选择逻辑联结词:\n"); printf("1. 与(AND)\n"); printf("2. 或(OR)\n"); printf("3. 非(NOT)\n"); printf("4. 蕴含(IMPLIES)\n"); printf("5. 等价(EQUIVALENT)\n"); scanf("%d", &choice);
switch (choice) {
case 1:
return AND;
case 2:
return OR;
case 3:
return NOT;
case 4:
return IMPLIES;
case 5:
return EQUIVALENT;
default:
printf("无效的选择!\n");
exit(1);
}
}
// 创建命题公式 Proposition* createProposition(PropositionVariable* p, PropositionVariable* q, PropositionVariable* r) { Proposition* prop = (Proposition*) malloc(sizeof(Proposition)); prop->connective = getConnective();
if (prop->connective == NOT) {
prop->left = createProposition(p, q, r);
prop->right = NULL;
} else {
prop->left = createProposition(p, q, r);
prop->right = createProposition(p, q, r);
}
return prop;
}
int main() { PropositionVariable p, q, r; initPropositionVariable(&p); initPropositionVariable(&q); initPropositionVariable(&r);
int choice;
do {
printf("请输入命题公式:\n");
Proposition* prop = createProposition(&p, &q, &r);
printf("真值表:\n");
printTruthTable(prop, &p, &q, &r);
determinePropositionType(prop, &p, &q, &r);
freeProposition(prop);
printf("是否继续输入命题公式?(1-是,0-否):");
scanf("%d", &choice);
} while (choice == 1);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/gTX 著作权归作者所有。请勿转载和采集!