Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Datenstrukturen Programmierung II Prof. Dr. Michael Löwe.

Ähnliche Präsentationen


Präsentation zum Thema: "Datenstrukturen Programmierung II Prof. Dr. Michael Löwe."—  Präsentation transkript:

1 Datenstrukturen Programmierung II Prof. Dr. Michael Löwe

2 Datenstrukturen2 Inhalt Der Begriff „Datenstruktur“ Datenstrukturen = Datentyp + Prozeduren Funktional Mit Seiteneffekte Datenstrukturen = Programm-Modul Datenstrukturen = Objektinstanzen von Klassen Vergleich und Resümee

3 Datenstrukturen3 Struktur erhaltende Operationen = Schnittstelle Datenstrukturen Strukturierte Daten Strukturierte Daten Operation 1 Rumpf 1 Operation 2 Rumpf 2 Operation 3 Rumpf 3 Benutzer Operation 1Operation 2Operation 3 Konsistenz Invarianten Eigenschaften

4 Datenstrukturen4 Datentyp und Funktion Parameter 1: Type Parameter 2:Type Parameter n: Type  Ergebnis: Type Funktionskopf WAS Funktionsrumpf Algorithmus Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n WIE

5 Datenstrukturen5 Funktion 1 Rumpf 1 Funktion 2 Rumpf 2 Funktion 3 Rumpf 3 Funktion n Rumpf n Datentyp Name Modul Feldselektion Feldzuweisung Datenstruktur = Datentyp + Funktionen Benutzer Variablen- deklaration Funktionsaufruf FeldselektionFeldzuweisung

6 Datenstrukturen6 STREAM als funktionale Datenstruktur Public Type MyStream data() As Variant first As Integer behindLast As Integer size As Integer End Type Public Function first(x As MyStream) As Variant Public Function append(x As MyStream, y As Variant) As MyStream Public Sub removeFirst(x As MyStream) As MyStream Public Function emptyStream() As MyStream Public Function isEmptyStream(x As MyStream) As Boolean Public Function streamLength(x As MyStream) As Integer first append removeFirst

7 Datenstrukturen7 First und emptyStream Public Function first(x As MyStream) As Variant first = x.data(x.first) End Function Public Function emptyStream() As MyStream Dim result As MyStream ReDim result.data(1 To packageSize) result.first = 1 result.behindLast = 1 result.size = packageSize emptyStream = result End Function d1d2 firstbehindLastsize1

8 Datenstrukturen8 append Public Function append(x As MyStream, y As Variant) As MyStream append = x If append.behindLast <= append.size Then append.data(append.behindLast) = y append.behindLast = append.behindLast + 1 Else append.size = append.size + packageSize ReDim Preserve append.data(1 To append.size) append.data(append.behindLast) = y append.behindLast = append.behindLast + 1 End If End Function d1d2 firstbehindLastsize1

9 Datenstrukturen9 removeFirst Public Function removeFirst(ByRef x As MyStream) As MyStream Dim index As Integer removeFirst = x If Not isEmptyStream(removeFirst) Then If removeFirst.first <= packageSize Then removeFirst.first = removeFirst.first + 1 Else For index = 1 To removeFirst.behindLast - packageSize - 1 removeFirst.data(index) = removeFirst.data(index + packageSize + 1) Next index removeFirst.behindLast = removeFirst.behindLast - packageSize - 1 removeFirst.first = 1 End If End Function d1d2 firstbehindLastsize1

10 Datenstrukturen10 Benutzung (......) Dim x as MyStream Dim y as MyStream x = emptyStream x = append(x, 7) y = removeFirst(x) y = x (......) x.size = 44 x.data(12) = 18 (......) Variablen- deklaration Initialisierung Funktions- aufrufe Feldselektion Feldaktualisierung d1d2 firstbehindLastsize1

11 Datenstrukturen11 Benutzung in Algorithmen Public Function join (x As MyStream, y As MyStream) As MyStream `Hängt den Strom y hinten an den Strom x an. Keine Seiteneffekte. Dim y1 As MyStream join = x y1 = y Do While not isemptyStream(y1) join = append(join, first(y1) y1 = removeFirst(y1) Loop End Function

12 Datenstrukturen12 Parameter 1: TypeParameter 2:Type ByRef Parameter n: Type Prozedurkopf WAS Prozedurrumpf Algorithmus Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n Elem1 : type 1 Elem2 : Type 2 Elem n : type n WIE Datentyp und Prozedur

13 Datenstrukturen13 Prozedur 1 Rumpf 1 Prozedur 2 Rumpf 2 Prozedur 3 Rumpf 3 Prozedur n Rumpf n Datentyp Name Modul Datenstruktur = Datentyp + Prozeduren Benutzer Variablen- deklaration Prozeduraufruf FeldselektionFeldzuweisung Feldselektion Feldzuweisung

14 Datenstrukturen14 STREAM als Datenstruktur Public Type MyStream data() As Variant first As Integer behindLast As Integer size As Integer End Type Public Function first(x As MyStream) As Variant Public Sub append(ByRef x As MyStream, y As Variant) Public Sub removeFirst(ByRef x As MyStream) Public Function emptyStream() As MyStream Public Function isEmptyStream(x As MyStream) As Boolean Public Function streamLength(x As MyStream) As Integer

15 Datenstrukturen15 append Public Sub append(ByRef x As MyStream, y As Variant) If x.behindLast <= x.size Then x.data(x.behindLast) = y x.behindLast = x.behindLast + 1 Else x.size = x.size + packageSize ReDim Preserve x.data(1 To x.size) x.data(x.behindLast) = y x.behindLast = x.behindLast + 1 End If End Sub d1d2 firstbehindLastsize1

16 Datenstrukturen16 removeFirst Public Sub removeFirst(ByRef x As MyStream) Dim index As Integer If Not isEmptyStream(x) Then If x.first <= packageSize Then x.first = x.first + 1 Else For index = 1 To x.behindLast - packageSize - 1 x.data(index) = x.data(index + packageSize + 1) Next index x.behindLast = x.behindLast - packageSize - 1 x.first = 1 End If End Sub d1d2 firstbehindLastsize1

17 Datenstrukturen17 Benutzung (......) Dim x as MyStream Dim y as MyStream x = emptyStream Call append(x, 7) Call removeFirst(x) y = x (......) x.size = 44 x.data(12) = 18 (......) Variablen- deklaration Initialisierung Funktions- aufrufe Feldselektion Feldaktualisierung d1d2 firstbehindLastsize1

18 Datenstrukturen18 Benutzung in Algorithmen Public Sub split (ByRef x As MyStream, ByRef y As MyStream) `Leitet jedes zweite Element in x nach y um. Dim a As MyStream a = x x = emptyStream Do While not isEmptyStream(a) Call append(x, first(a)) Call removeFirst(a) If not isEmptyStream(a) Then Call append(y, first(a)) Call removeFirst(a) End If Loop End Sub

19 Datenstrukturen19 Datenstruktur = Modul + Prozeduren Parameter 1Parameter 2Parameter n  Ergebnis Prozedurkopf Algorithmus Prozedurrumpf (Private) Variablen des Moduls kontrollierte Seiteneffekte

20 Datenstrukturen20 Datenstruktur = Modul + Prozeduren Benutzer Prozedur 1 Rumpf 1 Prozedur 2 Rumpf 2 Funktion 3 Rumpf 3 Funktion n Rumpf n „Private“ Variablen Auswertung Zuweisung Modul Prozeduraufruf Funktionsaufruf

21 Datenstrukturen21 STREAM als Modul Module MyStream Private data() As Variant Private first As Integer Private behindLast As Integer Private size As Integer Public Function first() As Variant Public Sub append(y As Variant) Public Sub removeFirst() Public Sub initialzeAsEmptyStream() Public Function isEmptyStream() As Boolean Public Function streamLength() As Integer End Module MyStream

22 Datenstrukturen22 append Public Sub append(y As Variant) If behindLast <= size Then data(behindLast) = y behindLast = behindLast + 1 Else size = size + packageSize ReDim Preserve data(1 To size) data(behindLast) = y behindLast = behindLast + 1 End If End Sub d1d2 firstbehindLastsize1

23 Datenstrukturen23 removeFirst Public Sub removeFirst Dim index As Integer If Not isEmptyStream Then If first <= packageSize Then first = first + 1 Else For index = 1 To behindLast - packageSize - 1 data(index) = data(index + packageSize + 1) Next index behindLast = behindLast - packageSize - 1 first = 1 End If End Sub d1d2 firstbehindLastsize1

24 Datenstrukturen24 Benutzung (......) call initialzeAsEmptyStream Call append(7) call append(12) x = first Call removeFirst (......) Initialisierung Prozedur- aufrufe d1d2 firstbehindLastsize1

25 Datenstrukturen25 Benutzung in Algorithmen Public Sub inStream (s As String) `Liefert den Strom ab, der zusätzlich die in s durch „|“ getrennten Elemente enthält. Benutzt: Function instr(s As String, p As String) As Integer Do While s <> „“ append(left(s, instr(s, „|“)-1) s = right(s, Len(s) - instr(s, „|“)) Loop If s <> „“ Then append(s) End Sub

26 Datenstrukturen26 Funktionen und Objekte Parameter 1Parameter 2Parameter n  Ergebnis Prozedurkopf Algorithmus Prozedurrumpf (Private) Variablen eines Objekts kontrollierte Seiteneffekte

27 Datenstrukturen27 Datenstruktur = Objekt zu einer Klasse Benutzer Prozedur 1 Rumpf 1 Prozedur 2 Rumpf 2 Funktion 3 Rumpf 3 Funktion n Rumpf n „Private“ Variablen eines Objekts Auswertung Zuweisung Objekt Prozeduraufruf Funktionsaufruf

28 Datenstrukturen28 STREAM als Klassenmodul Class MyStream Private data() As Variant Private first As Integer Private behindLast As Integer Private size As Integer Public Function first As Variant Public Sub append(y As Variant) Public Sub removeFirst Public Function isEmptyStream As Boolean Public Function streamLength As Integer Private Sub initialize End Class

29 Datenstrukturen29 first Public Function first As Variant first = data(first) End Function d1d2 firstbehindLastsize1

30 Datenstrukturen30 append Public Sub append(y As Variant) If behindLast <= size Then data(behindLast) = y behindLast = behindLast + 1 Else size = size + packageSize ReDim Preserve data(1 To size) data(behindLast) = y behindLast = behindLast + 1 End If End Sub d1d2 firstbehindLastsize1

31 Datenstrukturen31 removeFirst Public Sub removeFirst Dim index As Integer If Not isEmptyStream Then If first <= packageSize Then first = first + 1 Else For index = 1 To behindLast - packageSize - 1 data(index) = data(index + packageSize + 1) Next index behindLast = behindLast - packageSize - 1 first = 1 End If End Sub d1d2 firstbehindLastsize1

32 Datenstrukturen32 isEmptyStream und streamLength Public Function isEmptyStream() As Boolean If first = behindLast Then isEmptyStream = True Else isEmptyStream = False End If End Function Public Function streamLength() As Integer streamLength = behindLast - first End Function d1d2 firstbehindLastsize1

33 Datenstrukturen33 emptyStream:= initialize Private Sub initialize ReDim data(1 To packageSize) first = 1 behindLast = 1 size = packageSize End Sub d1d2 firstbehindLastsize1

34 Datenstrukturen34 Initialisierung Zuweisungen Benutzung (......) Dim x as MyStream Dim y as MyStream Set x = New MyStream Call x.append (7) Call x.append (11) Call x.removeFirst z = x.first Set y = New MyStream Set y = x Variablen- deklaration Funktions- aufrufe d1d2 firstbehindLastsize1

35 Datenstrukturen35 Benutzung in Algorithmen Public Function shuffle (s As MyStream) `Liefert den Strom ab, der alternierend Zeichen aus sich selbst und aus s enthält. Seiteneffekt auf s! Dim result As MyStream Set result = New MyStream Do While not (s.isEmptyStream And Me.isEmptyStream) If Not Me.isEmptyStream Then Call result.append (me.first) Call Me.removeFirst End If If Not s.isEmptyStream Then Call result.append (s.first) Call s.removeFirst End If Loop Set Me = result `(?) End Function

36 Datenstrukturen36 Vergleich und Resümee

37 „Grosse Zahlen“ objektorientiert

38 Datenstrukturen38 Klasse MyCardinal Private data() As Integer Public Sub setData(x() As Integer) Public Function getData() As Integer() Public Function asCardinal(s As String) As MyCardinal Public Function asString() As String Public Function cNull() As MyCardinal Public Function cOne() As MyCardinal Public Function cAdd(x As MyCardinal) As MyCardinal Public Function cSub(x As MyCardinal) As MyCardinal Public Function cMul(x As MyCardinal) As MyCardinal Public Function cDiv(x As MyCardinal) As MyCardinal Public Function cMod(x As MyCardinal) As MyCardinal Public Function cEQ(x As MyCardinal) As Boolean Public Function cBG(x As MyCardinal) As Boolean

39 Datenstrukturen39 Klasse MyInteger Private positive As MyCardinal Private negative As MyCardinal Public Sub setPositive(x As MyCardinal) Public Sub setNegative(x As MyCardinal) Public Function getPositive As MyCardinal Public Function getNegative As MyCardinal Public Function asInteger(s As String) As MyInteger Public Function asString() As String Public Function iNull() As MyInteger Public Function iOne() As MyIntegerl Public Function iAdd(x As MyInteger) As MyInteger Public Function iSub......... Public Function iEQ(x As MyInteger) As Boolean Public Function iBG(x As MyInteger) As Boolean

40 Datenstrukturen40 Klasse MyRational Private numerator As MyInteger Private denominator As MyInteger Public Sub setNumerator(x As MyInteger) Public Sub setDenominator(x As MyInteger) Public Function getNumerator As MyInteger Public Function getdenominator As MyInteger Public Function asRational(s As String) As MyRational Public Function asString() As String Public Function rNull() As MyRational Public Function rOne() As MyRationall Public Function rAdd(x As MyRational) As MyRational Public Function rSub......... Public Function rEQ(x As MyRational) As Boolean Public Function rBG(x As MyRational) As Boolean

41 Datenstrukturen41 Beispiele für Implementierungen Public Function rMul(x As MyRational) As MyRational Set rMul = New MyRational Call rMul.setNumerator (x.getNumerator.iMul (numerator)) Call rMul.setDenominator (x.getDenominator.iMul (denominator)) End Function Public Function iAdd(x As MyInteger) As MyInteger Set iAdd = New MyInteger Call iAdd.setPositive (x.getPositive.cAdd (positive)) Call iAdd.setNegative (x.getNegative.cAdd (negative)) End Function

42 Datenstrukturen42 Beispiele für Implementierungen Public Function rAdd(x As MyRational) As MyRational Set rAdd = New MyRational Call rMul.setNumerator (x.getNumerator.iMul (denominator).iAdd (x.getDenominator.iMul (numerator))) Call rMul.setDenominator (x.getDenominator.iMul (denominator)) End Function


Herunterladen ppt "Datenstrukturen Programmierung II Prof. Dr. Michael Löwe."

Ähnliche Präsentationen


Google-Anzeigen