In: Computer Science
In C++, Create a program that can try out every possible logical combination of the variables A, B, and C, and determine which combinations will yield a true statement. Take note that there are eight different possible combinations of the three variables. Make certain you test all eight of the combinations.
(1) (A and B) or (A and C)
(2) (A and C) and (B and !C)
(3) (A or B) and !(B or C)
(4) (A or (!A and C)) and (!A and !B)
(5) ((B and C) or (C and A)) and ((A or B) and C)
#include<iostream>
using namespace std;
int main()
{
cout<<"(1)Testing: expression : (A and B) or (A
and C)\n";
bool a[] = {true,false};
int c=0;
cout<<" A | B | C | (A and B) or (A and C)\n";
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
{
bool A = a[i];
bool B = a[j];
bool C= a[k];
bool result =( (A && B) || (A &&
C) );//(A and B) or (A and C)
if(result)c++;
cout<<boolalpha<<A<<"|";
cout<<boolalpha<<B<<"|";
cout<<boolalpha<<C<<"|";
cout<<boolalpha<<result<<endl;
}
if(c==8)cout<<"(A and B) or (A and C) is True
Statement\n";
c=0;
cout<<"(2)Testing: expression : (A and C) and (B
and !C)\n";
cout<<" A | B | C | (A and C) and (B and !C)\n";
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
{
bool A = a[i];
bool B = a[j];
bool C= a[k];
bool result =( (A && C) || (B &&
!C) );// (A and C) and (B and !C)
if(result)c++;
cout<<boolalpha<<A<<"|";
cout<<boolalpha<<B<<"|";
cout<<boolalpha<<C<<"|";
cout<<boolalpha<<result<<endl;
}
if(c==8)cout<<" (A and C) and (B and !C) is True
Statement\n";
c=0;
cout<<"(3)Testing: expression
: (A or B) and !(B or C)\n";
cout<<" A | B | C | (A or B) and !(B or C)\n";
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
{
bool A = a[i];
bool B = a[j];
bool C= a[k];
bool result =( (A || B) && !(B || C));//
(A or B) and !(B or C)
if(result)c++;
cout<<boolalpha<<A<<"|";
cout<<boolalpha<<B<<"|";
cout<<boolalpha<<C<<"|";
cout<<boolalpha<<result<<endl;
}
if(c==8)cout<<" (A or B) and !(B or C) is True
Statement\n";
c=0;
cout<<"(4)Testing: expression
: (A or (!A and C)) and (!A and !B)\n";
cout<<" A | B | C | (A or (!A and C)) and (!A and
!B)\n";
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
{
bool A = a[i];
bool B = a[j];
bool C= a[k];
bool result =( (A || (!A && C)) and (!A
&& !B) );// (A or (!A and C)) and (!A and !B)
if(result)c++;
cout<<boolalpha<<A<<"|";
cout<<boolalpha<<B<<"|";
cout<<boolalpha<<C<<"|";
cout<<boolalpha<<result<<endl;
}
if(c==8)cout<<" (A or (!A and C)) and (!A and
!B) is True Statement\n";
c=0;
cout<<"(5)Testing: expression
: ((B and C) or (C and A)) and ((A or B) and C)\n";
cout<<" A | B | C | ((B and C) or (C and A)) and ((A or B)
and C)\n";
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
{
bool A = a[i];
bool B = a[j];
bool C= a[k];
bool result =( ((B && C) || (C
&& A)) && ((A || B) && C) );// ((B and C)
or (C and A)) and ((A or B) and C)
if(result)c++;
cout<<boolalpha<<A<<"|";
cout<<boolalpha<<B<<"|";
cout<<boolalpha<<C<<"|";
cout<<boolalpha<<result<<endl;
}
if(c==8)cout<<"((B and C) or (C and A)) and ((A
or B) and C) is True Statement\n";
c=0;
}
output:
(1)Testing: expression : (A and B) or (A and C)
A | B | C | (A and B) or (A and C)
true|true|true|true
true|true|false|true
true|false|true|true
true|false|false|false
false|true|true|false
false|true|false|false
false|false|true|false
false|false|false|false
(2)Testing: expression : (A and C) and (B and !C)
A | B | C | (A and C) and (B and !C)
true|true|true|true
true|true|false|true
true|false|true|true
true|false|false|false
false|true|true|false
false|true|false|true
false|false|true|false
false|false|false|false
(3)Testing: expression : (A or B) and !(B or C)
A | B | C | (A or B) and !(B or C)
true|true|true|false
true|true|false|false
true|false|true|false
true|false|false|true
false|true|true|false
false|true|false|false
false|false|true|false
false|false|false|false
(4)Testing: expression : (A or (!A and C)) and (!A and !B)
A | B | C | (A or (!A and C)) and (!A and !B)
true|true|true|false
true|true|false|false
true|false|true|false
true|false|false|false
false|true|true|false
false|true|false|false
false|false|true|true
false|false|false|false
(5)Testing: expression : ((B and C) or (C and A)) and ((A or B) and
C)
A | B | C | ((B and C) or (C and A)) and ((A or B) and C)
true|true|true|true
true|true|false|false
true|false|true|true
true|false|false|false
false|true|true|true
false|true|false|false
false|false|true|false
false|false|false|false
Process exited normally.
Press any key to continue . . .