Question

I'm trying to do CNF operator in Java and I have an error with equality.

Firstly, I did most of the software but I didn't do totally.

My codes are here :

import acm.program.*;
public class split extends ConsoleProgram{
public void run()
{
    String veri     = "(p or q or s or t or k) and (p or q)";
    String yeni     = tekrarsil(parcala(veri));

    int []sayilar = new int[yeni.length()];
    for (int i = 0; i < yeni.length(); i++) {
        sayilar[i] = readInt("give a number for "+yeni.charAt(i)+" :");

    }
    for (int i = 0; i < sayilar.length; i++) {
        println(sayilar[i]);
    }
}

public String parcala(String veri)
{
    String yenistr  = "";
    String yeni[]   = veri.split("and");
    for (int j = 0; j < yeni.length; j++) {
        String yveri[] = yeni[j].split("or");
        for (int i = 0; i < yveri.length; i++) {
            yveri[i] = yveri[i].trim();
            if(i==0){
                yenistr = yenistr.concat(yveri[i].substring(1));

            }else if(i==yveri.length-1){
                yenistr = yenistr.concat(yveri[i].substring(0,yveri[i].length()-1));

            }else{
                yenistr = yenistr.concat(yveri[i]);

            }
        }
    }
    return (yenistr);
}
public String tekrarsil(String S)
{
        for (int i = 0; i < S.length(); i++)
            for (int k = i+1; k <= S.length()-1; k++){
                if (S.charAt(i) == S.charAt(k))
                {
                    S = S.substring(0,k)+ S.substring(k+1,S.length());
                    k--;
                }
            }
        return S;

     }
    }

The problem is the equality.

The program should check the variables is true or false.

For instance :

(p or q or s or t or k) and (q or p)

this program does like bellow:

give a number for p : 1(user will give a number one (true) or zero(false))
give a number for q : 0
give a number for s : 1
give a number for t : 0
give a number for k : 0
(if the variable use more than one, program will ask once)

If we look at these variables, we can see that (1 or 0 or 1 or 0 or 0) and (1 or 0) It will return true but I can't do this. I can't understand how to do it.

Best wishes.

Was it helpful?

Solution

How about this solution with some modifications to fit your current needs?

Or, as a crude workaround, you could use the Rhino as a JavaScript engine, simply change textual representation of operations to operators like &&, ||, etc. and have the JS engine evaluate and give you the results.

If you are interested only in the output values of the expressions, the above should be sufficient.

If you need to devise your own algorithm, than you'll need to write a parser yourself which is a tedious work, but feasible.

Let the user enter all the expression along with the operators (it will not make that much of a different from an implementation point of view). You'll of course need to validate the input. Data you'll need to deal with are either 0, 1, parentheses (which trigger result stacking/unstacking) and operators themselves (well, additionally, there are whitespaces, but you should simply ignore them).

Basically, evaluating from the left to right, you take the first data parameter (either 0, 1) and put it in the current result. Then you read the operator and apply it to the result and the next piece of data. An so on until you reach the end of the input. Upon encountering parentheses the simplest thing to do is to have the parser recursively call itself with the expression inside the parentheses (you should match them up) and treat the return value as an ordinal piece of data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top