Question

In: Computer Science

C++ Write a program that produces the truth table of the following logical operators. You aresupposed...

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 (<->)

Solutions

Expert Solution

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


Related Solutions

Write a C++ program to construct the truth table of P ∨¬(Q ∧ R) If you...
Write a C++ program to construct the truth table of P ∨¬(Q ∧ R) If you could include comments to explain the code that would be much appreciated!!! :) Thank you so much!
write a program that will print a truth table for p ^ ~q. Using C++ please.
write a program that will print a truth table for p ^ ~q. Using C++ please.
Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory...
Part 1 Write a C++ program in which you define the following logical functions: 1) contradictory function (¬?) 2) logical and (? ∧ ?) 3) logical inclusive or (? ∨ ?) 4) implication function (? → ?) 5) biconditional (? ↔ ?) 6) logical exclusive or (? ⨁ ?) You can find the logical definitions for the above functions as given by Bertrand Russell in your text. You need to write a method (function) for each corresponding logical function. The...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
1. write a truth table using this symbol: --> 2. write the inputs for the truth...
1. write a truth table using this symbol: --> 2. write the inputs for the truth table to the left of the --> and write the outputs for the truth table to the right of the --> 3. write the compliment, or NOT using ' As an example: The truth table for AND is written this way: A B --> A AND B 0 0 --> 0 0 1 --> 0 1 0 --> 0 1 1 --> 1 or...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub...
USING C++: Consider the precedence levels of the relational, logical, and arithmetic operators of the PySub language to be as follows (NOTE: 5 has highest precedence and 0 lowest): 5 *, /, % 4 +, - 3 <, <=, >, >=, !=, == 2 not 1 and 0 or 1.  Infix-Postfix Conversion and Evaluation with Logical and Relational operators – Convert the following infix expression to a postfix expression and evaluate the result (assume that true=1 and false=0). Provide both the...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table...
IN C++: Write a program to display the following table. MUST use for-loop Enter the table size: 10 N N^2 Square root of N --------------------------------------------- 1 1 1.00 2 4 1.41 3 9 1.73 ….. 10 100 3.16
(C++) Write a program that prints a table in the following format given a "x" read...
(C++) Write a program that prints a table in the following format given a "x" read from the keyboard. For example, if x is 4, it prints out 0 0 1 1 2 4 3 9 4 16 To avoid mismatch, put 8 white spaces between two numbers.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT