當前位置:首頁 » 編程語言 » 真值表寫c語言代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

真值表寫c語言代碼

發布時間: 2022-09-02 08:48:55

A. 真值表的如何用c語言表示

呵呵,簡單啊,你自定義一些符號為與 、或等,在菜單寫明就行代表什麼就行啊

B. 如何用C語言做出「構造命題公式的真值表」的程序~

const int p[]={0,0,1,1};
const int q[]={0,1,0,1};
int i;
printf("p 合取 q 結果\n");
for(i=0;i<4;i++)
{
if(p[i]==0 || q[i]==0)
{
printf("%d %d 0\n",p[i],q[i]);
}
else
{
printf("%d %d 1\n",p[i],q[i]);
}
}

printf("p 析取 q 結果\n");
for(i=0;i<4;i++)
{
if(p[i]==1 || q[i]==1)
{
printf("%d %d 1\n",p[i],q[i]);
}
else
{
printf("%d %d 0\n",p[i],q[i]);
}
}
printf("p 單條件 q 結果\n");
for(i=0;i<4;i++)
{
if(p[i]==1 && q[i]==0)
{
printf("%d %d 0\n",p[i],q[i]);
}
else
{
printf("%d %d 1\n",p[i],q[i]);
}
}
printf("p 雙條件 q 結果\n");
for(i=0;i<4;i++)
{
if((p[i]==1 && q[i]==1) || (p[i]==0 && q[i]==0))
{
printf("%d %d 1\n",p[i],q[i]);
}
else
{
printf("%d %d 0\n",p[i],q[i]);
}
}
//運行結果:

p 合取 q 結果
0 0 0
0 1 0
1 0 0
1 1 1
p 析取 q 結果
0 0 0
0 1 1
1 0 1
1 1 1
p 單條件 q 結果
0 0 1
0 1 1
1 0 0
1 1 1
p 雙條件 q 結果
0 0 1
0 1 0
1 0 0
1 1 1
//是這個意思嗎?

C. 如何用c編寫簡單的離散數學合式公式真值表

#include <stdio.h>

int main()

{
char p[10],q[10];
char pandq,porq;
puts("輸入P,Q的值(T或者F)");
scanf("%s%s",p,q);
if(p[0]=='T'&&q[0]=='T')pandq='T';
else pandq='F';
if(p[0]=='F'&&q[0]=='F')porq='F';
else porq='T';
printf("PVQ=%c P∧Q=%c\n",porq,pandq);
return 0;
}

/*
合式公式是什麼啊?
告訴我,我幫助你寫

如P→(PVQ)等等PVQ是當P和Q同時為F是PVQ為F,否則為T,P∧Q是當P和Q同時為T時P∧Q為T,否則為F,P→Q是當P為T,Q為F是P→Q為F,否則為T,P←→Q是當P和Q相同是為T,否則為F

PVQ
Q F T
P
F F T
T T T

P∧Q
Q F T
P
F F F
T F T
這樣行嗎,你畫一下表格吧
*/

D. 用C語言實現(p→q)→(!p∧!r)的真值表

摘要 (P∧(P->Q))->Q

E. 用C語言編程:輸入命題公式的合式公式,求出公式的真值表,並輸出該公式的主合取範式和主析取範式

A-Z + is OR * is AND _ is → # is⊕(圓圈裡加個+) @ is ⊙

$ is ↑ 命題的"與非" 運算( "與非門" )

% is ↓ 命題的"或非"運算( "或非門" )

Input the source formula:

A*!S+R

NORMALc: (!A*!B*!R)+(A*!B*!R)+(!A*B*!R)+(A*B*!R)+(!A*!B*R)+(!A*B*R)+(A*B*R)

NORMALd (!A+B+!R)

Error!

Input the source formula:

A#B

Here!

4countTerms

NORMALc: (A*!B)+(!A*B)

NORMALd (A+B)*(!A+!B)

Error!

Input the source formula:

stack<char> stk;

bool isVar(char ch)const;

void addMin(int minterm);

void addMax(int maxterm);

bool compute(int minterm);

void getInversePoland();

int countTerms(int n);

void assign(int minterm);

stack<bool> boolStk;

public:

formulaBase();

formulaBase(const formulaBase& rhs);

~formulaBase();

void getSource();

cout<<"Invalid input !"

"Operate again:"<<endl;

cin>>sourceFormula;*/

}

void formulaBase::getInversePoland()

{

char temp,temp1;

for(int i=0;sourceFormula[i]!='';i++)

stk.pop();

}

else break;

}

stk.push(temp);

break;

case '*':

while (!stk.empty())

//從鍵盤上任意輸入一個主析取範式,輸出與之等值的主合取範式。┐∧∨

# include <iostream>

# include <cmath>

# include <windows.h>

using namespace std ;

(5)真值表寫c語言代碼擴展閱讀:

(1)命題變項是命題公式。

(2)如果A是命題公式,則¬A是命題公式。

(3)如果A和B是命題公式,那麼(A∧B)、(A∨B)、(A→B)和(A↔B)都是命題公式。

(4)當且僅當有限次地應用(1),(2),(3)所得到的包含命題變項,聯結詞和圓括弧的符號串是命題公式。

命題公式的定義是一個遞歸定義形式。命題公式本身不是命題,沒有真值,只有對其命題變項進行賦值後,它才有真值。

5個聯結詞運算兒有不同的優先順序。當它們同時出現在一個命題公式里時,聯結間運算的優先次序為、∧、∨、→、,如果有括弧,則括弧內的運算優先進行。

F. C語言問題

因為 while 的循環條件是「真」也就是非0的數。x==0 說明當x=0的時候才循環,那麼那麼!0就是真,所以!x 和x==0一樣的。

G. 怎麼用C語言寫一個程序,判斷真值表的

//---------------------------------------------------------------------------

#include <stdio.h>

int main(void)
{
int p,q;
printf("%9c!P\t!Q\tP&Q\tP|Q\tP^Q\n");
for (p=0; p<2; p++) {
for (q=0; q<2; q++) {
printf("P=%d,Q=%d\t%3d\t%2d\t%2d\t%2d\t%2d\n",p,q,!p?1:0,!q?1:0,p&&q?1:0,p||q?1:0,p^q?1:0);
}
}
return 0;
}
//---------------------------------------------------------------------------

H. 如何用c語言實現真值表

已通過測試,望採納
其餘樓主可參照對應真值表補充
#include <stdio.h>
main()
{ int a=1,b=0;
printf("a=%d\tb=%d\n",a,b);
printf("a&&b=%d\n",a&b);
printf("a||b=%d\n",a||b);
printf("!a=%d\n",!a);
printf("a||(a&&b)=%d\n",a||(a&&b));
printf("a&&(a||b)=%d\n",a&&(a||b));
}

I. 用c語言怎麼寫真值表

用bool數組表示就可以。不用使用二叉樹什麼的。
你需要解決的有兩點:

如何將輸入的變數,編碼為數組的元素索引。
真值的運算過程是一系列關系操作,即與或非。

J. 輸入一個邏輯表達式,輸出它的真值表,要用c語言寫的

/*本程序支持任意輸入的邏輯表達式,可以進行與或非和蘊涵的運算,表達式中可帶括弧.bintree.h和stack.cpp為二叉樹和棧的定義及實現*/
#include
<stdio.h>
#include
<malloc.h>
#define
MAXNUM
100
//棧最大元素個數
#define
MAXEXP
30
//允許用戶輸入的表達式最大字元數
#include
"bintree.h"
#include
"stack.cpp"
const
char
and
=
'&',
or
=
'|',
then
=
'-';
bool
InOpt(char
c)
{
return
(c
==
'&'
||
c
==
'|'
||
c
==
'-'
||
c
==
'#');
}
bool
IsNum(char
c)
{
return
(c
>=
'0'
&&
c
<=
'9');
}
bool
IsAlp(char
c)
{
return
((c
<=
'z'
&&
c
>=
'a')
||
(c
>=
'A'
&&
c
<=
'Z'));
}
bool
CheckSyntax(char*
exp)
{
char*
cp
=
exp;
while
(*cp
!=
'\0')
{
if
(!(IsNum(*cp)
||
IsAlp(*cp)
||
InOpt(*cp)
||
*cp
==
'('
||
*cp
==
')'))
return
FALSE;
cp++;
}
if
(*(--cp)
!=
'#')
return
FALSE;
return
TRUE;
}
PBinTree
TransferTree(char
*exp)
{
PBinTreeNode
pbt
=
CrtBinTree();
Stack<BinTreeNode*>
st;
Stack<char>
sc;
char*
ch
=
exp,c;
sc.Push('#');
while
(!(sc.GetTop()==
'#'
&&
*ch
==
'#'))
{
if
(IsAlp(*ch))
{
PBinTreeNode
t
=
CrtBinTree();
t->data
=
*ch;
st.Push(t);
}
else
if
(IsNum(*ch))
{
while
(IsNum(*ch))
{
ch++;
}
ch--;
PBinTreeNode
t
=
CrtBinTree();
t->data
=
*ch;
st.Push(t);
}
else
{
switch
(*ch)
{
case
'(':
sc.Push(*ch);
break;
case
')':
{
c
=
sc.Pop();
while
(c
!=
'(')
{
PBinTreeNode
t
=
CrtBinTree();
t->data
=
c;
t->rChild
=
st.Pop();
t->lChild
=
st.Pop();
st.Push(t);
c
=
sc.Pop();
}
break;
}
default:
{
while
(sc.GetTop()
!=
'#'
&&
sc.GetTop()
!=
'(')
{
PBinTreeNode
t
=
CrtBinTree();
c
=
sc.Pop();
t->data
=
c;
t->rChild
=
st.Pop();
t->lChild
=
st.Pop();
st.Push(t);
}
if
(*ch
!=
'#')
sc.Push(*ch);
break;
}
}
}
if
(!sc.IsEmpty()
&&
*ch
!=
'#')
ch++;
}
pbt
=
st.Pop();
return
pbt;
}
void
GetVariable(PBinTree
pbt)
{
PBinTree
vpt
=
pbt;
if
((pbt->data
>=
'a'
&&
pbt->data
<=
'z')
||
(pbt->data
>=
'A'
&&
pbt->data
<=
'Z'))
{
printf
("請輸入%c的值(1或0):\n",vpt->data);
scanf
("%c",&vpt->data);
getchar();
}
if
(vpt->lChild
!=
NULL)
GetVariable(vpt->lChild);
if
(vpt->rChild
!=
NULL)
GetVariable(vpt->rChild);
}
char
Caculate(PBinTree
pbt)
{
PBinTree
vpt
=
pbt;
if
(vpt
==
NULL)
{
printf("沒有任何錶達式可計算!");
return
FALSE;
}
if
(vpt->lChild
==
NULL)
//找到葉子結點
return
vpt->data;
if
(InOpt(vpt->data)
&&
Caculate(vpt->lChild)
&&
Caculate(vpt->rChild))
{
switch(vpt->data)
{
case
and:
if
(Caculate(vpt->lChild)
==
'1'
&&
Caculate(vpt->rChild)
==
'1')
vpt->data
=
'1';
else
vpt->data
=
'0';
break;
case
or:
if
(Caculate(vpt->lChild)
==
'0'
&&
Caculate(vpt->rChild)
==
'0')
vpt->data
=
'0';
else
vpt->data
=
'1';
break;
case
then:
if
(Caculate(vpt->lChild)
==
'1'
&&
Caculate(vpt->rChild)
==
'0')
vpt->data
=
'0';
else
vpt->data
=
'1';
break;
}
}
return
vpt->data;
}
void
main()
{
char*
exp
=
(char*)
malloc
(sizeof(char)*MAXEXP);
printf("****************************************************************************\n");
printf("**
邏輯表達式計算器1.10
**\n");
printf("**
Created
by
YangPengfei([email protected])
**\n");
printf("**
本計算器前只支持或(|)、與(&)、非(!)以及蘊涵(-)運算
**\n");
printf("****************************************************************************\n");
printf("\n");
printf("請輸入需要計算的邏輯表達式(需要在表達式後加一個\"#\"號):\n");
gets(exp);
while
(!CheckSyntax(exp))
{
printf("表達式輸入錯誤,請重新輸入:\n");
gets(exp);
}
PBinTree
pbt
=
TransferTree(exp);
GetVariable(pbt);
printf
("這個邏輯表達式的值為:
%c
\n",Caculate(pbt));
}