/* ================================================================================ = Autor: Mario SCHNEPF = MatrklNr: e0225056 = E-Mail: ma_s@gmx.at / e0225056@student.tuwien.ac.at = Datum: 15. Jänner 2003 ================================================================================ = abstract void doAbleitung() throws EprogException; = ==> siehe Term.java.doAbleitung() = ------------------------------------------------------------------------------ = abstract void checkFormatierung() throws EprogException; = ==> siehe Term.java.doAbleitung() = ------------------------------------------------------------------------------ = abstract String erzeugeString (); = ==> siehe Term.java.doAbleitung() ================================================================================ */ package symbdiff; import eprog.*; class zahl extends Term { // ANFANG: Variablen deklariern ============================================== public int Koeffizient = +1; public int Exponent = +1; private int Art = 0; // ENDE: Variablen deklarieren =============================================== // ANFANG: Konstruktor ======================================================= public zahl (String Temp) { Eingabe = Temp; } // ENDE: Konstruktor ========================================================= // ANFANG: checkFormatierung ================================================= public void checkFormatierung() throws EprogException { // Vorzeichen überprüfen (wenn "-" ==> negativ - sonst in jedem Fall + // anschließend Vorzeichen aus dem Eingabestring löschen if (Eingabe.charAt(0) == '-') { Koeffizient = -1; Eingabe = Eingabe.substring(1); } else { Koeffizient = +1; if (Eingabe.charAt(0) == '+') Eingabe = Eingabe.substring(1); } // Eingabestring hat jetzt Länge 1, 2 oder 3 // FORMAT: "", "", "", "", "" // ----- 1 Z E I C H E N ---------------------------- if (Eingabe.length()==1) { // überprüfen ob es sich um eine Variable handeln kann if (Global.X.indexOf(Eingabe) > -1) { x = Eingabe; Exponent = 1; Art = 1; } else { int zahl = erzeugeInt(Eingabe); if (zahl != -1) { Koeffizient = Koeffizient * zahl; x = null; Exponent = 0; Art = 2; } else throw new EprogException ("ZAHL: 1 Zeichen - Fehler"); } } // ----- 2 Z E I C H E N ---------------------------- else if (Eingabe.length() == 2) { // überprüfen ob der Fall "" vorliegt if ((erzeugeInt(Eingabe.substring(0,1))) != -1 && (Global.X.indexOf(Eingabe.substring(1)) != -1)) { Koeffizient = Koeffizient * erzeugeInt(Eingabe.substring(0,1)); x = Eingabe.substring(1); Exponent = 1; Art = 3; } // Fall: else if ((erzeugeInt(Eingabe.substring(1))) != -1 && (Global.X.indexOf(Eingabe.substring(0,1)) != -1)) { x = Eingabe.substring(0,1); Exponent = erzeugeInt(Eingabe.substring(0,1)); Art = 4; } else throw new EprogException ("ZAHL: 2 Zeichen - Fehler"); } // ----- 3 Z E I C H E N ---------------------------- else if (Eingabe.length() == 3) { if ((erzeugeInt(Eingabe.substring(0,1)) != -1) && (Global.X.indexOf(Eingabe.substring(1,2)) != -1) && (erzeugeInt(Eingabe.substring(2)) != -1)) { Koeffizient = Koeffizient * erzeugeInt(Eingabe.substring(0,1)); x = Eingabe.substring(1,2); Exponent = erzeugeInt(Eingabe.substring(2)); Art = 5; } else throw new EprogException ("ZAHL: 3 Zeichen - Fehler"); } // ----- andere Anzahl von Zeichen --------------------- else throw new EprogException ("ZAHL: Zeichenkette != Länge 1-3"); } // ENDE: checkFormatierung =================================================== // ANFANG: doAbleitung ======================================================= public void doAbleitung() throws EprogException { // nach Global.V ableiten if (Global.V == null) Global.V = x; else if (!(Global.V.equals(x)) && (x != null)) throw new EprogException ("ZAHL: falsche Variable gefunden: "); if ((x != null) && (Exponent != 0)) { Koeffizient = Koeffizient * Exponent; Exponent = Exponent - 1; } else Koeffizient = 0; if (Exponent == 0) x = null; } // ENDE: doAbleitung ========================================================= // ANFANG: erzeugeString ===================================================== public String erzeugeString() { String Ausgabe = ""; // wenn Koeffizient gleich 0 ==> es muss nichts ausgeben werden if (Koeffizient > 1) Ausgabe = "+" + Koeffizient; else if (Koeffizient == 1) Ausgabe = "+"; else Ausgabe = Integer.toString(Koeffizient); if (x != null) Ausgabe += x; if ((Exponent != 0) && Exponent != 1) Ausgabe += Exponent; if ((Koeffizient == 0) && (x=="") && (Exponent == 0)) Ausgabe = ""; return Ausgabe; } // ENDE: erzeugeString ======================================================= }