In: Computer Science
C++
Write a program that produces the truth table of the following logical operators. You aresupposed to output one table with all the operators (one column for each operator). Write theheader of the table - this is the name of the columns-. Output the result on the file prog1 output.txt. The table should contain the letters T and F, it should NOT print 1s and 0s. Show theresults on the following order:
1. negation (!)
2. disjunction (AND operator, &)
3. conjunction (OR operator, |)
4. exclusive or (or but not both, ^)
5. implication (CONDITIONAL operator, ->)
6. biconditional (<->)
#include<stdio.h>
#include<iostream>
#include <fstream>
#include <cstdlib>
// for exit function
using namespace std;
using std::cerr;
using std::endl;
using std::ofstream;
int main()
{
int i,a,b,temp,p,r;
char x,y,z,q,s;
ofstream outdata;
// outdata
is like cin
outdata.open("prog1output.txt");
// opens
the file
if( !outdata )
{ // file couldn't be opened
cerr << "Error: file could not be opened"
<< endl;
exit(1);
}
outdata<<"\tA B \tneg(A) \tneg(B) \tConj \tDisj
\tXor \t\tBicon \t\tCond"<<endl; // writing to file
for(i=0;i<4;i++)
{
temp=i;
a=i%2;
temp/=2;
b=temp%2;
p=!a;
if(p==1)
q='T';
else
q='F';
r=!b;
if(r==1)
s='T';
else
s='F';
int c=a&&b;
if(c==1)
x='T';
else
x='F';
int d=a||b;
if(d==1)
y='T';
else
y='F';
int e=a^b;
if(e==1)
z='T';
else
z='F';
outdata<<endl<<"\t"<<b<<"
"<<a<<"\t"<<s<<"\t"<<q<<"\t"<<x<<"\t"<<y<<"\t"<<z;
// writing to file
if(a==b){
outdata<<"\t\t"<<"T";
// writing to file
}
else
outdata<<"\t\t"<<"F";
// writing to file
if(a==b){
outdata<<"\t\t"<<"T";
// writing to file
}
else{
if(a==1
&& b==0){
outdata<<"\t\t"<<"F";
// writing to file
}
else{
outdata<<"\t\t"<<"T";
// writing to file
}
}
}
outdata.close();
//closing
file
}