您的位置:首页 > 其它

离散数学实践:常用逻辑联结词计算

2015-03-29 19:00 495 查看

准备知识

在命题逻辑中,五个常用逻辑联结词是最基本的概念,它的计算是后续合式公式值的计算的基础。
(1)    真值的表示:整数0表示真值为假,整数1表示真值为真。
(2)    否定逻辑联结词:当实参为0时,返回1,否则返回0。
(3)    合取逻辑联结词:当两个实参同时为1时,返回1,否则返回0。
(4)    析取逻辑联结词:当两个实参同时为0时,返回0,否则返回1。
(5)    条件逻辑联结词:当条件的前件为1,后件为0时,返回0,否则返回1。
(6)    双条件逻辑联结词:当双条件的左右同时为0,或同时为1时,返回1,否则返回0。

目的是将五个常用逻辑联结词的计算过程封装成五个函数,并测试简单的公式求值。以便熟悉五个常用逻辑联结词的基本概念,并编程求值。

参考代码

#include <stdio.h>

//Member funtions and define

int negation(const int p);
int disjunction(const int p, const int q);
int conjunction(const int p, const int q);
int biconditional(const int p, const int q);
int PropostionalCompute(const int p, const int q ,const char ch);

// Negation  function
int negation(const int p)
{
return p == 0 ? 1 : 0;
}

//Conjuciton function
int conjunction(const int p, const int q)
{
return (p != 0 && q != 0) ? 1 : 0;
}

//Disjunciton function
int disjunction(const int p, const int q)
{
return (p == 0 && q == 0) ? 0 : 1;
}

//Conditional function
int conditional(const int p, const int q)
{
return (p != 0 && q == 0) ? 0 : 1;
}

//Biconditional function
int biconditional(const int p, const int q)
{
return ((p == 0 && q == 0) || (p != 0 && q != 0)) ? 1 : 0;
}

// PropostionalCompute function: Choice  Propostional Connetive ,
// e.g.: negation, disjuction tec...
int PropostionalCompute(const int p, const int q, const char ch)
{
switch(ch)
{
case '~': return negation(q);
case '+': return disjunction(p, q);
case '*': return conjunction(p, q);
case '>': return conditional(p, q);
case '=': return biconditional(p, q);
default: return -1;
}
}

int main (){

int i ,j; // i,j is an integer
char  k; // k is a char

printf("Enter one Mumber: ");
scanf("%d", &i);
printf("Enter two Mumber: ");
scanf("%d", &j);
printf("Enter Propostional Connetive: ");
scanf("%c",&k);
if(k =='\n'){
scanf("%c",&k);
}

printf ("The Results is %d\n", PropostionalCompute(i,j,k));
return 0;
}


测试结果



关于Discrete Mathematics更多讨论与交流,敬请关注本博客和新浪微博songzi_tea.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息