1

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.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
CWOmer
  • 101
  • 1
  • 2
  • 11
  • 1
    You should always have your source code written in 100% English. A reason for this is being a professional standard, another is to let others understand it, and there can be even more... – Powerslave May 11 '13 at 20:06
  • if it is problem, i can change code. I think that it doesn't matter. – CWOmer May 11 '13 at 20:48
  • However, im glad you for your comments and i am waiting your help – CWOmer May 11 '13 at 20:49
  • It doesn't matter unless you want an answer here. How many programmers speak English as opposed to your language? Are you addressing your question to your own nationals, or to the world? Which is more likely to yield a correct answer? It's up to you. – user207421 Dec 30 '13 at 08:43
  • Actually there are plenty of places in the world where code isn't written in English. People use their native language all the time. It may reduce the number of people who understand it so isn't the best idea if you want maximum feedback but no need to jump on the guy about it. – Tim B Dec 30 '13 at 09:26

1 Answers1

2

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.

Community
  • 1
  • 1
Powerslave
  • 1,408
  • 15
  • 16
  • Thanks for your interesting. However, i cannot use the another library. I think you understood the problem but i must do with my own code – CWOmer May 12 '13 at 06:42
  • Then you might want to take a look at [ScriptingEngine](http://bit.ly/10BnRnw), [AbstractScriptingEngine](http://bit.ly/17VhbWq) and [Bingdings](http://bit.ly/19eMV7L) so you can get a grasp of a finer approach. Also, there are several articles available on the **[subject](http://bit.ly/f4Xs2a) [of](http://bit.ly/YOoxuz) [expression](http://bit.ly/15BDCTk) [parsing](http://bit.ly/MZ0Co)**. These explain the better than I could (i.e. the *"If you need to devise your own algorithm"* part of my answer). – Powerslave May 12 '13 at 12:19