Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Modulare Programmierung

Ähnliche Präsentationen


Präsentation zum Thema: "Modulare Programmierung"—  Präsentation transkript:

1 Modulare Programmierung
Vorlesung besteht aus zwei Teilen Programmierung (4 SWS) Methodik der Programmierung größerer Projekte im Team Grundkenntnisse moderner Programmiertechniken (JAVA) Grundlagen (2 SWS) prinzipieller Aufbau von Computern Arbeitsweise von Computern Darstellung von Daten im Computer Logik DVG1 - Modulare Programmierung

2 DVG1 - Modulare Programmierung
Beispiel Schreiben Sie ein Programm, das eine Tabelle erzeugt, die für i=1,7,49,...< die Werte i, sqrt(i) und (double)i*i enthält. i | sqrt(i) | i*i 1 | E+00 | E+00 7 | E+00 | E+01 49 | E+00 | E+03 343 | E+01 | E+05 2401 | E+01 | E+06 16807 | E+02 | E+08 | E+02 | E+10 | E+02 | E+11 | E+03 | E+13 | E+03 | E+15 | E+04 | E+16 DVG1 - Modulare Programmierung

3 DVG1 - Modulare Programmierung
0.te Näherung public class t1 { public static void main(String [] args) System.out.println( "\n i | sqrt(i) | i*i"); " "); for (long i=1;i< ;i*=7) System.out.println(i+" | "+Math.sqrt(i)+" | "+(double)i*i); } DVG1 - Modulare Programmierung

4 Ergebnis der 0.ten Näherung
i | sqrt(i) | i*i 1 | 1.0 | 1.0 7 | | 49.0 49 | 7.0 | 343 | | 2401 | 49.0 | 16807 | | E8 | | E10 | | E11 | | E13 | | E15 | | E16 DVG1 - Modulare Programmierung

5 DVG1 - Modulare Programmierung
1.te Näherung public class t1 { public static void main(String [] args) System.out.println( "\n i | sqrt(i) | i*i"); " "); for (long i=1;i< ;i*=7) System.out.println(i+"\t| "+Math.sqrt(i)+"\t| "+(double)i*i); } DVG1 - Modulare Programmierung

6 Ergebnis der 1.ten Näherung
i | sqrt(i) | i*i 1 | 1.0 | 1.0 7 | | 49.0 49 | 7.0 | 343 | | 2401 | 49.0 | 16807 | | E8 | | E10 | | E11 | | E13 | | E15 | | E16 DVG1 - Modulare Programmierung

7 DVG1 - Modulare Programmierung
Probleme Die Wirkung von Tabulatoren ist nicht vollständig durch das Programm beeinflußbar. Zahlen sollten rechtsbündig ausgegeben werden. Das Format von Gleitkommazahlen hängt sehr von dem Wert ab und ist somit nicht vorhersehbar. Anzahl der Stellen Exponentialdarstellung Vorzeichen Fazit: formatgesteuerte Ausgabe fehlt in JAVA! Z.B. Pascal: write(i:8,x:8:2); DVG1 - Modulare Programmierung

8 Formatierung von Festkommazahlen
Eingabe: Festkommazahl (byte, short, int, long) Alle Festkommatypen werden automatisch nach long konvertiert. ==> Wir wählen long als Eingabetyp: long z Ausgabe: Zeichenkette, die den Wert der Eingabezahl repräsentiert. Format der Ausgabe: <bl>...< bl><vz><zn>...<z0> <bl>: Leerzeichen <vz>: Vorzeichen = ' ' oder '-' <zi>: Ziffern zusätzliche Eingabe: Länge der Ausgabe: int l DVG1 - Modulare Programmierung

9 DVG1 - Modulare Programmierung
Probleme: Was tun, wenn falsche Parameter eingegeben werden? ==> einzige Möglichkeit für Fehler ist l<= ==> null ausgeben Was tun, wenn sich die Zahl nicht mit l Stellen darstellen läßt? z.B.: l=3 und z= ==> Zeichenkette "**...*" mit l Sternen ausgeben DVG1 - Modulare Programmierung

10 Vorhandene Lösungen untersuchen
In JAVA existiert die Methode Long.toString Aus der JAVA-Doku: public static String toString(long i) Returns a new String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and the radix 10 were given as arguments to the toString method that takes two arguments. Parameters: i - a long to be converted. Returns: a string representation of the argument in base 10. DVG1 - Modulare Programmierung

11 DVG1 - Modulare Programmierung
public static String toString(long i, int radix) Creates a string representation of the first argument in the radix specified by the second argument. ... If the first argument is negative, the first element of the result is the ASCII minus sign '-' ('\u002d'. If the first argument is not negative, no sign character appears in the result. The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character '0' ('\u0030'); otherwise, the first character of the representation of the magnitude will not be the zero character. Parameters: i - a long. radix - the radix. Returns: a string representation of the argument in the specified radix. DVG1 - Modulare Programmierung

12 DVG1 - Modulare Programmierung
Darstellung ist o.k. Wenn die Zahl zu klein ist, werden zu wenig Stellen ausgegeben. Wenn Zahl zu groß ist, werden zu viel Stellen ausgegeben. ==> Long.toString kann verwendet werden. Das Resultat muß aber modifiziert werden. DVG1 - Modulare Programmierung

13 Blockdiagramm - 1.Version
long z, int l l <= 0 return null; true String s; Berechnung der Ausgabe false return s; DVG1 - Modulare Programmierung

14 DVG1 - Modulare Programmierung
Methode - 1.Version public static String toString (long z, int l) { if ( l <= 0 ) return null; String s; // Berechnung der Ausgabezeichenkette return s; } DVG1 - Modulare Programmierung

15 Blockdiagramm - 2.Version
long z, int l l <= 0 return null; true false String s = Long.toString(z); s.length() =l links auffüllen <l Fehlerbehandlung >l return s; DVG1 - Modulare Programmierung

16 DVG1 - Modulare Programmierung
Methode - 2.Version public static String toString (long z, int l) { if ( l <= 0 ) return null; String s = Long.toString(z); if ( s.length() == l ) return s; if ( s.length() < l) // links auffuellen return s; } // Fehlerbehandlung DVG1 - Modulare Programmierung

17 Blockdiagramm - 3.Version
return s; String s = Long.toString(z); s.length() =l >l <l return null; true false l <= 0 long z, int l s="*"; s=' '+s; true s.length()<l true false s.length()<l false s='*'+s; DVG1 - Modulare Programmierung

18 DVG1 - Modulare Programmierung
Methode - 3.Version public static String toString (long z, int l) { if ( l <= 0 ) return null; String s = Long.toString(z); if ( s.length() == l ) return s; if ( s.length() < l) do s = ' '+s; } while ( s.length()<l); return s; } s = "*"; while ( s.length() < l) s = '*' + s; DVG1 - Modulare Programmierung

19 Methode - 1.optimierte Version
public static String toString (long z, int l) { if ( l <= 0 ) return null; String s = Long.toString(z); while ( s.length() < l) s = ' '+s; } if ( s.length() == l ) return s; s = "*"; s = '*' + s; return s; DVG1 - Modulare Programmierung

20 Methode - 2.optimierte Version
public static String toString (long z, int l) { if ( l <= 0 ) return null; String s = Long.toString(z); char fz = ' '; if ( s.length() > l) fz= '*' ; s = "*"; } while ( s.length() < l) s = fz+s; return s; DVG1 - Modulare Programmierung

21 Methode - endgültige Version
/** * <EM>toString</EM> konvertiert eine <CODE>long</CODE>-Variable * in eine Zeichenkette und füllt mit Leerzeichen bis zur * Länge <CODE>l</CODE> auf. Die Gesamtlänge enthält * das Vorzeichen "-" für negative Zahlen. <BR> * Falls die Zahl nicht in dem Format darstellbar ist, * wird die Zeichenkette "**...*" ausgegeben. <BR> * Wird <CODE>l<=0</CODE> angegeben, so wird <CODE>null</CODE> * zurückgegeben. Gerhard Telschow 1.0 z auszugebende Variable l Länge der Ausgabe Zeichenkette */ DVG1 - Modulare Programmierung

22 DVG1 - Modulare Programmierung
public static String toString(long z, int l) { // Falls l<= null zurueckgeben ! if (l<=0) return null; // Rohkonvertierung vom System uebernehmen String s = Long.toString(z); // Standardfuellzeichen ist das Leerzeichen char fz = ' '; // Wenn Laenge zu gross ist, Fuellzeichen wird *, // Zeichenkette wird geloescht if ( s.length() > l ) fz = '*'; s = "*"; } // Wenn noetig mit Fuellzeichen auffuellen while ( s.length() < l) s = fz + s; return s; DVG1 - Modulare Programmierung

23 DVG1 - Modulare Programmierung
2.te Näherung public class t1 { public static void main(String [] args) System.out.println( "\n i | sqrt(i) | i*i"); " "); for (long i=1;i< ;i*=7) System.out.println(InOut.toString(i,12)+" | "+Math.sqrt(i)+"\t| "+(double)i*i); } DVG1 - Modulare Programmierung

24 Ergebnis der 2.ten Näherung
i | sqrt(i) | i*i 1 | 1.0 | 1.0 7 | | 49.0 49 | 7.0 | 343 | | 2401 | 49.0 | 16807 | | E8 | | E10 | | E11 | | E13 | | E15 | | E16 DVG1 - Modulare Programmierung

25 Nutzung der Entwicklung
Um die entwickelte Methode weiter zu nutzen und deren Anwendung zu erleichtern, können wir zusätzliche Schnittstellen entwickeln. Z.B.: Konvertierung einer Zahl und anschließende Ausgabe: /** * <EM>print</EM> gibt eine <CODE>long</CODE>-Variable aus und * füllt mit führenden Leerzeichen bis zur Länge * <CODE>l</CODE> auf. Die Gesamtlänge enthält das * Vorzeichen "-" für negative Zahlen.<BR> * Falls die Zahl nicht in dem Format darstellbar ist, wird die * Zeichenkette "**...*" ausgegeben.<BR> * Wird <CODE>l<=0</CODE> angegeben, so erfolgt keine Ausgabe. Gerhard Telschow 1.0 z auszugebende Variable l Länge der Ausgabe incl. Vorzeichen Nothing */ public static void print(long z, int l) { String s = toString(z, l); if ( s != null) System.out.print(s); } DVG1 - Modulare Programmierung

26 DVG1 - Modulare Programmierung
Z.B.: Konvertierung einer Zahl und anschließende Ausgabe mit Zeilenschaltung: /** * <EM>println</EM> gibt eine <CODE>long</CODE>-Variable aus und * füllt mit führenden Leerzeichen bis zur Länge * <CODE>l</CODE> auf. Die Gesamtlänge enthält das * Vorzeichen "-" für negative Zahlen. Anschließend wird * auf eine neue Zeile geschaltet.<BR> * Falls die Zahl nicht in dem Format darstellbar ist, wird die * Zeichenkette "**...*" ausgegeben.<BR> * Wird <CODE>l<=0</CODE> angegeben, so erfolgt keine Ausgabe. Gerhard Telschow 1.0 z auszugebende Variable l Länge der Ausgabe incl. Vorzeichen Nothing */ public static void println(long z, int l) { print(z,l); System.out.println(); } DVG1 - Modulare Programmierung

27 Weg von der Aufgabe zur Lösung
Grobkonzept Verfeinerung Lösung Optimierung Ausbau Dokumentation Endgültige Lösung DVG1 - Modulare Programmierung

28 Formatierung von Gleitkommazahlen
Eingabe : Gleitkommazahl (float, double) float wird automatisch nach double konvertiert ==> wählen double : double z Ausgabe: Zeichenkette, die den Wert der Eingabezahl repräsentiert. Format der Ausgabe: <vm><m0><.><m1>...<mn>E<ve><ek>...<e0> <vm>: Vorzeichen = ' ' oder '-' <mi>: Ziffern der Mantisse mit folgenden Nullen <ve>: Vorzeichen = '+' oder '-' <ei>: Ziffern des Exponenten mit führenden Nullen zusätzliche Eingabe: Länge der Mantisse: int mant (Stellen nach dem Dezimalpunkt) Länge des Exponenten: int expo Gesamtlänge der Ausgabe: int l , l = mant+expo+5 DVG1 - Modulare Programmierung

29 DVG1 - Modulare Programmierung
Probleme: Welche Parameter wählt man. Nur zwei der drei Parameter l, mant und expo können genutzt werden. Wählen l und mant. Was tun, wenn falsche Parameter eingegeben werden? Fehler ist l < mant+6 Fehler ist mant < 1 null ausgeben Sonderfälle: z==+0.0 ==> " E+0..0" z==-0.0 ==> " E+0..0" z==+Infinity ==> " Inf " z==-Infinity ==> "-Inf " z==NaN ==> "NaN " Was tun, wenn sich die Zahl nicht mit l Stellen darstellen läßt? ==> Zeichenkette "**...*" mit l Sternen ausgeben DVG1 - Modulare Programmierung

30 Tests für die Sonderfälle
z==+0.0: if ( z == 0.0 & 1.0/z > 0.0 ) ... z==-0.0: if ( z == 0.0 & 1.0/z < 0.0 ) ... z==+Infinity: if ( (1.0/z) == 0.0 & z > 0.0 ) ... z==-Infinity: if ( (1.0/z) == 0.0 & z < 0.0 ) ... z==NaN: if ( z != z) ... DVG1 - Modulare Programmierung

31 Blockdiagramm - 1.Version
double z, int l, int mant l<mant+6 | mant<1 return null; true false String s; Sonderfälle Berechnung der Ausgabe return s; DVG1 - Modulare Programmierung

32 DVG1 - Modulare Programmierung
Methode - 1.Version public static String toString (double z, int l, int mant) { if ( l < mant+6 | mant < 1 ) return null; String s; // Sonderfaelle // Berechnung der Ausgabezeichenkette return s; } DVG1 - Modulare Programmierung

33 DVG1 - Modulare Programmierung
z != z s="NaN" true false s.length()>0 1.0/z==0.0 false true z>0.0 s=" Inf" true s="-Inf" false false z==0.0 true 1.0/z>0.0 true s=" 0.0" false s="-0.0" Mantisse s=s+"E+0" false Exponent rechts auffüllen true return s; DVG1 - Modulare Programmierung

34 DVG1 - Modulare Programmierung
Methode - Sonderfälle if ( z != z ) s="NaN" ; else { if (1.0/z == 0.0) if ( z>0.0 ) s=" Inf"; else s="-Inf"; } if ( z == 0.0 ) if ( 1.0/z > 0.0 ) s=" 0.0"; else s="-0.0"; while (s.length()<mant+3) s=s+"0"; s=s+"E+"; while (s.length()<l) s=s+"0"; DVG1 - Modulare Programmierung

35 DVG1 - Modulare Programmierung
if ( s.length() > 0 ) { while (s.length()<l) s=s+" "; return s; } DVG1 - Modulare Programmierung

36 Mantisse und Exponent berechnen Vorzeichen des Exponent bestimmen
Vorzeichen bestimmen Mantisse und Exponent berechnen Mantisse umwandeln Vorzeichen des Exponent bestimmen Exponent umwandeln Fehler behandeln Ergebnis berechnen DVG1 - Modulare Programmierung

37 s=Double.toString(z); s=s.substring(0,mant+2);
char vz=' '; z<0.0 true vz='-'; z=-z; false int e=0; z>=10.0 z*=0.1; true e++; false z<1.0 z*=10.0; true e--; false s=Double.toString(z); s.length()<mant+2 s=s+'0'; true s=s.substring(0,mant+2); false DVG1 - Modulare Programmierung

38 String se=Integer.toString(e); false
char vze='+'; e<0 true vze='-'; e=-e; String se=Integer.toString(e); false se.length()<l-mant-5 se='0'+se; true false se.length()>l-mant-5 true s=""; false s.length()<l return s; false return vz+s+'E'+vze+se; s=s+'*'; true DVG1 - Modulare Programmierung

39 DVG1 - Modulare Programmierung
/********************************************************************** Das Vorzeichen wird bestimmt, in vz gemerkt und die Zahl in positiv verwandelt. ***********************************************************************/ char vz = ' '; if (z<0.0) { vz='-'; z=-z; } Der Exponent wird berechnet; z*10^e bleibt der eingegebene Wert int e = 0; while (z >= 10.0) z*=0.1; e++; while (z<1.0) z*=10.0; e--; Mantisse umwandeln **********************************************************************/ s=Double.toString(z); while (s.length()<mant+2) s=s+'0'; s=s.substring(0,mant+2); DVG1 - Modulare Programmierung

40 DVG1 - Modulare Programmierung
/********************************************************************** Vorzeichen des Exponenten bestimmen **********************************************************************/ String vze = "+"; if (e<0) { vze="-"; e=-e; } Exponenten umwandeln String se = Integer.toString(e); while (se.length()<l-mant-5) se="0"+se; if ( se.length()>l-mant-5) s=""; while (s.length()<l) s=s+'*'; return s; Ergebnis zusammenfuegen return vz+s+'E'+vze+se; DVG1 - Modulare Programmierung

41 DVG1 - Modulare Programmierung
3.te Näherung public class t1 { public static void main(String [] args) System.out.println( "\n i | sqrt(i) | i*i"); " "); for (long i=1;i< ;i*=7) System.out.println(InOut.toString(i,12)+" | "+ InOut.toString(Math.sqrt(i),19,12)+" | "+ InOut.toString((double)i*i),19,12)); } DVG1 - Modulare Programmierung

42 Ergebnis der 3.ten Näherung
i | sqrt(i) | i*i 1 | E+00 | E+00 7 | E+00 | E+01 49 | E+00 | E+03 343 | E+01 | E+05 2401 | E+01 | E+06 16807 | E+02 | E+08 | E+02 | E+10 | E+02 | E+11 | E+03 | E+13 | E+03 | E+15 | E+04 | E+16 DVG1 - Modulare Programmierung

43 Nutzung der Entwicklung
Um die entwickelte Methode weiter zu nutzen und deren Anwendung zu erleichtern, können wir zusätzliche Schnittstellen entwickeln. Z.B.: Konvertierung einer Zahl und anschließende Ausgabe: public static void print(double z, int l, int mant) { String s = toString(z, l, mant); if ( s != null) System.out.print(s); } public static void println(double z, int l, int mant) print(z, l, mant); System.out.println(); DVG1 - Modulare Programmierung


Herunterladen ppt "Modulare Programmierung"

Ähnliche Präsentationen


Google-Anzeigen