When I was taking MATH 381, Discrete Mathematics while I was a senior in high school, there ended up being more "busy work" than I expected, which was filling in truth tables. Basically evaluating, for every possible configuration of true/false variables, what the full statement evaluates to. I decided to write a little parser that determines it for you.
read moreSample output:
How many variables do you have?
3
How many other statements do you have?
0
Enter your next variable name
p
Enter your next variable name
q
Enter your next variable name
r
p q r
true true true
true false true
true true false
true false false
false true true
false false true
false true false
false false false
Sample output 2:
Input: ((p|~q)&r)|p
truetruetrue
truetruefalse
truefalsetrue
truefalsefalse
falsetruetrue
falsetruefalse
falsefalsetrue
falsefalsefalse
true
true
true
true
false
false
true
false
truthEval.java
import java.util.ArrayList;
import java.util.Scanner;
public class truthEval {
public boolean evaloriginal(String statement,boolean p,boolean q, boolean r){
boolean returnstatement=false;
char characters[] = new char[statement.length()];
statement.getChars(0, statement.length(), characters, 0);
int opens = 0;
int closes = 0;
for(int i=0; i<characters.length;i++){
if(characters[i]=='('){
opens++;
}
if(characters[i]==')'){
closes++;
}
if(opens==closes&&(characters[i]=='&'||characters[i]=='|')){
String left= statement.substring(0, i);
if(left.startsWith("(")){
left = (String) left.subSequence(1, left.length());
}
if(left.endsWith(")")){
left = (String) left.subSequence(0, left.length()-1);
}
String right=statement.substring(i+1,statement.length());
if(right.startsWith("(")){
right = (String) right.subSequence(1, right.length());
}
if(right.endsWith(")")){
right = (String) right.subSequence(0, right.length()-1);
}
char junction = characters[i];
returnstatement= eval(left, right, junction, p, q, r);
}
}
return returnstatement;
}
public boolean eval(String left, String right, char junction, boolean p,boolean q, boolean r){
boolean leftTruth=true;;
boolean rightTruth=true;
if(nested(left,p,q,r)){
leftTruth = evaloriginal(left,p,q,r);
}
if(nested(right,p,q,r)){
rightTruth = evaloriginal(right,p,q,r);
}
if(left.equals("p")){
leftTruth=p;
}
if(left.equals("~p")){
leftTruth=!p;
}
if(right.equals("p")){
rightTruth=p;
}
if(right.equals("~p")){
rightTruth=!p;
}
if(left.equals("q")){
leftTruth=q;
}
if(left.equals("~q")){
leftTruth=!q;
}
if(right.equals("q")){
rightTruth=q;
}
if(right.equals("~q")){
rightTruth=!q;
}
if(left.equals("r")){
leftTruth=r;
}
if(left.equals("~r")){
leftTruth=!r;
}
if(right.equals("r")){
rightTruth=r;
}
if(right.equals("~r")){
rightTruth=!r;
}
if(junction=='>'&&(leftTruth&&!rightTruth)){
return false;
}
if(junction=='>'&&(leftTruth&&rightTruth)){
return true;
}
if(junction=='>'&&(!leftTruth)){
return true;
}
if(junction=='&'&&(rightTruth&&leftTruth)){
return true;
}
if(junction=='|'&&(rightTruth||leftTruth)){
return true;
} else{
return false;
}
}
public boolean nested(String side, boolean p,boolean q, boolean r){
boolean returnvalue = false;
char characters[] = new char[side.length()];
side.getChars(0, side.length(), characters, 0);
int opens = 0;
int closes = 0;
for(int i=0; i<characters.length;i++){
if(characters[i]=='('){
opens++;
}
if(characters[i]==')'){
closes++;
}
if(opens==closes&&(characters[i]=='&'||characters[i]=='|')){
returnvalue=true;
}
}
return returnvalue;
}
}
truthTableCreator.java
import java.util.Scanner;
public class truthTableCreator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("How many variables do you have?");
int x= scan.nextInt();
System.out.println("How many other statements do you have?");
int y= scan.nextInt();
int number=1;
for(int i=0;i<x;i++){
number=number*2;
}
String truthTable[][]= new String [number+1][y+x];
for(int i=0;i<x;i++){
System.out.println("Enter your next variable name");
truthTable[0][i]=scan.next();
}
for(int i=x;i<y+x;i++){
System.out.println("Enter your next statement");
truthTable[0][i]=scan.next();
}
int incrementor=1;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
if(i==0){
truthTable[incrementor][0]="true";
}
else{
truthTable[incrementor][0]="false";
}
if(k==0){
truthTable[incrementor][1]="true";
}
else{
truthTable[incrementor][1]="false";
}
if(j==0){
truthTable[incrementor][2]="true";
}
else{
truthTable[incrementor][2]="false";
}
incrementor++;
}
}
}
int a=0;
while(a<y+x){
System.out.print(" "+truthTable[0][a]);
a++;
}
for(int i=1;i<number+1;i++){
System.out.println();
for(int j=0;j<y+x;j++){
System.out.print(" "+truthTable[i][j]);
}
}
}
}
truthTableGivenStatement.java
public class truthTableGivenStatement {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
truthEval eval = new truthEval();
boolean truthTable[][]= new boolean [9][3];
int incrementor=0;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
if(i==0){
truthTable[incrementor][0]=true;
}
else{
truthTable[incrementor][0]=false;
}
if(j==0){
truthTable[incrementor][1]=true;
}
else{
truthTable[incrementor][1]=false;
}
if(k==0){
truthTable[incrementor][2]=true;
}
else{
truthTable[incrementor][2]=false;
}
System.out.print(truthTable[incrementor][0]);
System.out.print(truthTable[incrementor][1]);
System.out.print(truthTable[incrementor][2]);
System.out.println();
incrementor++;
}
}
}
for(int i=0;i<8;i++){
System.out.println(eval.evaloriginal("((p|~q)&r)|p", truthTable[i][0], truthTable[i][1],truthTable[i][2]));
}
}
}