真值表編程
『壹』 怎麼用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;
}
//---------------------------------------------------------------------------
『貳』 用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]!='