In: Computer Science
create a program in java that will evaluate a logical expression (compound proposition) based on the given truth values of individual propositional variables. The logical expression may include the logical AND or the logical OR operators. The NOT operator will be included in the variable names itself. So, a proposition such as ¬a would appear as na in the logical expression.
Here is an example of a logical expression using proper notation:
a ∧ b ∨ ¬c ∨ d
This expression will be represented as the following in this program to accommodate simple keyboard input:
a and b or nc or d
The logical operators will be evaluated left to right. Parentheses will not be included.
input the following information:
• number of variables in an expression, between 2 and 5
• the truth values of each of these variables
• a logical expression involving these variables, their negated counterparts and the logical operators and/or
Below is the code for the above problem. I have implement the solution in java language as no language was specified
I have also provided some sample test cases below the code
//Importing Scanner class for taking input from the user
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//taking Count of variable or operands from user
System.out.println("Enter the number of variables (must be between 2 and 5 both inclusive):");
int variable_count = sc.nextInt();
//calculation Count of operators using operands
int operator_count = variable_count - 1;
//Taking boolean value for each operator and storing in array declared below
System.out.println("Enter the Boolean values for the variables (True/False):");
boolean truth_value_arr[] = new boolean[variable_count];
for(int i=0; i<variable_count; i++) {
truth_value_arr[i] = sc.nextBoolean();
}
// this is basically used to clear the input buffer for taking a string input from user
sc.nextLine();
//Take the logical expression to be evaluated from the user
System.out.println("Enter the logical expression to be evaluated:");
/*
I have taken the expression and divided the tokens at space and stored it in an array
eg. if expression is "a and nb or c" then split operation will generate tokens as
"a", "and" , "nb", "or", "c".
Then these tokens are stored in the array below.
*/
String [] express = sc.nextLine().split(" ");
/*
Here i am preprocessing the token to evaluate all the operands who have "n" in front of them.
This means that all the operands which are having a not operation are evaluated here.
*/
for(int i=0; i<express.length;i=i+2) {
if(express[i].startsWith("n")) {
truth_value_arr[i/2] = !truth_value_arr[i/2];
}
}
//Taken a temp variable to store the intermediate result and also the final result at the end.
boolean temp = true;
int operator_index = 1;
/*Here i have evaluated the first expression from left to right which means first two variables and the operand
and the result is stored in the temp variable
eg. if expression is "a and b or c" then "a and b" is evaluated here below.
*/
if(express[operator_index].equals("and")) {
temp = truth_value_arr[0] && truth_value_arr[1];
}
else if(express[operator_index].equals("or")) {
temp = truth_value_arr[0] || truth_value_arr[1];
}
/*
this is incremented by 2 as logical operators are always at odd
position in expression array as we start index from 0 and we started operator_index at 1
*/
operator_index = operator_index+2;
/*
In this for loop i have evaluated the rest of the logical expression.
the loop runs for operator_count number of times as the
expression will be evaluated for the number operators are there in the expression
*/
for(int i=1; i<operator_count;i++) {
if(express[operator_index].equals("and")) {
temp = temp && truth_value_arr[i+1];
} else if(express[operator_index].equals("or")) {
temp = temp || truth_value_arr[i+1];
}
operator_index = operator_index+2;
}
//At last i print the result of expression based on value present in the temp variable
if(temp) {
System.out.println("Result of given logical expression after evaluation: True");
} else{
System.out.println("Result of given logical expression after evaluation: False");
}
}
}
Some Sample Test cases executed on above code