diff --git a/09-1 KeyValueList/KeyValueList.cs b/09-1 KeyValueList/KeyValueList.cs index 9b83155fa905e28d21755b5531ad4cc2a1b50ef3..57610cadbcc84aba2e7a446782bd722139d13dc5 100644 --- a/09-1 KeyValueList/KeyValueList.cs +++ b/09-1 KeyValueList/KeyValueList.cs @@ -4,7 +4,7 @@ using System.Text; namespace _09_1_KeyValueList { - class KeyValueList<K,V> where K:IComparable<K> + class KeyValueList<K,V> where K:IEquatable<K> { private class KVItem { @@ -17,7 +17,7 @@ namespace _09_1_KeyValueList KVItem first = null, last = null; int count = 0; - public void AddEnd(K key, V val) + private void AddEnd(K key, V val) { KVItem newItem = new KVItem(key, val); // 1. Neues Element anlegen count++; @@ -37,7 +37,7 @@ namespace _09_1_KeyValueList private KVItem ItemSearch(K searchKey) { for (KVItem item = first; item != null; item = item.next) - if (item.key.CompareTo(searchKey) == 0) + if (item.key.Equals(searchKey)) return item; return null; } diff --git a/09-1 KeyValueList/Program.cs b/09-1 KeyValueList/Program.cs index 2779daf4a02d9819ce326f50c538e045db9286a5..3af63347deffaf7918e70326e3bc516e4bcbbd75 100644 --- a/09-1 KeyValueList/Program.cs +++ b/09-1 KeyValueList/Program.cs @@ -45,6 +45,61 @@ namespace _09_1_KeyValueList Console.WriteLine(item); } } + Console.WriteLine("*************"); + + #region Terminkalender2 + KeyValueList<string, KeyValueList<string, string>> terminkalender2 = new KeyValueList<string, KeyValueList<string, string>>(); + terminkalender2["17.05.2021"] = new KeyValueList<string, string>(); + terminkalender2["17.05.2021"]["09:45"] = "Prog2 Übung"; + terminkalender2["17.05.2021"]["11:30"] = "Prog2 Vorlesung"; + + terminkalender2["18.05.2021"] = new KeyValueList<string, string>(); + terminkalender2["18.05.2021"]["08:00"] = "Prog2 Übung"; + terminkalender2["18.05.2021"]["09:45"] = "Prog2 Vorlesung"; + + foreach (var tag in terminkalender2) + { + Console.WriteLine($"\nTermine am {tag.key}:"); + foreach (var termin in tag.val) + { + Console.WriteLine(termin); + } + Console.WriteLine("---------"); + } + #endregion + + #region Rezeptverwaltung + // Pfannkuchen, Zutaten(Mehl, Milch, Eier), Zubereitung (Schritt1, Schritt2, ...) + KeyValueList<string, (List<string>, List<string>)> rezepte = new KeyValueList<string, (List<string>, List<string>)>(); + + List<string> zutaten = new List<string>(); + zutaten.Add("Mehl"); + zutaten.Add("Milch"); + zutaten.Add("Eier"); + List<string> zubereitung = new List<string>(); + zubereitung.Add("Eier in Schüssel schlagen"); + zubereitung.Add("mit Mehl und Milch verrühren"); + + rezepte["Pfannkuchen"] = (zutaten, zubereitung); + + + List<string> zutaten2; + List<string> zubereitung2; + (zutaten2, zubereitung2) = rezepte["Pfannkuchen"]; + + + Console.WriteLine("\nZutaten: "); + foreach (var item in zutaten2) + { + Console.WriteLine(item); + } + + Console.WriteLine("\nZubereitung:"); + foreach (var item in zubereitung2) + { + Console.WriteLine(item); + } + #endregion } } } diff --git a/09-2 GenericSplitArray/09-2 GenericSplitArray.csproj b/09-2 GenericSplitArray/09-2 GenericSplitArray.csproj new file mode 100644 index 0000000000000000000000000000000000000000..da55c59d46fcfbe1745448bb9b68d009b3efc7db --- /dev/null +++ b/09-2 GenericSplitArray/09-2 GenericSplitArray.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_09_2_GenericSplitArray</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/09-2 GenericSplitArray/Program.cs b/09-2 GenericSplitArray/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..9b690cad6f669f3ef770b381b5e08c46907cb14b --- /dev/null +++ b/09-2 GenericSplitArray/Program.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace _09_2_GenericSplitArray +{ + class Program + { + static T[] SplitArray<T>(T[] arr, T limit) where T:IComparable<T> + { + List<T> res = new List<T>(); + foreach (var item in arr) + { + if (item.CompareTo(limit) >= 0) + res.Add(item); + } + return res.ToArray(); + } + + + static void Main(string[] args) + { + int [] f = { 10, 30, 20, 45, 50, 80 }; + int[] r = SplitArray<int>(f, 30); + foreach (var item in r) + { + Console.WriteLine(item); + } + } + } +} diff --git a/09-3 Intro Delegates/09-3 Intro Delegates.csproj b/09-3 Intro Delegates/09-3 Intro Delegates.csproj new file mode 100644 index 0000000000000000000000000000000000000000..8161c919abaf7acdb1fce86e21aac5c39efacda2 --- /dev/null +++ b/09-3 Intro Delegates/09-3 Intro Delegates.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_09_3_Intro_Delegates</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/09-3 Intro Delegates/Program.cs b/09-3 Intro Delegates/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..d1b3258ed5c18e65a6dd5d0aed924de585cbaa1b --- /dev/null +++ b/09-3 Intro Delegates/Program.cs @@ -0,0 +1,27 @@ +using System; + +namespace _09_3_Intro_Delegates +{ + class Program + { + delegate double MyFunction(double zzz); + static void Wertetabelle(double von, double bis, MyFunction funktion) + { + for (double x = von; x <= bis; x += 1) + Console.WriteLine($"{x,6:f2} | {funktion(x)}"); + Console.WriteLine("------------------"); + } + + // ^^^^^^^^ FIRMA A ^^^^^^ + + // -------- FIRMA B ------ + + static double Quadrieren(double xxx) => xxx * xxx; + + static void Main(string[] args) + { + Wertetabelle(0, 3, Math.Sin); + Wertetabelle(0, 3, Quadrieren); + } + } +} diff --git a/09-Ubg GenericStack-Di/09-Ubg GenericStack-Di.csproj b/09-Ubg GenericStack-Di/09-Ubg GenericStack-Di.csproj new file mode 100644 index 0000000000000000000000000000000000000000..c951bb84ee885e78a3d3046a148b5482a38fbbb0 --- /dev/null +++ b/09-Ubg GenericStack-Di/09-Ubg GenericStack-Di.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_09_Ubg_GenericStack_Di</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/09-Ubg GenericStack-Di/GenericStack.cs b/09-Ubg GenericStack-Di/GenericStack.cs new file mode 100644 index 0000000000000000000000000000000000000000..1cd211fe74e98aa81fca34c913c71908a5f1f922 --- /dev/null +++ b/09-Ubg GenericStack-Di/GenericStack.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Text; + +// Erstellen Sie mit Hilfe einer Liste einen generischen Stapel (Stack), in dem beliebige Elemente +// gespeichert werden können. +// Ein Stack ist eine Datenstruktur, ähnlich wie ein Stapel auf Ihrem Schreibtisch. Das, was +// zuoberst aufliegt, wird auch zuerst wieder heruntergenommen. +// Sie können einen Stapel leicht mithilfe einer Liste aufbauen. Hierfür benötigen Sie folgenden Methoden: +// - AddFirst: Als erstes Element einfügen +// - GetFirst(bool removeFromStack=true): Liefert das erste Element zurück und entfernt es vom Stapel; kann +// per removeFromStack unterbunden werden +// - bool IsEmpty signalisiert, ob der Stack leer ist +// - Contains(T): Sucht nach dem übergebenen Element und liefert true/false zurück +// - GetEnumerator() : Ermöglicht einen Durchlauf durch den Stapel und liefert nach und nach alle Elemente zurück + +namespace _09_Ubg_GenericStack_Di +{ + class GenericStack<T> where T:IEquatable<T> + { // where-Einschränkungen: + // class ~ Referenzvariable + // struct ~ Wertetyp + // new ~ T muss einen Defaultkonstruktor haben + class StackItem + { + public T data; + public StackItem next; + public StackItem(T Data) { data = Data; next = null; } + } + StackItem first = null; + + public void Addfirst(T Data) + { + StackItem item = new StackItem(Data); + item.next = first; + first = item; + } + public T GetFirst(bool removeFromStack = true) + { + //if (first == null) + // return default(T); + //T variable = default(T); + + if (first == null) + throw new Exception("Stack ist leer!"); + + T res = first.data; + if (removeFromStack) + first = first.next; + return res; + + //first = first.next; FALSCH!!! + //return first.data; + } + public bool IsEmpty { get => first == null; } + + public IEnumerator<T> GetEnumerator() + { + for (StackItem item = first; item!=null; item=item.next) + { + yield return item.data; + } + } + public bool Contains(T searchData) + { + for (StackItem item = first; item != null; item = item.next) + { + if (item.data.Equals(searchData)) + return true; + } + return false; + } + } +} diff --git a/09-Ubg GenericStack-Di/Program.cs b/09-Ubg GenericStack-Di/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..07292035e04a157a6eaa7957baab8de510c1c95f --- /dev/null +++ b/09-Ubg GenericStack-Di/Program.cs @@ -0,0 +1,33 @@ +using System; + +namespace _09_Ubg_GenericStack_Di +{ + class Person : IEquatable<Person> + { + string name, vorname; + public bool Equals(Person other) + { + return name.Equals(other.name); + } + } + class Program + { + static void Main(string[] args) + { + GenericStack<int> iStack = new GenericStack<int>(); + iStack.Addfirst(27); + iStack.Addfirst(34); + iStack.Addfirst(48); + //while (!iStack.IsEmpty) + //{ + // Console.WriteLine(iStack.GetFirst()); + //} + Console.WriteLine(iStack.Contains(34)); + foreach (var item in iStack) + { + Console.WriteLine(item); + } + GenericStack<Person> pStack = new GenericStack<Person>(); + } + } +} diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index 3b01304df6c8ab2a37382b956e30fdb654088d15..cff5b157d2a9a5bd5345646f62c4b54b0ac31f8b 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -65,9 +65,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "08-UbgExceptionsDi", "08-Ub EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "08-3 GenericList", "0ß8-3 GenericList\08-3 GenericList.csproj", "{05E06BC5-768E-44D2-82C3-B473AEEDBD6B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09-Ubg GenericStack-Mo", "09-Ubg GenericStack-Mo\09-Ubg GenericStack-Mo.csproj", "{E81D4D2B-71A8-4B75-827B-18249E4D30B5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-Ubg GenericStack-Mo", "09-Ubg GenericStack-Mo\09-Ubg GenericStack-Mo.csproj", "{E81D4D2B-71A8-4B75-827B-18249E4D30B5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "09-1 KeyValueList", "09-1 KeyValueList\09-1 KeyValueList.csproj", "{DCBBE729-6B39-46C4-A072-8B793A6245DF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "09-1 KeyValueList", "09-1 KeyValueList\09-1 KeyValueList.csproj", "{DCBBE729-6B39-46C4-A072-8B793A6245DF}" +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}" +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}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -207,6 +213,18 @@ Global {DCBBE729-6B39-46C4-A072-8B793A6245DF}.Debug|Any CPU.Build.0 = Debug|Any CPU {DCBBE729-6B39-46C4-A072-8B793A6245DF}.Release|Any CPU.ActiveCfg = Release|Any CPU {DCBBE729-6B39-46C4-A072-8B793A6245DF}.Release|Any CPU.Build.0 = Release|Any CPU + {24C0A0CB-45A1-468F-A858-83B4BCA2BBCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {24C0A0CB-45A1-468F-A858-83B4BCA2BBCF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {24C0A0CB-45A1-468F-A858-83B4BCA2BBCF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {24C0A0CB-45A1-468F-A858-83B4BCA2BBCF}.Release|Any CPU.Build.0 = Release|Any CPU + {BA6C4BCE-076A-48C9-882F-3B3A26885518}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA6C4BCE-076A-48C9-882F-3B3A26885518}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA6C4BCE-076A-48C9-882F-3B3A26885518}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA6C4BCE-076A-48C9-882F-3B3A26885518}.Release|Any CPU.Build.0 = Release|Any CPU + {29E4DF54-C46F-4B4A-8E16-8ED1E705B199}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {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 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE