From 1fb4c15d368c5f44ee9c00266426f7d008842e1b Mon Sep 17 00:00:00 2001 From: wienkop <uwe.wienkop@th-nuernberg.de> Date: Mon, 26 Apr 2021 13:14:00 +0200 Subject: [PATCH] 2021-04-26 Intro Vererbung --- 05-2 IntroVererbungKfz/KfzCoolStyle.cs | 40 +++++++++++- 05-2 IntroVererbungKfz/KfzOldStyle.cs | 30 ++++++++- 05-2 IntroVererbungKfz/Program.cs | 41 +++++++++++- 06-UbgBinTree-Mo/BinTree.cs | 88 ++++++++++++++++++++++++++ 06-UbgBinTree-Mo/Program.cs | 14 +++- 5 files changed, 209 insertions(+), 4 deletions(-) diff --git a/05-2 IntroVererbungKfz/KfzCoolStyle.cs b/05-2 IntroVererbungKfz/KfzCoolStyle.cs index b16b11e..1e41dd2 100644 --- a/05-2 IntroVererbungKfz/KfzCoolStyle.cs +++ b/05-2 IntroVererbungKfz/KfzCoolStyle.cs @@ -4,7 +4,45 @@ using System.Text; namespace _05_2_IntroVererbungKfz { - class KfzCoolStyle + class Kfz // KfzCoolStyle + // Ist die Basisklasse von mehreren kfz-Ausprägungen { + string kennzeichen; + public virtual double Steuern() { return -1; } + // virtual ~ Diese Methode kann/darf in einer + // von Kfz abgeleiteten Klasse überschrieben werden + } + + class Pkw : Kfz // Pkw ist-ein Kfz + // Pkw ist eine Spezialisierung eines Kfz + // Pkw ist ein Kfz mit Zusatzeigenschaften + // Pkw erbt von Kfz + // Pkw ist von Kfz abgeleitet + { + int hubraum=1599; + int co2; + public override double Steuern() + { + return (hubraum + 99) / 100 * 2; + } + } + + class Motorrad : Kfz + { + int hubraum = 250; + public override double Steuern() + { + return (hubraum + 24) / 25 * 1.84; + } + } + + class LKW : Kfz + { + int gewicht; + int hubraum; + //public override double Steuern() + //{ + // return 556; + //} } } diff --git a/05-2 IntroVererbungKfz/KfzOldStyle.cs b/05-2 IntroVererbungKfz/KfzOldStyle.cs index adb183a..718812b 100644 --- a/05-2 IntroVererbungKfz/KfzOldStyle.cs +++ b/05-2 IntroVererbungKfz/KfzOldStyle.cs @@ -4,7 +4,35 @@ using System.Text; namespace _05_2_IntroVererbungKfz { - class KfzOldStyle + class Pkw_old { + int hubraum; + int co2; + + public double Steuern() + { + return (hubraum + 99) / 100 * 2; + } + } + + class Motorrad_old + { + int hubraum; + public double Steuern() + { + return (hubraum + 24) / 25 * 1.84; + } } + + class LKW_old + { + int gewicht; + int hubraum; + public double Steuern() + { + return 556; + } + } + + } diff --git a/05-2 IntroVererbungKfz/Program.cs b/05-2 IntroVererbungKfz/Program.cs index 60ff173..8a5c8c8 100644 --- a/05-2 IntroVererbungKfz/Program.cs +++ b/05-2 IntroVererbungKfz/Program.cs @@ -31,11 +31,50 @@ namespace _05_2_IntroVererbungKfz //Lkw-Steuer richtet sich auch nach Schadstoffklassen und Geräuschklassen //Der Höchstsatz für die günstigste Lkw-Steuerklasse beträgt 556,00 Euro + // Steuerberechnung EScouter + // 15€ pro Jahr + class Program { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + #region OldStyle + //Pkw[] pkw = new Pkw[1000]; + //LKW[] lkw = new LKW[1000]; + //Motorrad[] motorrad = new Motorrad[1000]; + + //Console.WriteLine("Neues Fahrzeug anlegen (0=PKW, 1=LKW, 2=Motorrad"); + //int auswahl = Convert.ToInt32(Console.ReadLine()); + //if (auswahl == 0) + // pkw[10] = new Pkw(); + //else if (auswahl == 1) + // lkw[47] = new LKW(); + //pkw[1].Steuern(); + //lkw[1].Steuern(); + //motorrad[1].Steuern(); + #endregion + Kfz[] kfz = new Kfz[1000]; + kfz[0] = new LKW(); + kfz[1] = new Pkw(); + kfz[2] = new Motorrad(); + // kfz[3] = new Kfz(); + + //Console.WriteLine((kfz[0] as LKW).Steuern()); + //Console.WriteLine(((Pkw)kfz[1]).Steuern()); + //Console.WriteLine(((Motorrad)kfz[2]).Steuern()); + + for (int i = 0; i < 3; i++) + { + #region Lästige explizite Konvertierungen + //if (kfz[i] is LKW) + // Console.WriteLine((kfz[i] as LKW).Steuern()); + //else if (kfz[i] is Pkw) + // Console.WriteLine(((Pkw)kfz[i]).Steuern()); + //else if (kfz[i] is Motorrad) + // Console.WriteLine(((Motorrad)kfz[i]).Steuern()); + #endregion + Console.WriteLine(kfz[i].Steuern()); + } } } } diff --git a/06-UbgBinTree-Mo/BinTree.cs b/06-UbgBinTree-Mo/BinTree.cs index 5363fb5..bbddf51 100644 --- a/06-UbgBinTree-Mo/BinTree.cs +++ b/06-UbgBinTree-Mo/BinTree.cs @@ -17,5 +17,93 @@ namespace _06_UbgBinTree_Mo class BinTree { + class BItem + { + public string name; + public BItem left = null, right = null; + public int lCount = 0, rCount = 0; + public BItem(string Name) { name = Name; } + public override string ToString() + { + string lString = (left == null) ? "null" : left.name; + string rString = (right == null) ? "null" : right.name; + return $"left:{lString} <- {name}|{lCount}|{rCount} -> right:{rString}"; + } + } + BItem root = null; + + public void BInsert(string Name) + { + BItem newItem = new BItem(Name); + if (root == null) + root = newItem; + else + { + bool done = false; + BItem item = root; + do + { + if (Name.CompareTo(item.name)<0) + // Name < item.name --> Name - item.name < 0 + // Name.CompareTo(item.name) < 0 + // --> linker Teilbaum + { + item.lCount++; + if (item.left == null) + { + item.left = newItem; + done = true; + } + else + item = item.left; + } + else + { + item.rCount++; + if (item.right == null) + { + item.right = newItem; + done = true; + } + else + item = item.right; + } + } while (!done); + } + } + public int Level(string Name) + { + BItem item = root; + int level = 0; + while (item != null) + { + if (Name == item.name) + return level; + level++; + item = (Name.CompareTo(item.name)<0) ? item.left : item.right; + } + return -1; + } + public void Print() { Print(root); } + private void Print(BItem item) + { + if (item != null) + { + if (item.left != null) + Print(item.left); + + Console.WriteLine(item.name); + + if (item.right != null) + Print(item.right); + } + } + public int Fib(int n) + { + if (n < 2) + return 1; + else + return Fib(n - 1) + Fib(n - 2); + } } } diff --git a/06-UbgBinTree-Mo/Program.cs b/06-UbgBinTree-Mo/Program.cs index ded9bac..6957b8d 100644 --- a/06-UbgBinTree-Mo/Program.cs +++ b/06-UbgBinTree-Mo/Program.cs @@ -6,7 +6,19 @@ namespace _06_UbgBinTree_Mo { static void Main(string[] args) { - Console.WriteLine("Hello World!"); + BinTree tree = new BinTree(); + tree.BInsert("Claudia"); + tree.BInsert("Anton"); + tree.BInsert("Dieter"); + tree.BInsert("Berta"); + tree.BInsert("Emil"); + + tree.Print(); + Console.WriteLine(tree.Level("Claudia")); + Console.WriteLine(tree.Level("Anton")); + Console.WriteLine(tree.Level("Emil")); + + } } } -- GitLab