In: Computer Science
C++:
Write a program that produces truth tables for the following compound propositions. Write the header of the tables, including intermedial steps and the final result. There should be three tables. Output the result on the file prog2 output.txt.
1. p&!(p|q)
2. (p|q)&!(p&q)
3. (p–>q)<->(!q–>!p)
NOTE: Do not hard code truth tables. The program must use binary or bitwase operators to compute the results.
Coding
#include <iostream>//header file in and out
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
bool p[4] = { true, true, false, false };//take variable as boolean
value
bool q[4] = { true, false, true, false };//take variable as boolean
value
ofstream myfile;
myfile.open ("output.txt");//make text file an output.txt
myfile<<endl<<"Table 1"<<endl;//write in text
file
myfile << "p | q" << " | " << "p&!(p|q)"
<< endl;
for (int i = 0; i < 4; i++)
{
myfile << setw(1) << p[i] << " |
";//write in text file
for (int j = 0; j < 1; j++)
{
myfile << setw(1) << q[i] << " |
";//write in text file
myfile << setw(3) << (p[i] &&
!(p[i]|q[i]));//write in text file
}
myfile << endl;
}
myfile<<endl<<"Table 2"<<endl;//write in text
file
myfile << "p | q" << " | " <<
"(p|q)&!(p&q)" << endl;//write in text file
for (int i = 0; i < 4; i++)
{
myfile << setw(1) << p[i] << " |
";//write in text file
for (int j = 0; j < 1; j++)
{
myfile << setw(1) << q[i] << " |
";
myfile << setw(3) << ((p[i]||q[i])
&& !(p[i]&q[i]));//write in text file
}
myfile << endl;
}
myfile<<endl<<"Table 3"<<endl;
myfile << "p | q" << " | " <<
"(p–>q)<->(!q–>!p)" << endl;/*we have to
elaborate this (p–>q)<->(!q–>!p) =*/
for (int i = 0; i < 4; i++)
{
myfile << setw(1) << p[i] << " |
";
for (int j = 0; j < 1; j++)
{
myfile << setw(1) << q[i] << " |
";//write in text file
myfile << setw(3) << (!((!p[i]||q[i]) ||
(q[i]||!p[i])&&(!(q[i]||!p[i]) ||(!p[i]||q[i]))));//write
in text file and simplicifcation in image file
}
myfile << endl;
}
myfile.close();
return 0;
}
output:
if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........