Skip to content
Snippets Groups Projects
Commit 417e8e69 authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

Generische Max-Funktion und generische Doppelt-verkettete Liste

parent 9f0bfa13
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -18,6 +18,18 @@ 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)
......@@ -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
<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>
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()}");
}
}
}
}
<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>
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()}");
}
}
}
}
}
<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>
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;
}
}
}
<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>
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 }));
}
}
}
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("------------");
}
}
}
<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>
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;
}
}
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);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment