In: Computer Science
Write a program in C++ that will output the truth table for a simple wff. There will only be Ps and Qs, so you can hard code the P truth table and the Q truth table into arrays. The user will use A for ^ , O for V, N for ' , & I for -> .
Hint: Read the first character, load the appropriate truth table into the first working array. Read the next character. If it is an N, output the negation of the first working array. If it is a P or Q, oad the appropriate truth table into the second working, array, then read the last character and output the appropriate truth table.
Expression Would be input as
P^Q PQA
P' PN
Q->P QPI
Example runs (note the user input is bold)
Run 1
Please input the wff: PQA
The truth table is
T
F
F
F
Run 2
Please input the wff: P'
The truth table is
F
F
T
T
#include<iostream>
using namespace std;
void And(char p[],char q[])//METHOD TO TRUTHTABLE OF AND
{
int i;
cout<<"The Truth table is:\n";
for(i=0;i<4;i++)
{
if(p[i]=='T'&&q[i]=='T')cout<<"T\n";
else cout<<"F\n";
}
}
void Implies(char p[],char q[])//METHOD TO TRUTHTABLE OF
->
{
int i;
cout<<"The Truth table is:\n";
for(i=0;i<4;i++)
{
if(p[i]=='T'&&q[i]=='F')cout<<"F\n";
else cout<<"T\n";
}
}
void Not(char p[])//METHOD TO TRUTHTABLE OF NOT
{
int i;
cout<<"The Truth table is:\n";
for(i=0;i<4;i++)
{
if(p[i]=='T')cout<<"T\n";
else cout<<"F\n";
}
}
int main()
{
char p[]={'T','T','F','F'},q[] =
{'T','F','T','F'};
string s;
cout<<"Please input the wff:";//taking
input..
cin>>s;
int i;
char *l1,*l2;
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='P'){
if(i==0)
l1=p;//loading array,
else
l2=p;//loading array,
}
else
if(s[i]=='Q')
{
if(i==0)
l1=q;//loading array,
else
l2=q; //loading array,
}
else if(s[i]=='A')
{
And(l1,l2); //method calling
}
else if(s[i]=='N')
{
Not(l1);//method
calling
}
else if(s[i]=='|')
{
Implies(l1,l2);//method calling
}
}
return 0;
}
ouput:-