Question

In: Computer Science

Write a program in C++ that will output the truth table for a simple wff. There...

Write a program in C++ that will output the truth table for a simple wff. There will only be Ps and Qs, so you can hard code the P truth table and the Q truth table into arrays. The user will use A for ^ , O for V, N for ' , & I for -> .

Hint: Read the first character, load the appropriate truth table into the first working array. Read the next character. If it is an N, output the negation of the first working array. If it is a P or Q, oad the appropriate truth table into the second working, array, then read the last character and output the appropriate truth table.

Expression                Would be input as

P^Q                            PQA

P'                                PN

Q->P                           QPI

Example runs (note the user input is bold)

Run 1

Please input the wff: PQA

The truth table is

T

F

F

F

Run 2

Please input the wff: P'

The truth table is

F

F

T

T

Solutions

Expert Solution

#include<iostream>
using namespace std;
void And(char p[],char q[])//METHOD TO TRUTHTABLE OF AND
{
   int i;
   cout<<"The Truth table is:\n";
   for(i=0;i<4;i++)
   {
       if(p[i]=='T'&&q[i]=='T')cout<<"T\n";
       else cout<<"F\n";
          
   }
}
void Implies(char p[],char q[])//METHOD TO TRUTHTABLE OF ->
{
   int i;
   cout<<"The Truth table is:\n";
   for(i=0;i<4;i++)
   {
       if(p[i]=='T'&&q[i]=='F')cout<<"F\n";
       else cout<<"T\n";
          
   }
}
void Not(char p[])//METHOD TO TRUTHTABLE OF NOT
{
   int i;
   cout<<"The Truth table is:\n";
   for(i=0;i<4;i++)
   {
       if(p[i]=='T')cout<<"T\n";
       else cout<<"F\n";
          
   }
  
}
int main()
{
   char p[]={'T','T','F','F'},q[] = {'T','F','T','F'};
   string s;
   cout<<"Please input the wff:";//taking input..
   cin>>s;
   int i;
   char *l1,*l2;
  
   for(i=0;s[i]!='\0';i++)
   {
       if(s[i]=='P'){
           if(i==0)
               l1=p;//loading array,
           else l2=p;//loading array,
       }
       else if(s[i]=='Q')  
       {
           if(i==0)
               l1=q;//loading array,
           else l2=q;   //loading array,
       }
       else if(s[i]=='A')
       {
           And(l1,l2);   //method calling
       }
       else if(s[i]=='N')
       {
           Not(l1);//method calling
       }
       else if(s[i]=='|')
       {
           Implies(l1,l2);//method calling
       }
   }
  
   return 0;
}

ouput:-


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.
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, &)...
Write a simple C program to generate a chart of circle properties. Output chart “iteration” is...
Write a simple C program to generate a chart of circle properties. Output chart “iteration” is to be specified by user input. Your chart will include the radius, diameter, circumference, and Area values in a single table, and radius values will range from 0 to 50, floating point values, with three-decimal-place precision. Table should have all column entries right-justified and each column will have a heading above the entries. For example, assuming user enters a incrementation value of 5, the...
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...
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...
C++: Write a program that produces truth tables for the following compound propositions. Write the header...
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.
Write a C++ program that will output the multiplication table as show below: 1*1=1          2*1=2                 &nbsp
Write a C++ program that will output the multiplication table as show below: 1*1=1          2*1=2                   3*1=3         ……  9*1=1 1+2=2         2*2=4                   3*2=6          ……  9*2=18 …….           …….                   …….           ……  ……. 1*9=9         2*8=18                 3*9=27         ……  9*9=81
please write simple python 3 program with explanations and correct output Bilbo and problems on the...
please write simple python 3 program with explanations and correct output Bilbo and problems on the board Problem Statement Bilbo once reached his maths claws earlier than anyone else.He saw some N unique numbers greater than 0 written on the board.He thought of challenging his classmates when they come.So for each number i,he wrote the number before and after it on a paper slip.If it is the last number then the number after it is assumed to be 0.If it...
Write a C++ program to swap two numbers and show your output
Write a C++ program to swap two numbers and show your output
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT