In: Computer Science
I need this Java code translated into C Code. Thank you.
//Logical is the main public class
public class Logical {
public static void main(String args[]) {
char [][] table=
{{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}};
// table contains total combinations of p,q,& r
int totalTrue, totalFalse, proposition;
//proposition 1:
proposition=1;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
{
char o=
conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0]));
System.out.println(" "+table[i][0]+" "+table[i][1]+"
"+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
System.out.println(" ");
System.out.println(" ");
//proposition 2:
proposition=2;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
{
char o=
conjuctive(disjunctive(table[i][0],negation(table[i][1])),disjunctive(table[i][2],negation(implecation(table[i][0],table[i][1]))));
System.out.println(" "+table[i][0]+" "+table[i][1]+"
"+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
System.out.println(" ");
System.out.println(" ");
//Proposition 3
proposition=3;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
{
char o=
implecation(table[i][0],implecation(negation(conjuctive(table[i][0],negation(table[i][1]))),conjuctive(table[i][0],table[i][1])));
System.out.println(" "+table[i][0]+" "+table[i][1]+"
"+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
System.out.println(" ");
System.out.println(" ");
//Proposition 4
proposition=4;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
{
char o=
conjuctive(conjuctive(table[i][0],implecation(table[i][0],table[i][1])),negation(table[i][1]));
System.out.println(" "+table[i][0]+" "+table[i][1]+"
"+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
/* //Proposition 0
proposition=0;
start(0);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++)
{
{
char o=
conjuctive(table[i][0],conjuctive(table[i][1],negation(table[i][2])));
System.out.println(" "+table[i][0]+" "+table[i][1]+"
"+table[i][2]+" "+o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);*/
}
//method to check contradiction & tautology
static public void finalOutput(int totalTrue,int totalFalse,int
proposition)
{
System.out.println(" "+totalTrue+" combination result in
Proposition " +proposition+" being T");
System.out.println(" "+totalFalse+" combination result in
Proposition "+proposition+ " being F");
if(totalTrue==8)
System.out.println(" Proposition "+ proposition+ " is a
tautology");
else if(totalFalse==8)
System.out.println(" Proposition " + proposition+" is a
contradiction");
else
System.out.println(" Proposition "+proposition +" is neither a
tautology nor a contradiction");
}
static public void start(int i)
{
System.out.println(" "+"p"+" "+"q"+" "+"r"+" "+"Proposition "+
i);
System.out.println("----------------------------------------------------");
}
// negation method
static public char negation(char x)
{
if(x=='T')
return 'F';
else
return 'T';
}
// disjuctive method
static public char disjunctive(char x, char y)
{
if(x=='T' || y=='T')
return 'T';
else
return 'F';
}
// conjuctive method
static public char conjuctive(char x, char y)
{
if(x=='F' || y=='F')
return 'F';
else
return 'T';
}
// implecation method
static public char implecation(char x,char y)
{
if((x=='T' && y=='T') ||(x=='F' && y=='F') ||
(x=='F' && y=='T'))
return 'T';
else
return 'F';
}
}
Please find the answer below, all the details are mentioned in the comments. Conversion is done as per the code provided in the question.
Logical.c
#include<stdio.h>
//function declarations
void start();
char negation(char x);
char conjuctive(char x, char y);
char implecation(char x,char y);
void finalOutput(int totalTrue,int totalFalse,int
proposition);
char disjunctive(char x, char y);
//main method to execute the functions
void main(){
char table[8][3]=
{{'F','F','F'},{'F','F','T'},{'F','T','F'},{'F','T','T'},{'T','F','F'},{'T','F','T'},{'T','T','F'},{'T','T','T'}};
// table contains total combinations of p,q,& r
int totalTrue, totalFalse, proposition;
//proposition 1:
proposition=1;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++){
{
char o=
conjuctive(implecation(negation(table[i][0]),table[i][1]),implecation(table[i][2],table[i][0]));
printf(" %c %c %c
%c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
printf(" \n");
printf(" \n");
//proposition 2:
proposition=2;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++){
{
char o=
conjuctive(disjunctive(table[i][0],negation(table[i][1])),disjunctive(table[i][2],negation(implecation(table[i][0],table[i][1]))));
printf(" %c %c %c
%c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
printf(" \n");
printf(" \n");
//Proposition 3
proposition=3;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++){
{
char o=
implecation(table[i][0],implecation(negation(conjuctive(table[i][0],negation(table[i][1]))),conjuctive(table[i][0],table[i][1])));
printf(" %c %c %c
%c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
printf(" \n");
printf(" \n");
//Proposition 4
proposition=4;
start(proposition);
totalTrue=0;
totalFalse=0;
for(int i=0;i<8;i++){
{
char o=
conjuctive(conjuctive(table[i][0],implecation(table[i][0],table[i][1])),negation(table[i][1]));
printf(" %c %c %c
%c\n",table[i][0],table[i][1],table[i][2],o);
if(o=='T')
totalTrue++;
else
totalFalse++;
}
}
finalOutput(totalTrue,totalFalse,proposition);
}//main method end
//function definitions
//method to check contradiction & tautology
void finalOutput(int totalTrue,int totalFalse,int
proposition){
printf(" %d combination result in Proposition %d being
T\n",totalTrue,proposition);
printf(" %d combination result in Proposition %d being
F\n",totalFalse,proposition);
if(totalTrue==8)
printf(" Proposition %d is a tautology\n",proposition);
else if(totalFalse==8)
printf(" Proposition %d is a contradiction\n",proposition);
else
printf(" Proposition %d is neither a tautology nor a
contradiction\n",proposition);
}
void start(int i){
printf(" p q r Proposition %d\n",i);
printf("----------------------------------------------------\n");
}
// negation method
char negation(char x){
if(x=='T')
return 'F';
else
return 'T';
}
// disjuctive method
char disjunctive(char x, char y){
if(x=='T' || y=='T')
return 'T';
else
return 'F';
}
// conjuctive method
char conjuctive(char x, char y){
if(x=='F' || y=='F')
return 'F';
else
return 'T';
}
// implecation method
char implecation(char x,char y){
if((x=='T' && y=='T') ||(x=='F' && y=='F') ||
(x=='F' && y=='T'))
return 'T';
else
return 'F';
}
Output:
Please let us know in the comments if you face any problems.