Question

In: Computer Science

#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...

#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.

Solutions

Expert Solution

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:-


Related Solutions

#include <iostream> #include "lib.hpp" using namespace std; int main() {    // declare the bool bool...
#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;...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;...
#include<iostream> using namespace std; int main() {    int number_resistors;    double Resistors;    double series;    double parellel;    cout << "how many resistors: ";    cin >> number_resistors;    while (number_resistors != 0)    {        cout << "Enter the values of" << number_resistors << " resistors ";        for (int i = 0; i < number_resistors; i++) {            cin >> Resistors;            series += Resistors;                parellel...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char...
write the algorithm for this the code?!. #include<iostream> using namespace std; #include<string.h> int main() { char plain[50], cipher[50]="", decrypt[50]=""; int subkeys[50], len;       cout<<"Enter the plain text:"<<endl; cin>>plain;    cout<<"Enter the first subkey:"<<endl; cin>>subkeys[0];    _strupr(plain);    len = strlen(plain);    /**********Find the subkeys**************/    for(int i=1; i<len; i++) { if ((plain[i-1]>='A') && (plain[i-1]<='Z')) { subkeys[i] = plain[i-1]-65; } }    /****************ENCRYPTION***************/       for(int i=0; i<len; i++) { if ((plain[i]>='A') && (plain[i]<='Z')) {    cipher[i] = (((plain[i]-65)+subkeys[i])%26)+65; }...
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled          &nbsp
#include <iostream> #include <iomanip> using namespace std; int main() {             float miles;   //miles traveled             float hours;   //time in hours             float milesPerHour; //calculated miles per hour             cout << "Please input the Miles traveled" << endl;             cin >> miles;             cout << "Please input the hours traveled" << endl;             cin >> hours;                         milesHours = miles / hours; cout << fixed << showpoint << setprecision(2);             cout << "Your speed is " <<...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char**...
complete the program #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main(int argc, char** argv) { int number, sum, count; // Write a while loop that reads a number from the user and loop // until the number is divisible by 7 cout << "What is the number? "; cin >> number; while ( ... ) { ... } cout << number << " is divisible by 7!! << endl << endl; // Write a for loop that...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() {...
#include <iostream> #include <string> #include <fstream> #include <vector> #include <sstream> using namespace std; int main() { ifstream infile("worldpop.txt"); vector<pair<string, int>> population_directory; string line; while(getline(infile, line)){ if(line.size()>0){ stringstream ss(line); string country; int population; ss>>country; ss>>population; population_directory.push_back(make_pair(country, population)); } } cout<<"Task 1"<<endl; cout<<"Names of countries with population>=1000,000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second>=1000000000){ cout<<population_directory[i].first<<endl; } } cout<<"Names of countries with population<=1000,000"<<endl; for(int i=0;i<population_directory.size();i++){ if(population_directory[i].second<=1000000){ cout<<population_directory[i].first<<endl; } } } can u pls explain the logic behind this code up to 10 lines pls, many thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT