diff --git a/OOP2024_AMP.sln b/OOP2024_AMP.sln index 18e2dd1ec7c27d911774047b30aa98d9bc3ebc6c..88300807acc1a8302819d45f59adc365d7f228a9 100644 --- a/OOP2024_AMP.sln +++ b/OOP2024_AMP.sln @@ -21,7 +21,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P08 CycleList", "P08 CycleL EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P09 DoubleLinkedList", "P09 DoubleLinkedList\P09 DoubleLinkedList.csproj", "{700D8B3A-C742-4E1C-A550-33730D987C57}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P10 BinTree", "P10 BinTree\P10 BinTree.csproj", "{CE6BBA02-59C6-4A56-9867-1F49C451CFBB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P10 BinTree", "P10 BinTree\P10 BinTree.csproj", "{CE6BBA02-59C6-4A56-9867-1F49C451CFBB}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P11Di Vererbung", "P11 Vererbung\P11Di Vererbung.csproj", "{B69D5E37-7CBE-4AFE-957C-493ECC4B553C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P12 Exceptions", "P12 Exceptions\P12 Exceptions.csproj", "{C09AAE98-B2F6-4190-9E0D-7448C89206B5}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P13 Generics", "P13 Generics\P13 Generics.csproj", "{C520D5E8-2156-4627-AA9A-BDC40C392356}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P11Mi Vererbung", "P11Mi Vererbung\P11Mi Vererbung.csproj", "{8F6110B2-04CA-444C-86AD-BC600B520F12}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P14 GenericList", "P14 GenericList\P14 GenericList.csproj", "{4C9F3622-88C7-4A7D-8D21-16BBFC70D8FC}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -69,6 +79,26 @@ Global {CE6BBA02-59C6-4A56-9867-1F49C451CFBB}.Debug|Any CPU.Build.0 = Debug|Any CPU {CE6BBA02-59C6-4A56-9867-1F49C451CFBB}.Release|Any CPU.ActiveCfg = Release|Any CPU {CE6BBA02-59C6-4A56-9867-1F49C451CFBB}.Release|Any CPU.Build.0 = Release|Any CPU + {B69D5E37-7CBE-4AFE-957C-493ECC4B553C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B69D5E37-7CBE-4AFE-957C-493ECC4B553C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B69D5E37-7CBE-4AFE-957C-493ECC4B553C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B69D5E37-7CBE-4AFE-957C-493ECC4B553C}.Release|Any CPU.Build.0 = Release|Any CPU + {C09AAE98-B2F6-4190-9E0D-7448C89206B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C09AAE98-B2F6-4190-9E0D-7448C89206B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C09AAE98-B2F6-4190-9E0D-7448C89206B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C09AAE98-B2F6-4190-9E0D-7448C89206B5}.Release|Any CPU.Build.0 = Release|Any CPU + {C520D5E8-2156-4627-AA9A-BDC40C392356}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C520D5E8-2156-4627-AA9A-BDC40C392356}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C520D5E8-2156-4627-AA9A-BDC40C392356}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C520D5E8-2156-4627-AA9A-BDC40C392356}.Release|Any CPU.Build.0 = Release|Any CPU + {8F6110B2-04CA-444C-86AD-BC600B520F12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8F6110B2-04CA-444C-86AD-BC600B520F12}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8F6110B2-04CA-444C-86AD-BC600B520F12}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8F6110B2-04CA-444C-86AD-BC600B520F12}.Release|Any CPU.Build.0 = Release|Any CPU + {4C9F3622-88C7-4A7D-8D21-16BBFC70D8FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C9F3622-88C7-4A7D-8D21-16BBFC70D8FC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C9F3622-88C7-4A7D-8D21-16BBFC70D8FC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C9F3622-88C7-4A7D-8D21-16BBFC70D8FC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/P10 BinTree/Program.cs b/P10 BinTree/Program.cs index 18429223ca7150f1b2cf18ba52758845d653de8d..3da1121ec4584ec2d8fd36b1b25c264fd9ef0a3b 100644 --- a/P10 BinTree/Program.cs +++ b/P10 BinTree/Program.cs @@ -18,12 +18,24 @@ namespace P10_BinTree Console.Write($"{number} - "); right?.Print(); } + public IEnumerable<int> Enumerate() + { + if (left != null) + foreach (var item in left.Enumerate()) + yield return item; + + yield return number; + + if (right != null) + foreach (var item in right.Enumerate()) + yield return item; + } } LItem? root = null; - public void Add(int number) + public void Add(int number) { LItem? neu = new LItem(number); - if (root == null) + if (root == null) root = neu; else { @@ -57,6 +69,12 @@ namespace P10_BinTree { root?.Print(); } + public IEnumerator<int> GetEnumerator() + { + if (root != null) + foreach (var item in root.Enumerate()) + yield return item; + } } class Program { @@ -64,12 +82,17 @@ namespace P10_BinTree { BTree bt = new BTree(); bt.Add(50); - bt.Add(30); - bt.Add(70); + bt.Add(25); + bt.Add(75); bt.Add(60); - bt.Add(50); - bt.Add(65); - bt.Print(); + bt.Add(40); + bt.Add(10); + bt.Add(90); + bt.Add(30); + //bt.Print(); + + foreach (int zahl in bt) + Console.WriteLine(zahl); } } -} +} \ No newline at end of file diff --git a/P11 Vererbung/P11Di Vererbung.csproj b/P11 Vererbung/P11Di Vererbung.csproj new file mode 100644 index 0000000000000000000000000000000000000000..1ba48af554c6103a5661ad6fdce82a5c072dc8e5 --- /dev/null +++ b/P11 Vererbung/P11Di Vererbung.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <RootNamespace>P11_Vererbung</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/P11 Vererbung/Program.cs b/P11 Vererbung/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..6b960da12eea7e493ba05425ed8dfc56cbcf594d --- /dev/null +++ b/P11 Vererbung/Program.cs @@ -0,0 +1,71 @@ +namespace P11_Vererbung +{ + interface ISteuern + { + double Steuern(); + } + class Hunde : ISteuern + { + public virtual double Steuern() => 120; + } + class Kfz : ISteuern + { + public string kennzeichen; + public Kfz(string Kennzeichen) + { + kennzeichen = Kennzeichen; + } + public virtual double Steuern() => -999; + } + class Pkw : Kfz + { + int hubraum; + public Pkw(string Kennzeichen, int Hubraum) : base(Kennzeichen) + { + hubraum = Hubraum; + } + public override double Steuern() => (hubraum + 99) / 100 * 10; // 10€ pro 100ccm + } + class HistoPkw : Pkw + { + public HistoPkw(string Kennzeichen, int Hubraum) : base(Kennzeichen, Hubraum) + { } + public override double Steuern() + { + return 0; + } + } + class Lkw : Kfz + { + int gewicht; + public Lkw(string Kennzeichen, int Gewicht) : base(Kennzeichen) + { + gewicht = Gewicht; + } + public override double Steuern() => (gewicht) / 1000 * 25; // 25€ pro 1000 kg + } + class Program + { + static void Main(string[] args) + { + //Pkw[] pkws = new Pkw[10]; + //pkws[0] = new Pkw("N-AB 123", 1598); + //Lkw[] lkws = new Lkw[10]; + //lkws[0] = new Lkw("AN-XY 345", 25000); + //foreach (Pkw p in pkws) { } + //foreach (Lkw l in lkws) { } + + ISteuern[] kfzs = new Kfz[10]; + kfzs[0] = new Kfz("xx-xx yyyy"); + kfzs[1] = new Pkw("N-AB 123", 1598); + kfzs[2] = new Lkw("AN-XY 345", 25000); + kfzs[3] = new HistoPkw("ER-KL 333H", 1423); + kfzs[4] = new Hunde(); + foreach (Kfz k in kfzs) + { + if (k != null) + Console.WriteLine($"{k.kennzeichen}: {k.Steuern()}"); + } + } + } +} diff --git a/P11Mi Vererbung/P11Mi Vererbung.csproj b/P11Mi Vererbung/P11Mi Vererbung.csproj new file mode 100644 index 0000000000000000000000000000000000000000..f7f7e8590b4636fa18e31c5c352b5abde776d86b --- /dev/null +++ b/P11Mi Vererbung/P11Mi Vererbung.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <RootNamespace>P11Mi_Vererbung</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/P11Mi Vererbung/Program.cs b/P11Mi Vererbung/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..c4b355ff76c0da937ce3d0b199763ad5938fea06 --- /dev/null +++ b/P11Mi Vererbung/Program.cs @@ -0,0 +1,92 @@ +namespace P11Mi_Vererbung +{ + interface IBesteuerbar + { + double Steuern(); + } + class Hunde : IBesteuerbar + { + public double Steuern() => 120; + } + abstract class Kfz : IBesteuerbar + { + public string kennzeichen { get; private set; } + // Properties können in abgeleiteten Klassen überschrieben werden + //public virtual double Steuern() => -9999; + // virtual: Methode KANN in abgeleiteten Klassen überschrieben werden + public abstract double Steuern(); + // abstract: Methode MUSS in abgeleiteten Klassen überschrieben werden + public Kfz(string kennzeichen) + { + this.kennzeichen = kennzeichen; + } + public override string ToString() + { + return base.ToString(); + } + } + class Pkw:Kfz + { + int hubraum; + public Pkw(string kennzeichen, int hubraum) : base(kennzeichen) + // base: Aufruf des Basisklassenkonstruktors + { + // this.kennzeichen = kennzeichen; -- private set + this.hubraum = hubraum; + } + public override double Steuern() => (hubraum + 99) / 100 * 6; // 6€ pro 100 ccm + } + class HistPkw : Pkw + { + public HistPkw(string kennzeichen, int hubraum) : base(kennzeichen,hubraum) + { } + public override double Steuern() => 0; // steuerbefreit + } + class Lkw:Kfz + { + int gewicht; + public Lkw (string kennzeichen, int gewicht) : base(kennzeichen) + { + // this.kennzeichen = kennzeichen; -- private set + this.gewicht = gewicht; + } + public override double Steuern() => 20 * gewicht / 1000; // 20€ pro 1000kg + } + class Program + { + static void Main(string[] args) + { + #region Old-Style + //Pkw[] pkws = new Pkw[10]; + //Lkw[] lkws = new Lkw[10]; + //pkws[0] = new Pkw("N-AB 123", 1498); + //lkws[0] = new Lkw("ER-XY 345", 25000); + //foreach (var pk in pkws) + //{ + // if (pk != null) + // Console.WriteLine($"{pk.kennzeichen}, {pk.Steuern()}"); + //} + //foreach (var lk in lkws) + //{ + // if (lk != null) + // Console.WriteLine($"{lk.kennzeichen}, {lk.Steuern()}"); + //} + #endregion + IBesteuerbar[] kfzs = new IBesteuerbar[10]; + kfzs[0] = new Pkw("N-AB 123", 1498); + kfzs[1] = new Lkw("ER-XY 345", 25000); + kfzs[2] = new HistPkw("FÜ-DF 456H", 1498); + kfzs[3] = new Hunde(); + //kfzs[3] = new Kfz("xx-xx 878"); + foreach (IBesteuerbar fz in kfzs) + { + if (fz != null) + { + //if (fz is Pkw) + //Console.WriteLine($"{fz.kennzeichen}, {fz.Steuern()}"); + Console.WriteLine($"{fz.Steuern()}"); + } + } + } + } +} diff --git a/P12 Exceptions/P12 Exceptions.csproj b/P12 Exceptions/P12 Exceptions.csproj new file mode 100644 index 0000000000000000000000000000000000000000..3878ed77b84f5010510dfbfc2165657497c4b6cf --- /dev/null +++ b/P12 Exceptions/P12 Exceptions.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <RootNamespace>P12_Exceptions</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/P12 Exceptions/Program.cs b/P12 Exceptions/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..e25be22a5020c69ed250628535416b57ca491d32 --- /dev/null +++ b/P12 Exceptions/Program.cs @@ -0,0 +1,61 @@ +namespace P12_Exceptions +{ + class MeineException : Exception + { + public MeineException(string s) : base(s) { } + } + internal class Program + { + static void Main(string[] args) + { + Console.WriteLine( "Main: Start"); + try + { + Console.WriteLine(Top(1000)); + } + catch (ArgumentOutOfRangeException) + { + Console.WriteLine("Schau da, ein ArgumentOutRangeException-Fehler"); + } + catch (MeineException) + { + Console.WriteLine("MeineException-Fehler"); + } + catch (IndexOutOfRangeException ex) + { + Console.WriteLine($"Fehler gefangen; Fehlertext: {ex.Message}"); + } + finally + { + Console.WriteLine( "Main: finally"); + } + Console.WriteLine("Main: Ende"); + } + static int Top(int n) + { + Console.WriteLine("Top: Start"); + try + { + int x = 100 / n; + Low(x); + Console.WriteLine("Top: Ende"); + return x; + } + finally { Console.WriteLine("Top: Finally -- Das machen wir noch zuende!"); } + } + + static int Low(int x) + { + Console.WriteLine("Low: Start"); + if (x == 0) { + MeineException a = + new MeineException("Durch null zu teilen ist verboten!"); + throw a; + } + int y= 100 / x; + Console.WriteLine("Low: Ende"); + return y; + } + } + +} diff --git a/P13 Generics/P13 Generics.csproj b/P13 Generics/P13 Generics.csproj new file mode 100644 index 0000000000000000000000000000000000000000..80c8bbb7350fad1b8ed21613350b3bdb1bad41aa --- /dev/null +++ b/P13 Generics/P13 Generics.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <RootNamespace>P13_Generics</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/P13 Generics/Program.cs b/P13 Generics/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..a447cdb876823022e94294e01ba1d9ec3ba3eff2 --- /dev/null +++ b/P13 Generics/Program.cs @@ -0,0 +1,33 @@ +namespace P13_Generics +{ + class Person : IComparable<Person> + { + public string Name { get; set; } + public int Alter { get; set; } + public override string ToString()=>$"{Name}, {Alter}"; + public int CompareTo(Person other) => Name.CompareTo(other.Name); + //public int CompareTo(Person other) => Alter - other.Alter; + } + internal class Program + { + //static int Max(int a, int b) => a > b ? a : b; + //static double Max(double a, double b) => a > b ? a : b; + //static string Max(string a, string b) => a.CompareTo(b)>0 ? a : b; + static T Max<T>(T a, T b) where T:IComparable<T> + => a.CompareTo(b)>0 ? a : b; + // where T:IComparable<T> ~ Zusicherung: T implementiert das Interface IComparable + + // CompareTo(a,b) ~ a-b + // neg ~ a<b + // 0 ~ a==b + // pos ~ a>b + static void Main(string[] args) + { + Console.WriteLine(Max(3, 4)); + Console.WriteLine(Max("Meier,Anton","Meier,Berta")); + int x = 1.CompareTo(2) > 0 ? 1 : 2; + Console.WriteLine(Max(new Person { Name = "Anton", Alter=22}, + new Person { Name="Berta", Alter =21 })); + } + } +} diff --git a/P14 GenericList/DblList.cs b/P14 GenericList/DblList.cs new file mode 100644 index 0000000000000000000000000000000000000000..4cdd984b1b7a209445bed462a686dd4594a99c61 --- /dev/null +++ b/P14 GenericList/DblList.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace P14_GenericList +{ + class DblList<T>:IComparable<DblList<T>> + where T:IComparable<T> + { + class LItem + { + public T? data; + public LItem? next = null, prev = null; + public LItem(T? data) + { + this.data = data; + } + } + LItem? first = null, last = null; + int anz = 0; + public int CompareTo(DblList<T> other) => anz - other.anz; + public override string ToString() => $"DblList, {anz} Elemente"; + public void AddLast(T? data) + { + anz++; + LItem? lItem = new(data); + if (last == null) + first = last = lItem; + else + { + lItem.prev = last; + last = last.next = lItem; + } + } + public void AddFirst(T? data) + { + anz++; + LItem? lItem = new(data); + if (first == null) + first = last = lItem; + else + { + lItem.next = first; + first = first.prev = lItem; + } + } + public void AddSorted(T data) + { + // 1.Fall: + // Leere Liste + // oder neues Element > letztes Element + if (last == null || data.CompareTo(last.data) > 0) + AddLast(data); + + // Es existiert mind. ein Element in Liste + // Neues Element wird nicht letztes Element + else + { + // 2.Fall: + // Neues Element <= erstes Element + if (data.CompareTo(first!.data) <= 0) + AddFirst(data); + + // 3.Fall: + // In Mitte einfügen + else + { + LItem? neu = new(data); + anz++; + + LItem? tmp = first.next; + // Durchlaufen der Liste + // Wenn Name des nächsten Elements kleiner ist => weitergehen + // tmp verweist auf das Element VOR DEM eingefügt wird + while (tmp!.data.CompareTo(data) < 0) + tmp = tmp.next; + + // Einfügen des Elements + neu.prev = tmp.prev; + neu.next = tmp; + + tmp.prev.next = neu; + tmp.prev = neu; + } + } + } + public void PrintList() + { + for (LItem? item = first; item != null; item = item.next) + { + Console.WriteLine(item.data); + } + Console.WriteLine("------------"); + } + public IEnumerator<T?> GetEnumerator() + { + for (LItem? item = first; item != null; item = item.next) + yield return item.data; + } + public void ReversePrintList() + { + for (LItem? item = last; item != null; item = item.prev) + { + Console.WriteLine(item.data); + } + Console.WriteLine("------------"); + } + } +} diff --git a/P14 GenericList/P14 GenericList.csproj b/P14 GenericList/P14 GenericList.csproj new file mode 100644 index 0000000000000000000000000000000000000000..cf0e768edb94b6cea7f14c8c55b6104ad8e6ad56 --- /dev/null +++ b/P14 GenericList/P14 GenericList.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <RootNamespace>P14_GenericList</RootNamespace> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/P14 GenericList/Person.cs b/P14 GenericList/Person.cs new file mode 100644 index 0000000000000000000000000000000000000000..823678aecf1ef5c4ed09703268ec35b0536a5f8f --- /dev/null +++ b/P14 GenericList/Person.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace P14_GenericList +{ + class Person : IComparable<Person> + { + public string Name { get; set; } + public int Alter { get; set; } + public override string ToString() => $"{Name}, {Alter}"; + public Person(string name, int alter) + { + Name = name; + Alter = alter; + } + + public int CompareTo(Person other) => Name.CompareTo(other.Name); + //public int CompareTo(Person other) => Alter - other.Alter; + } +} diff --git a/P14 GenericList/Program.cs b/P14 GenericList/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..15b945df23de53f3329983dab1f0b6580986b54d --- /dev/null +++ b/P14 GenericList/Program.cs @@ -0,0 +1,39 @@ +namespace P14_GenericList +{ + internal class Program + { + static void Main(string[] args) + { + DblList<int> il = new DblList<int>(); + il.AddSorted(3); + il.AddSorted(1); + il.AddSorted(2); + il.AddSorted(4); + foreach (int i in il) + { + Console.WriteLine(i); + } + + DblList<Person> pl = new DblList<Person>(); + pl.AddSorted(new Person("Berta", 22)); + pl.AddSorted(new Person("Claudia", 22)); + pl.AddSorted(new Person("Anton", 22)); + + foreach (var item in pl) + { + Console.WriteLine(item); + } + DblList<DblList<Person>> pll = new(); + DblList<Person> pl2 = new DblList<Person>(); + pl2.AddSorted(new Person("Xaver", 63)); + pl2.AddSorted(new Person("Zacharias", 69)); + + pll.AddSorted(pl); + pll.AddSorted(pl2); + foreach (var item in pll) + { + Console.WriteLine(item); + } + } + } +}