From caafb90259dbc84f9641e02802eb5c62ee6012d5 Mon Sep 17 00:00:00 2001 From: Uwe Wienkop <uwe.wienkop@th-nuernberg.de> Date: Mon, 21 Jun 2021 13:07:02 +0200 Subject: [PATCH] 2021-06-21 Wdhlg. delegates + generics --- 13-Ubg ApplyAll/13-1 ApplyAll.csproj | 9 ++ 13-Ubg ApplyAll/Program.cs | 61 +++++++++++++ 13-UbgCycleList-Mo/13-UbgCycleList-Mo.csproj | 9 ++ 13-UbgCycleList-Mo/Program.cs | 92 ++++++++++++++++++++ Prog2WienkopSS2021.sln | 14 ++- 5 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 13-Ubg ApplyAll/13-1 ApplyAll.csproj create mode 100644 13-Ubg ApplyAll/Program.cs create mode 100644 13-UbgCycleList-Mo/13-UbgCycleList-Mo.csproj create mode 100644 13-UbgCycleList-Mo/Program.cs diff --git a/13-Ubg ApplyAll/13-1 ApplyAll.csproj b/13-Ubg ApplyAll/13-1 ApplyAll.csproj new file mode 100644 index 0000000..450eba1 --- /dev/null +++ b/13-Ubg ApplyAll/13-1 ApplyAll.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_13_Ubg_ApplyAll</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/13-Ubg ApplyAll/Program.cs b/13-Ubg ApplyAll/Program.cs new file mode 100644 index 0000000..f5fe9ab --- /dev/null +++ b/13-Ubg ApplyAll/Program.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; + +namespace _13_Ubg_ApplyAll +{ + /* Schreiben Sie die folgenden generischen Methoden: + * + * 1) ApplyAll, die für Felder eines beliebigen Typs, eine übergebene Funktion auf alle Feldelemente anwendet + * und das derart modifizierte Feld zurückliefert. + * + * 2) Select soll eine übergebene Filterfunktion auf alle Feldelemente anwenden und nur diejenigen zurückliefern, + * bei denen das Feldelement die Filterkriterien erfüllt. Hierfür kann die generische MS Liste<> verwendet werden. + * + * Schreiben Sie für unterschiedliche Basistypen Lambda-Ausdrücke oder anonyme Methoden + */ + class Program + { + delegate T MeineFkt<T>(T x); + //delegate int MeineFkt<int>(int x) + delegate bool MeinFilter<T>(T x); + + + // static int[] ApplyAll<int>(MeineFkt<int> fkt, params int[] werte) + // static string[] ApplyAll<string>(MeineFkt<string> fkt, params string[] werte) + static T[] ApplyAll<T>(MeineFkt<T> fkt, params T[] werte) + { + T[] res = new T[werte.Length]; + for (int i = 0; i < werte.Length; i++) + { + res[i] = fkt(werte[i]); + Console.Write($"{res[i]} "); + } + Console.WriteLine(); + return res; + } + + static T[] Select<T>(MeinFilter<T> filter, params T[] werte) + { + List<T> res = new List<T>(); + foreach (var item in werte) + { + if (filter(item)) + { + res.Add(item); + Console.Write($"{item} "); + } + } + Console.WriteLine(); + return res.ToArray(); + } + static void Main(string[] args) + { + ApplyAll(x => x + 1, 10, 30, 40, 50, 60, 70, 80, 100); + ApplyAll(x => (int) (x * 0.5), 10, 30, 40, 50, 60, 70, 80, 100); + ApplyAll(delegate (int x) { return x + 1; }, ApplyAll(x => 2 * x, 10, 20, 30)); + ApplyAll(x => x + 1, "Anton","Berta","Claudia"); + + Select(x => x>50, 10, 30, 40, 50, 60, 70, 80, 100); + } + } +} diff --git a/13-UbgCycleList-Mo/13-UbgCycleList-Mo.csproj b/13-UbgCycleList-Mo/13-UbgCycleList-Mo.csproj new file mode 100644 index 0000000..0f5226b --- /dev/null +++ b/13-UbgCycleList-Mo/13-UbgCycleList-Mo.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_13_UbgCycleList_Mo</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/13-UbgCycleList-Mo/Program.cs b/13-UbgCycleList-Mo/Program.cs new file mode 100644 index 0000000..94fd212 --- /dev/null +++ b/13-UbgCycleList-Mo/Program.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; + +namespace _13_UbgCycleList_Mo +{ + class CycleList + { + class LItem + { + public string name; + public LItem next; + public LItem(string Name) { name = Name; next = null; } + } + LItem first = null, last = null; + LItem nextIn, nextOut; + int capacity; + public int Count { get; private set; } // Anzahl gerade im Puffer gespeicherter Elemente + public void AddEnd(string Name) // wie bisher + { + LItem newItem = new LItem(Name); + if (first == null) + first = last = newItem; + else + { + last.next = newItem; + last = newItem; + } + } + public CycleList() + { + for (int i = 0; i < 10; i++) + AddEnd(null); + last.next = first; + Count = 0; + capacity = 10; + nextIn = nextOut = first; + } + public void Write(string Name) + { + if (Count == capacity) + throw new Exception("Buffer Full"); + nextIn.name = Name; + nextIn = nextIn.next; + Count++; + } + public string Read() + { + if (Count == 0) + throw new Exception("Buffer Empty"); + + string tmp = nextOut.name; + nextOut.name = null; + nextOut = nextOut.next; + Count--; + return tmp; + } + public void Enlarge(int n) + { + // 1) "Z" suchen + LItem item = first; + while (item.next != nextIn) + item = item.next; + // JETZT: item zeigt auf "Z", nextIn ist der Nachfolger + + for (int i = 0; i < n; i++) + { + LItem newItem = new LItem(null); + newItem.next = item.next; + item.next = newItem; + capacity++; + } + nextIn = item.next; + } + public IEnumerator<string> GetEnumerator() + { + LItem item = nextOut; + for (int i = 0; i < Count; i++) + { + yield return item.name; + item = item.next; + } + } + + } + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index 6c9328c..c72ef9e 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -107,7 +107,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "12-1 NewOverride", "12-1New EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "12-2 Delegateaufrufausbreitung", "12-2 Delegateaufrufausbreitung\12-2 Delegateaufrufausbreitung.csproj", "{30C34E16-FFAF-48A5-8F3F-AE3A7CF7B56D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "12-3 VererbungRedo", "12-3 VererbungRedo\12-3 VererbungRedo.csproj", "{882C4C12-B62B-4BDB-A52C-C1D62E9B0D6A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "12-3 VererbungRedo", "12-3 VererbungRedo\12-3 VererbungRedo.csproj", "{882C4C12-B62B-4BDB-A52C-C1D62E9B0D6A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "13-1 ApplyAll", "13-Ubg ApplyAll\13-1 ApplyAll.csproj", "{1335DAC1-39A7-4CB1-9893-F4017A57E2BA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "13-UbgCycleList-Mo", "13-UbgCycleList-Mo\13-UbgCycleList-Mo.csproj", "{C1594C99-2A3F-4948-AC3E-ED7D16379C48}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -327,6 +331,14 @@ Global {882C4C12-B62B-4BDB-A52C-C1D62E9B0D6A}.Debug|Any CPU.Build.0 = Debug|Any CPU {882C4C12-B62B-4BDB-A52C-C1D62E9B0D6A}.Release|Any CPU.ActiveCfg = Release|Any CPU {882C4C12-B62B-4BDB-A52C-C1D62E9B0D6A}.Release|Any CPU.Build.0 = Release|Any CPU + {1335DAC1-39A7-4CB1-9893-F4017A57E2BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1335DAC1-39A7-4CB1-9893-F4017A57E2BA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1335DAC1-39A7-4CB1-9893-F4017A57E2BA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1335DAC1-39A7-4CB1-9893-F4017A57E2BA}.Release|Any CPU.Build.0 = Release|Any CPU + {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C1594C99-2A3F-4948-AC3E-ED7D16379C48}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE -- GitLab