Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| q1:parser_turing_keller [2018/11/28 11:28] – admin03 | q1:parser_turing_keller [2019/09/21 07:59] (aktuell) – admin03 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| <code JAVA> | <code JAVA> | ||
| - | public class InfixCalculation{ | + | package infix; |
| - | + | ||
| - | | + | public class InfixCalulator |
| - | private String rechnung; // die vollständige Rechnung | + | private String rechnung; // die vollständige Rechnung |
| - | private int pos; // Position des nächsten Zeichens | + | |
| - | private char aktuell; // das aktuelle Zeichen | + | |
| - | // Stellt einen gewöhnlichen Infix-Ausdruck mit den vier | + | |
| - | // Grundrechenarten und Klammern dar, z.B. 3*(-(2+4)) | + | |
| - | public | + | |
| - | this.rechnung=rechnung+" | + | |
| - | } | + | System.out.println(this.rechnung); |
| - | + | | |
| - | // Rechnet einmal durch | + | |
| - | public double berechne(){ | + | |
| - | pos=0; | + | |
| - | weiter(); | + | |
| - | return summe(); | + | |
| - | } | + | pos=0; |
| - | + | | |
| - | private void weiter(){ | + | |
| - | aktuell=rechnung.charAt(pos++); | + | |
| - | } | + | |
| - | + | | |
| - | private boolean ziffer() | + | |
| - | { | + | |
| - | return aktuell> | + | |
| - | } | + | |
| - | + | | |
| - | private double zahl(){ | + | |
| - | double zahl = 0.0; | + | |
| - | while (ziffer()) { | + | |
| - | zahl *= 10; | + | |
| - | zahl += Character.getNumericValue(aktuell); | + | |
| - | weiter(); | + | double kommazahl = 0.0; |
| - | } | + | double potenz = 1.0; |
| - | return zahl; | + | |
| - | } | + | |
| - | + | | |
| - | // TERM --> ZAHL | (SUMME) | + | |
| - | private double term(){ | + | |
| - | double term = 1.0; | + | if(aktuell==' |
| - | if(ziffer()) { | + | weiter(); |
| - | term = zahl(); | + | while (ziffer()) { |
| - | } | + | |
| - | if(aktuell==' | + | |
| - | weiter(); | + | weiter(); |
| - | term = summe(); | + | } |
| - | } | + | } |
| - | return term; | + | return zahl+kommazahl; |
| - | } | + | |
| - | + | ||
| - | + | | |
| - | // PRODUKT --> TERM | TERM*TERM* ... | TERM/TERM/ ... | + | |
| - | // | (auch * und / gemischt) | + | |
| - | private double produkt(){ | + | |
| - | double produkt = 1.0; // Unsinn, aber compilierbar. | + | |
| - | if(ziffer()||aktuell==' | + | |
| - | produkt = term(); | + | |
| - | } | + | |
| - | if(aktuell==' | + | |
| - | weiter(); | + | weiter(); |
| - | produkt = produkt*produkt(); | + | |
| - | } | + | |
| - | if(aktuell=='/' | + | |
| - | weiter(); | + | |
| - | produkt = produkt/ | + | |
| - | } | + | |
| - | return produkt; | + | |
| - | } | + | |
| - | + | | |
| - | // SUMME --> +PRODUKT | -PRODUKT (evtl. führende Vorzeichen) | + | |
| - | // | PRODUKT | + | |
| - | // | PRODUKT+PRODUKT+ ... | + | |
| - | // | PRODUKT-PRODUKT- ... | + | |
| - | // | (auch + und - gemischt) | + | |
| - | private double summe(){ | + | |
| - | double summe = 0.0; | + | |
| - | | + | |
| - | summe += produkt(); | + | |
| - | } | + | |
| - | if(aktuell==' | + | |
| - | weiter(); | + | |
| - | summe += summe(); | + | |
| - | } | + | return produkt; |
| - | if(aktuell==' | + | |
| - | weiter(); | + | |
| - | summe -= summe(); | + | |
| - | } | + | |
| - | if(aktuell==')' | + | |
| - | return summe; | + | |
| - | } | + | |
| - | + | | |
| - | | + | |
| + | if(aktuell==' | ||
| + | return summe; | ||
| + | } | ||
| + | |||
| + | | ||
| + | summe += produkt(); | ||
| + | } | ||
| + | if(aktuell==' | ||
| + | summe += produkt(); | ||
| + | | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | summe += summe(); | ||
| + | } | ||
| + | |||
| + | | ||
| + | | ||
| + | |||
| + | |||
| + | |||
| + | public static void main(String[] args) { | ||
| + | String lese = " | ||
| + | System.out.println(" | ||
| + | InfixCalulator calc=new InfixCalulator(lese); | ||
| + | System.out.println(lese +" = " | ||
| + | } | ||
| + | } | ||
| - | public static void main(String[] args) { | ||
| - | System.out.println(" | ||
| - | InfixCalculation calc=new InfixCalculation(" | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| </ | </ | ||