From 01bbbd3f2f1c73661feee19cce61dd5a93e2d973 Mon Sep 17 00:00:00 2001 From: wienkop <uwe.wienkop@th-nuernberg.de> Date: Mon, 31 May 2021 12:56:28 +0200 Subject: [PATCH] 2021-04-31 Delegates/Tests in einer generischen Liste --- 09-3 Intro Delegates/Program.cs | 6 + .../10-2 DelegateVariable.csproj | 9 + 10-1 DelegateVariable/Program.cs | 12 ++ .../10-1 GenerischeListeMitSuche.csproj | 9 + 10-1 GenerischeListeMitSuche/GenericList.cs | 196 ++++++++++++++++++ 10-1 GenerischeListeMitSuche/Program.cs | 58 ++++++ 10-Ubg HashDict-Mo/10-Ubg HashDict-Mo.csproj | 9 + 10-Ubg HashDict-Mo/HashDict.cs | 60 ++++++ 10-Ubg HashDict-Mo/KeyValuePaar.cs | 16 ++ 10-Ubg HashDict-Mo/Program.cs | 29 +++ 10-Ubg Sets/10-Ubg Sets-Mo.csproj | 9 + 10-Ubg Sets/Program.cs | 12 ++ Prog2WienkopSS2021.sln | 22 +- 13 files changed, 445 insertions(+), 2 deletions(-) create mode 100644 10-1 DelegateVariable/10-2 DelegateVariable.csproj create mode 100644 10-1 DelegateVariable/Program.cs create mode 100644 10-1 GenerischeListeMitSuche/10-1 GenerischeListeMitSuche.csproj create mode 100644 10-1 GenerischeListeMitSuche/GenericList.cs create mode 100644 10-1 GenerischeListeMitSuche/Program.cs create mode 100644 10-Ubg HashDict-Mo/10-Ubg HashDict-Mo.csproj create mode 100644 10-Ubg HashDict-Mo/HashDict.cs create mode 100644 10-Ubg HashDict-Mo/KeyValuePaar.cs create mode 100644 10-Ubg HashDict-Mo/Program.cs create mode 100644 10-Ubg Sets/10-Ubg Sets-Mo.csproj create mode 100644 10-Ubg Sets/Program.cs diff --git a/09-3 Intro Delegates/Program.cs b/09-3 Intro Delegates/Program.cs index d1b3258..7355251 100644 --- a/09-3 Intro Delegates/Program.cs +++ b/09-3 Intro Delegates/Program.cs @@ -5,6 +5,9 @@ namespace _09_3_Intro_Delegates class Program { delegate double MyFunction(double zzz); + // Stellvertreterdefinition für Funktionen mit genau einem double Eingangsparameter und + // mit einem double-Returnwert + static void Wertetabelle(double von, double bis, MyFunction funktion) { for (double x = von; x <= bis; x += 1) @@ -14,6 +17,7 @@ namespace _09_3_Intro_Delegates // ^^^^^^^^ FIRMA A ^^^^^^ + // -------- FIRMA B ------ static double Quadrieren(double xxx) => xxx * xxx; @@ -22,6 +26,8 @@ namespace _09_3_Intro_Delegates { Wertetabelle(0, 3, Math.Sin); Wertetabelle(0, 3, Quadrieren); + Wertetabelle(0, 3, delegate (double x) { return 2 * x; }); // Anonyme Methode + Wertetabelle(0, 3, x => 2 * x); // Lambda Ausdruck } } } diff --git a/10-1 DelegateVariable/10-2 DelegateVariable.csproj b/10-1 DelegateVariable/10-2 DelegateVariable.csproj new file mode 100644 index 0000000..0919e11 --- /dev/null +++ b/10-1 DelegateVariable/10-2 DelegateVariable.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_10_1_DelegateVariable</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/10-1 DelegateVariable/Program.cs b/10-1 DelegateVariable/Program.cs new file mode 100644 index 0000000..f5ae6ae --- /dev/null +++ b/10-1 DelegateVariable/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace _10_1_DelegateVariable +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/10-1 GenerischeListeMitSuche/10-1 GenerischeListeMitSuche.csproj b/10-1 GenerischeListeMitSuche/10-1 GenerischeListeMitSuche.csproj new file mode 100644 index 0000000..7f608d8 --- /dev/null +++ b/10-1 GenerischeListeMitSuche/10-1 GenerischeListeMitSuche.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_10_1_GenerischeListeMitSuche</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/10-1 GenerischeListeMitSuche/GenericList.cs b/10-1 GenerischeListeMitSuche/GenericList.cs new file mode 100644 index 0000000..36181df --- /dev/null +++ b/10-1 GenerischeListeMitSuche/GenericList.cs @@ -0,0 +1,196 @@ +using System; +using System.Collections.Generic; +using System.Text; +namespace _10_1_GenerischeListeMitSuche +{ + //delegate bool MeinFilter<T>(T var); + + + class Liste<T> : IComparable<Liste<T>> where T:IComparable<T> + { + class LItem + { + public T data; + public LItem next = null, prev = null; + public LItem(T Data) { data = Data; } + + public override string ToString() + => $"Name: {data}, prev: {(prev == null ? "null" : prev.data.ToString())}, next: {(next == null ? "null" : next.data.ToString())}"; + } + + LItem first = null, last = null; + public int Count { get; private set; } + public Liste() { Count = 0; } + public int CompareTo(Liste<T> other) => Count - other.Count; + + public void AddEnd(T Data) + { + LItem newItem = new LItem(Data); + Count++; + if (first == null) + first = last = newItem; + else // Es gibt schon Listenelemente + { + last.next = newItem; + newItem.prev = last; + last = newItem; + } + } + public void AddFront(T Data) + { + LItem newItem = new LItem(Data); + Count++; + if (first == null) + first = last = newItem; + else + { + newItem.next = first; + first.prev = newItem; + first = newItem; + } + } + public void AddSorted(T InsData) + { + // 1. Fall: Leere Liste oder Anfügen am Listenende + if (first == null || last.data.CompareTo(InsData) <= 0) + AddEnd(InsData); + else if (InsData.CompareTo(first.data) <= 0) + // 2. Fall: Anfügen am Listenende + AddFront(InsData); + else + { + // Wir wissen: Das neue Element ist NICHT das erste und nicht das letzte Element + LItem newItem = new LItem(InsData); + Count++; + + LItem item = first; + while (item.next.data.CompareTo(InsData) < 0) + item = item.next; + // bis hierher identisch zur einfach-verketteten Liste + newItem.next = item.next; // Vorwärtsverkettung vom neuen Element zur Restliste + newItem.prev = item; // Rückwärtsverkettung vom neuen Elenent zum Listenanfangsstück + item.next.prev = newItem; // Rückwärtsverkettung vom nachfolgenden Listenelement + item.next = newItem; // Vorwärtsverkettung zum neuen Element + } + } + public void DeleteFirst() + { + // Fall 1: Liste ist leer + // Fall 2: Liste besteht nur aus EINEM Element + // Fall 3: Liste hat mehr als ein Element + + if (first == null) // Fall 1 + return; + Count--; + if (first == last) // Fall 2 + first = last = null; + else + { + first = first.next; + first.prev = null; + } + } + public void DeleteLast() + { + // Fall 1: Liste ist leer + // Fall 2: Liste besteht nur aus EINEM Element + // Fall 3: Liste hat mehr als ein Element + + if (first == null) // Fall 1 + return; + Count--; + if (first == last) // Fall 2 + first = last = null; + else + { + last = last.prev; + last.next = null; + } + } + public void DeleteByName(T DelData) + { + // Fall 1: Liste ist leer + // Fall 2: Das gesuchte Element befindet sich am Anfang + // Fall 3: Das gesuchte Element befindet sich am Ende + // Fall 4: Das gesuchte Element befindet sich mittendrin + + if (first == null) // Fall 1 + return; + if (first.data.CompareTo(DelData) == 0) // Fall 2 + DeleteFirst(); + else if (last.data.CompareTo(DelData) == 0) // Fall 3 + DeleteLast(); + else + { + // Fall 4: Wir wissen: + // Das zu löschende Element befindet sich weder am Anfang noch am Ende + // --> Es gibt ein Vorgänger- UND ein Nachfolger-Element + Count--; + LItem item = first.next; + while (item.next != null && item.data.CompareTo(DelData) != 0) + item = item.next; + if (item.next != null) + { + // d.h. Element wurde gefunden und item zeigt auf dieses Element + // 20 - 21 - 22 + item.prev.next = item.next; // 20 -> 22 Nachfolger + item.next.prev = item.prev; // 22 -> 20 Vorgänger + } + } + } + public void Print() + { + for (LItem item = first;item!=null;item=item.next) + { + Console.WriteLine(item.data); + } + Console.WriteLine("-------------------"); + } + public override string ToString() + { + StringBuilder sb = new StringBuilder(); + for (LItem item = first; item != null; item = item.next) + { + sb.Append(item.data.ToString()); + sb.Append(" "); + } + return sb.ToString(); + } + public void PrintReverse() + { + for (LItem item = last; item != null; item = item.prev) + { + Console.WriteLine(item.data); + } + Console.WriteLine("-------------------"); + } + public bool Exist(T findData) + { + for (LItem item = first; item != null; item = item.next) + { + if (item.data.CompareTo(findData)==0) + return true; + } + return false; + } + public IEnumerable<T> Reverse() + { + for (LItem item = last; item != null; item = item.prev) + yield return item.data; + } + public IEnumerator<T> GetEnumerator() + { + for (LItem item = first; item != null; item = item.next) + yield return item.data; + } + public delegate bool MeinFilter(T var); + //public delegate bool Predicate(T var); + + public IEnumerable<T> Filter(Predicate<T> filter) + { + for (LItem item = first; item != null; item = item.next) + if (filter(item.data)) // Filterfunktion + yield return item.data; + } + } +} diff --git a/10-1 GenerischeListeMitSuche/Program.cs b/10-1 GenerischeListeMitSuche/Program.cs new file mode 100644 index 0000000..1b9a8a2 --- /dev/null +++ b/10-1 GenerischeListeMitSuche/Program.cs @@ -0,0 +1,58 @@ +using System; + +namespace _10_1_GenerischeListeMitSuche +{ + class Kontakt : IComparable<Kontakt> + { + public string Name { get; set; } + public string Adr { get; set; } + public Kontakt(string name, string adr) + { Name = name;Adr = adr; } + public override string ToString() + => $"Name: {Name} Adresse: {Adr}"; + public int CompareTo(Kontakt other) => Name.CompareTo(other.Name); + } + class Program + { + static bool IstGeradeTest(int x) => x % 2 == 0; + static void Main(string[] args) + { + Liste<int> intListe = new Liste<int>(); + intListe.AddEnd(10); + intListe.AddEnd(11); + intListe.AddEnd(12); + intListe.AddEnd(13); + intListe.AddEnd(14); + + //foreach (var item in intListe.GeradeZahlen()) + //foreach (var item in intListe.Filter(IstGeradeTest)) // int --> bool + //foreach (var item in intListe.Filter(x => x % 2 == 0)) // nur gerade Zahlen + //foreach (var item in intListe.Filter(x => x >= 12)) // >=12 + foreach (var item in intListe.Filter(x => true)) + { + Console.WriteLine(item); + } + + + Liste<string> freunde = new Liste<string>(); + freunde.AddEnd("Anton"); + freunde.AddEnd("Berta"); + freunde.AddEnd("Claudia"); + freunde.AddEnd("Dieter"); + foreach (var item in freunde.Filter(x => x.StartsWith("B"))) + { + Console.WriteLine(item); + } + + + Liste<Kontakt> adressen = new Liste<Kontakt>(); + adressen.AddEnd(new Kontakt("Praesident", "Keßlerplatz 12")); + adressen.AddEnd(new Kontakt("Dekan-IN", "Hohfederstraße 40")); + adressen.AddEnd(new Kontakt("Dekan-AMP", "Keßlerplatz 12")); + foreach (var item in adressen.Filter(x => x.Adr.Contains("12"))) + { + Console.WriteLine(item); + } + } + } +} diff --git a/10-Ubg HashDict-Mo/10-Ubg HashDict-Mo.csproj b/10-Ubg HashDict-Mo/10-Ubg HashDict-Mo.csproj new file mode 100644 index 0000000..aed262d --- /dev/null +++ b/10-Ubg HashDict-Mo/10-Ubg HashDict-Mo.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_10_Ubg_HashDict_Mo</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/10-Ubg HashDict-Mo/HashDict.cs b/10-Ubg HashDict-Mo/HashDict.cs new file mode 100644 index 0000000..aff565d --- /dev/null +++ b/10-Ubg HashDict-Mo/HashDict.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace _10_Ubg_HashDict_Mo +{ + // Erstellen Sie eine Klasse HashDict, welches ebenfalls Schlüssel-/Wertpaare abspeichern kann. + // Allerdings sollen die Schlüssel-/Wertpaare nicht in einer Liste, sondern in einem + // hinreichend großen Feld abgelegt werden. + // Der Schlüssel soll direkt in einen Feldplatz umgerechnet werden (Hashfunktion), etwaige + // Konflikte sind geeignet zu behandeln. + // Für die Generierung eines Hashcodes gibt es bereits in Object eine Methode: + // int GetHashCode() + // Erstellen Sie + // - indexer-Zugriff (get;set) + // - GetEnumerator() + // - Contains-Methode + class HashDict<K,V> where K:IComparable<K> + { + K[] keys; + V[] values; + int length; + + public HashDict(int length=100) + { + keys = new K[length]; + values = new V[length]; + this.length = length; + } + + public V this[K key] + { + get + { + int ind= Math.Abs(key.GetHashCode()) % length; + while (keys[ind] != null && keys[ind].CompareTo(key) != 0) + ind = (ind + 1) % length; + + if (keys[ind] == null) + throw new Exception($"Der Schlüssel {key} wurde nicht gefunden"); + return values[ind]; + } + set + { + int ind = Math.Abs(key.GetHashCode()) % length; + while (keys[ind] != null && keys[ind].CompareTo(key) != 0) + ind = (ind + 1) % length; + + keys[ind] = key; + values[ind] = value; + } + } + public IEnumerator<KeyValuePaar<K, V>> GetEnumerator() + { + for (int i=0; i<length; i++) + if (keys[i]!=null) + yield return new KeyValuePaar<K, V>(keys[i], values[i]); + } + } +} diff --git a/10-Ubg HashDict-Mo/KeyValuePaar.cs b/10-Ubg HashDict-Mo/KeyValuePaar.cs new file mode 100644 index 0000000..12f3f09 --- /dev/null +++ b/10-Ubg HashDict-Mo/KeyValuePaar.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Text; + +namespace _10_Ubg_HashDict_Mo +{ + // "Wienkop" | 1614 + class KeyValuePaar<K, V> + { + public K key; + public V val; + public KeyValuePaar(K key, V val) { this.key = key;this.val = val; } + public override string ToString() => $"{key}: {val}"; + } +} diff --git a/10-Ubg HashDict-Mo/Program.cs b/10-Ubg HashDict-Mo/Program.cs new file mode 100644 index 0000000..bae822c --- /dev/null +++ b/10-Ubg HashDict-Mo/Program.cs @@ -0,0 +1,29 @@ +using System; + +namespace _10_Ubg_HashDict_Mo +{ + + class Program + { + static void Main(string[] args) + { + //Console.WriteLine("Hello World!"); + //string s = "Hallo"; + //Console.WriteLine(s.GetHashCode()); + HashDict<string, string> telbuch = new HashDict<string, string>(10); + + telbuch["Wienkop"] = "1614"; + telbuch["Otsa"] = "1855"; + telbuch["xyz"] = "3451"; + + Console.WriteLine(telbuch["Wienkop"]); + //Console.WriteLine(telbuch["zzz"]); + Console.WriteLine("-----------"); + + foreach (var item in telbuch) + { + Console.WriteLine($"Name: {item.key} -- TelNr: {item.val}"); + } + } + } +} diff --git a/10-Ubg Sets/10-Ubg Sets-Mo.csproj b/10-Ubg Sets/10-Ubg Sets-Mo.csproj new file mode 100644 index 0000000..8fcc490 --- /dev/null +++ b/10-Ubg Sets/10-Ubg Sets-Mo.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_10_Ubg_Sets</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/10-Ubg Sets/Program.cs b/10-Ubg Sets/Program.cs new file mode 100644 index 0000000..b14332c --- /dev/null +++ b/10-Ubg Sets/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace _10_Ubg_Sets +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index cff5b15..c77393c 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -71,9 +71,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-1 KeyValueList", "09-1 K EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-Ubg GenericStack-Di", "09-Ubg GenericStack-Di\09-Ubg GenericStack-Di.csproj", "{24C0A0CB-45A1-468F-A858-83B4BCA2BBCF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09-2 GenericSplitArray", "09-2 GenericSplitArray\09-2 GenericSplitArray.csproj", "{BA6C4BCE-076A-48C9-882F-3B3A26885518}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-2 GenericSplitArray", "09-2 GenericSplitArray\09-2 GenericSplitArray.csproj", "{BA6C4BCE-076A-48C9-882F-3B3A26885518}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09-3 Intro Delegates", "09-3 Intro Delegates\09-3 Intro Delegates.csproj", "{29E4DF54-C46F-4B4A-8E16-8ED1E705B199}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-3 Intro Delegates", "09-3 Intro Delegates\09-3 Intro Delegates.csproj", "{29E4DF54-C46F-4B4A-8E16-8ED1E705B199}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-Ubg HashDict-Mo", "10-Ubg HashDict-Mo\10-Ubg HashDict-Mo.csproj", "{BA4D6CD1-7029-4E45-B719-9FC1D5B17C71}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10-2 DelegateVariable", "10-1 DelegateVariable\10-2 DelegateVariable.csproj", "{675BC204-45BA-4E76-80D3-5899EF960624}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10-1 GenerischeListeMitSuche", "10-1 GenerischeListeMitSuche\10-1 GenerischeListeMitSuche.csproj", "{7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -225,6 +231,18 @@ Global {29E4DF54-C46F-4B4A-8E16-8ED1E705B199}.Debug|Any CPU.Build.0 = Debug|Any CPU {29E4DF54-C46F-4B4A-8E16-8ED1E705B199}.Release|Any CPU.ActiveCfg = Release|Any CPU {29E4DF54-C46F-4B4A-8E16-8ED1E705B199}.Release|Any CPU.Build.0 = Release|Any CPU + {BA4D6CD1-7029-4E45-B719-9FC1D5B17C71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA4D6CD1-7029-4E45-B719-9FC1D5B17C71}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA4D6CD1-7029-4E45-B719-9FC1D5B17C71}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA4D6CD1-7029-4E45-B719-9FC1D5B17C71}.Release|Any CPU.Build.0 = Release|Any CPU + {675BC204-45BA-4E76-80D3-5899EF960624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {675BC204-45BA-4E76-80D3-5899EF960624}.Debug|Any CPU.Build.0 = Debug|Any CPU + {675BC204-45BA-4E76-80D3-5899EF960624}.Release|Any CPU.ActiveCfg = Release|Any CPU + {675BC204-45BA-4E76-80D3-5899EF960624}.Release|Any CPU.Build.0 = Release|Any CPU + {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7B5C0DC3-41F0-4917-A84B-6C4B3A4D9E3F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- GitLab