Courtenay
Posts: 23
Nickname: courtz
Registered: Apr, 2003
|
|
Re: Char to boolean error
|
Posted: May 26, 2003 7:39 AM
|
|
Thanks for your swift reply. Basically i need to read in a proposition from a file - ie abc*~a~b~c where * is an and (&) sign ~ is a not (!) sign and the letters are all or(ed) (||) so basically i need to find out whether the above sentence is true or false. in this case it would be: a or b or c and not a or not b or not c The textfile that we must use looks like this: 3 2 abc*~a~b~c*
where 3 is the number of variables and 2 the number of clauses.
here is my code so far:
import java.io.*;
public class SATProblem{ private int variable; private int clause; private char[] data; private void Stringreader(){ String line = ""; String file = "testfile.txt"; try { FileReader fr = new FileReader(file); BufferedReader inFile = new BufferedReader(fr); variable = Integer.parseInt(inFile.readLine()); clause = Integer.parseInt(inFile.readLine()); data = (inFile.readLine()).toCharArray(); //data = (inFile.readLine()).toCharArray(); inFile.close(); } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException e) { System.out.println(e); } System.out.println(data); System.out.println(variable); System.out.println(clause); } public boolean getBoolean(int n, int pos){ pos = variable - pos; int temp = (int)(n/Math.pow(2,(pos-1))% 2); if (temp == 1) return true; else return false; } private void startAnalysis(){ boolean test[] = new boolean[variable]; boolean answer = false; for (int n = 0; n < Math.pow(2, variable); n++){ for (int pos = 0; pos < variable; pos++){ test [pos] = getBoolean(n,pos); } if (validateSent(test)){ answer = true; break; } } } public boolean validateSent(boolean test[]){ int count = 0; while (count <= variable){ for (int i = 0; i <= data.length; i++){ if (data[i] != '~'){ if (test[count]) data[i] = true; } } } } public static void main (String[] args){ SATProblem abc = new SATProblem(); abc.Stringreader(); abc.startAnalysis(); } }
startAnalysis and getBoolean basically construct the truth table where for three variables when n = 0,test[0] = false, test[1] = false, test[2] = false when n = 1,test[0] = false, test[1] = false, test[2] = true when n = 1,test[0] = false, test[1] = true, test[2] = true ETC you get the idea. Do u think u could help? Thanks a lot again COurtz
|
|