Zorbing icon

[Logik] Formel

Zorbing | PRO | 12/06/12 11:01:59 PM UTC | 0 ⭐ | 243 👁️ | Never ⏰ | []
Java |

3.1 KB

|

None

|

0 👍

/

0 👎

package main;
 
public class Formel {
    private String FORMEL;
    private Formel aussage[] = new Formel[2];
    private short pointer = 0;
    private char junk = '-';
    private boolean negThis;
 
    Formel(String formel, boolean neg) {
        FORMEL = formel;
        negThis = neg;
        parse(formel);
    }
 
    /*
     * Private-Methoden
     */
    private void addFormel(String sub, boolean neg) {
        if (pointer < 2) {
            aussage[pointer++] = new Formel(sub, neg);
        } else {
            DNF.wrongFormel();
        }
    }
 
    private void parse(String formel) {
        // für das momentane Zeichen
        char c;
 
        // Name einer Variablen
        String var = "";
        // eine mögliche Unterformel
        String sub = "";
 
        // die Anzahl der Klammern
        int countBrackets = 0;
        // ob das folgende negiert werden soll
        boolean neg = false;
        // ob zuletzt eine Formel kam
        boolean lastFormel = false;
 
        // alle Zeichen der Formel durchgehen
        for (int i = 0; i < formel.length(); i++) {
            // das momentane Zeichen
            c = formel.charAt(i);
            // es wurde bereits mindestens eine Klammer geöffnet
            if (countBrackets > 0) {
                if (c == '(') {
                    countBrackets++;
                } else if (c == ')') {
                    countBrackets--;
                    if (countBrackets == 0) {
                        addFormel(sub, neg);
                        lastFormel = true;
                        sub = "";
                        neg = false;
                    }
                }
                if (countBrackets > 0) {
                    sub += c;
                }
            } else
            // eine erste öffnende Klammer tritt auf
            if (c == '(') {
                if (var.isEmpty() && !lastFormel) {
                    countBrackets = 1;
                } else {
                    DNF.wrongFormel();
                }
            } else
            // falls c ein Junktor ist
            if ("~&|>:".indexOf(c) != -1) {
                if (!neg) {
                    if (!var.isEmpty()) {
                        addFormel(var, neg);
                        var = "";
                    }
                    if (c == '~') {
                        neg = true;
                    } else if (junk == '-') {
                        junk = c;
                        if (junk == '>') {
                            if (pointer == 0) {
                                DNF.wrongFormel();
                            }
                            aussage[0].negate();
                            junk = '|';
                        }
                    } else {
                        DNF.wrongFormel();
                    }
                    lastFormel = false;
                } else {
                    DNF.wrongFormel();
                }
            } else
            // falls c eine a ist und eine Variable anfängt
            if (c == 'a') {
                if (var.isEmpty()) {
                    var = "a";
                } else {
                    DNF.wrongFormel();
                }
            } else
            // falls c ein ' ist und ein Variable benennt
            if (c == '\'') {
                if (!var.isEmpty()) {
                    var += c;
                } else {
                    DNF.wrongFormel();
                }
            }
        }
 
        if (countBrackets > 0) {
            System.out.println(formel);
            DNF.wrongFormel();
        }
        if (!var.isEmpty() && !formel.equals(var)) {
            addFormel(var, neg);
        }
 
        if (junk == ':') {
            // hier müsste man noch die Äquivalenz rausrechnen...
        }
    }
 
    /*
     * Public-Methoden
     */
    public String back() {
        String ret = "";
        if (negThis) {
            ret += "~";
        }
        if (pointer == 0) {
            ret += FORMEL;
        } else if (pointer == 1) {
            ret += aussage[0].back();
        } else if (pointer == 2) {
            ret += "(" + aussage[0].back() + junk + aussage[1].back() + ")";
        }
        return ret;
    }
 
    public boolean calc() {
        return false;
    }
 
    public void negate() {
        negThis = !negThis;
    }
}

Comments