Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

How to check the given formula is tautology or not using recursion?

$
0
0
Hello.
I am trying to do some exersices about tautology checking. I can check it using truth table, but I have problem with writing code. Please give me sum hints. how can I start to think and write etc?
I have this formula : "((A|(B|C))<->((A|B)|C))";
And I have to write two recursive functions. One which computes the truth value
of a formula for a given truth value assignment of the variables that it contains.
One simple way to pass the variable value assignments is to pass two strings to
it, one with variable names and one with truth values. For instance, the strings
"DEA" and "TFF" means that D is true and E and A are false.The other recursive method should be something which
enumerates all possible truth value assignments, which are 2n for n variables.
For each assignment it shoudl use the other recursive function to compute the
value.

I wrote this code, but It does not work properly.
public static boolean isTautology(String str, int low, int high){
if (high <= low)
return true;
else if(str.charAt(low) != str.charAt(high))
return false;
else
return isTautology(str, low + 1, high - 1);
}


public static void main(String[] args) {
  Formula f = Formula.parse(args[0]);
  System.out.println(f.toString());
  System.out.println(formulaVars(f));
  System.out.println(" ((A|(B|C)) || ((A|B)/>/>|C))" + " " + "It is tautology" + " / " + isTautology (" it is Tautology", 0, 0) );
}
}



this is for check the value
public static void checkValue(String s, int n) {
		if (n == 0){
			System.out.println(s);
		}else{
			checkValue(s + "T", n+1);
			checkValue(s + "F", n-1);
			
		}
	}
	public static void Value(int n){
		checkValue(" ", n);
	}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>