In: Computer Science
#include <iostream>
#include "lib.hpp"
using namespace std;
int main() {
// declare the bool
bool a = true;
bool b= true;
//Print the Conjunction function
cout<<"\n\nConjunction Truth Table -"<<endl;
cout<< "\nP\tQ\t(P∧Q)" <<endl;
cout<< a <<"\t"<< b <<"\t"<< conjunction(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<< conjunction(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<< conjunction(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<< conjunction(!a,!b)<<endl;
//Print the Disjunction function
cout<<"\n\nDisjunction Truth Table -"<<endl;
cout<< "\nP\tQ\t(PVQ)" <<endl;
cout<< a <<"\t"<< b <<"\t"<< disjunction(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<< disjunction(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<< disjunction(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<< disjunction(!a,!b)<<endl;
//Print the ExclusiveOr function
cout<<"\n\nExclusiveOr Truth Table -"<<endl;
cout<< "\nP\tQ\t(P⊕Q)" <<endl;
cout<< a <<"\t"<< b <<"\t"<< exclusiveOr(a,b) <<endl;
cout<< a <<"\t"<< !b <<"\t"<< exclusiveOr(a,!b) <<endl;
cout<< !a <<"\t"<< b <<"\t"<< exclusiveOr(!a,b) <<endl;
cout<< !a <<"\t"<< !b <<"\t"<< exclusiveOr(!a,!b)<<endl;
//Print the Negation function
cout<<"\n\nNegation Truth Table -"<<endl;
cout<< "\nP\t~P" <<endl;
cout<< !a <<"\t" << negation(!a)<<endl;
cout<< a <<"\t" << negation(a) <<endl;
How can u do this code Using the enum keyword or a C++ class? To create a new type Boolean with the two values F and T defined.
C++ CODE USING C++ CLASS AND NEW TYPE BOOLEAN VALUES F AND T :-
#include <iostream>
using namespace std;
//functions for conjunction
int conjunction(int a,int b){
if(a==1 && b==1)
return 1;
else
return 0;
}
//functions for disjunction
int disjunction(int a,int b){
if(a==0 && b==0)
return 0;
else
return 1;
}
//functions for eclusive or
int exclusiveOr(int a,int b){
if(a==b)
return 0;
else
return 1;
}
//functions for negation
int negation(int a){
if(a==1)
return 0;
else
return 1;
}
// declare the bool
bool T =true;
bool F=true;
//class for conjunction
class con{
public:
void display(){
//Print the
Conjunction function
cout<<"\n\nConjunction Truth Table -"<<endl;
cout<<
"\nP\tQ\t(P?Q)" <<endl;
cout<< T
<<"\t"<< F <<"\t"<< conjunction(T,F)
<<endl;
cout<< T
<<"\t"<< !F <<"\t"<< conjunction(T,!F)
<<endl;
cout<< !T
<<"\t"<< F <<"\t"<< conjunction(!T,F)
<<endl;
cout<< !T
<<"\t"<< !F <<"\t"<<
conjunction(!T,!F)<<endl;
}
};
//class for disjunction
class dis{
public:
void display(){
//Print the
Disjunction function
cout<<"\n\nDisjunction Truth Table -"<<endl;
cout<<
"\nP\tQ\t(PVQ)" <<endl;
cout<< T
<<"\t"<< F <<"\t"<< disjunction(T,F)
<<endl;
cout<< T
<<"\t"<< !F <<"\t"<< disjunction(T,!F)
<<endl;
cout<< !T
<<"\t"<< F <<"\t"<< disjunction(!T,F)
<<endl;
cout<< !T
<<"\t"<< !F <<"\t"<<
disjunction(!T,!F)<<endl;
}
};
//class for exclusive or
class exclu{
public:
void display(){
//Print the
ExclusiveOr function
cout<<"\n\nExclusiveOr Truth Table -"<<endl;
cout<<
"\nP\tQ\t(P?Q)" <<endl;
cout<< T
<<"\t"<< F <<"\t"<< exclusiveOr(T,F)
<<endl;
cout<< T
<<"\t"<< !F <<"\t"<< exclusiveOr(T,!F)
<<endl;
cout<< !T
<<"\t"<< F <<"\t"<< exclusiveOr(!T,F)
<<endl;
cout<< !T
<<"\t"<< !F <<"\t"<<
exclusiveOr(!T,!F)<<endl;
}
};
//class for negation
class neg{
public:
void display(){
//Print the
Negation function
cout<<"\n\nNegation Truth Table -"<<endl;
cout<<
"\nP\t~P" <<endl;
cout<< !T
<<"\t" << negation(!T)<<endl;
cout<< T
<<"\t" << negation(T) <<endl;
}
};
int main() {
//creating objects for classes
con ob1;
dis ob2;
exclu ob3;
neg ob4;
//calling methods in classes to display the true
tables
ob1.display();
ob2.display();
ob3.display();
ob4.display();
return 0;
}
OUTPUT:-