From 473357a25164f56514da648c61de7fbac2e9649a Mon Sep 17 00:00:00 2001 From: wienkop <uwe.wienkop@th-nuernberg.de> Date: Mon, 22 Mar 2021 13:02:22 +0100 Subject: [PATCH] =?UTF-8?q?2021-03-22=20Op-=C3=9Cberladung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 01-3 Personalausweis/Buergeramt.cs | 39 ++++++++++++++ 01-3 Personalausweis/Personalausweis.cs | 32 +++++++++++ 01-3 Personalausweis/Program.cs | 36 ++++--------- 01-3 TestPersonalausweis/UnitTest1.cs | 13 +++-- 01-V IntroIndexer/Person.cs | 13 ++--- 01-V IntroIndexer/Personenliste.cs | 17 ++++++ 01-V IntroIndexer/Program.cs | 6 ++- .../02-1 OpUeberladungBrueche.csproj | 9 ++++ 02-1 OpUeberladungBrueche/Bruch.cs | 54 +++++++++++++++++++ 02-1 OpUeberladungBrueche/Program.cs | 36 +++++++++++++ Prog2WienkopSS2021.sln | 14 ++--- TestProject1/TestProject1.csproj | 20 ------- TestProject1/UnitTest1.cs | 14 ----- 13 files changed, 224 insertions(+), 79 deletions(-) create mode 100644 01-3 Personalausweis/Buergeramt.cs create mode 100644 01-3 Personalausweis/Personalausweis.cs create mode 100644 02-1 OpUeberladungBrueche/02-1 OpUeberladungBrueche.csproj create mode 100644 02-1 OpUeberladungBrueche/Bruch.cs create mode 100644 02-1 OpUeberladungBrueche/Program.cs delete mode 100644 TestProject1/TestProject1.csproj delete mode 100644 TestProject1/UnitTest1.cs diff --git a/01-3 Personalausweis/Buergeramt.cs b/01-3 Personalausweis/Buergeramt.cs new file mode 100644 index 0000000..89bc824 --- /dev/null +++ b/01-3 Personalausweis/Buergeramt.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace _01_3_Personalausweis +{ + public class Buergeramt + { + Personalausweis[] Amt; + int kapazitaet; + + public Buergeramt(int Kapazitaet) + { + Amt = new Personalausweis[Kapazitaet]; + kapazitaet = Kapazitaet; + } + + public Personalausweis this[int index] + { + get => Amt[index]; + set + { + if (index >= 0 && index < kapazitaet && Amt[index] == null) + Amt[index] = value; + else throw new Exception("Index außerhalb des Wertebereichs oder Feldplatz bereits belegt."); + } + } + + public IEnumerator<Personalausweis> GetEnumerator() + { + for (int i = 0; i < kapazitaet; i++) + { + if (Amt[i] != null) + yield return Amt[i]; + } + } + } + +} diff --git a/01-3 Personalausweis/Personalausweis.cs b/01-3 Personalausweis/Personalausweis.cs new file mode 100644 index 0000000..6fac8ee --- /dev/null +++ b/01-3 Personalausweis/Personalausweis.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace _01_3_Personalausweis +{ + public class Personalausweis + { + public string Name { get; private set; } + int id = 1000; + + static int naechste_ID = 1000; + + public Personalausweis(string Name) + { + this.Name = Name; + id = NaechsteID++; + } + + public static int NaechsteID + { + get => naechste_ID; + set + { + if (value < naechste_ID) + throw new ArgumentOutOfRangeException("Neue ID darf nicht kleiner dem aktuellen Stand sein"); + naechste_ID = value; + } + } + public override string ToString() => $"Name: {Name}, ID: {id}"; + } +} diff --git a/01-3 Personalausweis/Program.cs b/01-3 Personalausweis/Program.cs index a80eae1..1254391 100644 --- a/01-3 Personalausweis/Program.cs +++ b/01-3 Personalausweis/Program.cs @@ -2,31 +2,7 @@ namespace _01_3_Personalausweis { - public class Personalausweis - { - public string Name { get; private set; } - int id=1000; - - static int naechste_ID = 1000; - - public Personalausweis(string Name) - { - this.Name = Name; - id = NaechsteID++; - } - - public static int NaechsteID - { - get => naechste_ID; - set - { - if (value < naechste_ID) - throw new ArgumentOutOfRangeException("Neue ID darf nicht kleiner dem aktuellen Stand sein"); - naechste_ID = value; - } - } - public override string ToString() => $"Name: {Name}, ID: {id}"; - } + class Program { static void Main(string[] args) @@ -37,6 +13,16 @@ namespace _01_3_Personalausweis Console.WriteLine(anton.Name); Console.WriteLine(anton); Console.WriteLine(Personalausweis.NaechsteID); + + Buergeramt meinDorf = new Buergeramt(10); + meinDorf[1] = new Personalausweis("Emil"); + meinDorf[5] = new Personalausweis("Franz"); + + foreach (var item in meinDorf) + { + Console.WriteLine(item); + } + } } } diff --git a/01-3 TestPersonalausweis/UnitTest1.cs b/01-3 TestPersonalausweis/UnitTest1.cs index 7e554ce..b81f42a 100644 --- a/01-3 TestPersonalausweis/UnitTest1.cs +++ b/01-3 TestPersonalausweis/UnitTest1.cs @@ -4,24 +4,27 @@ using _01_3_Personalausweis; namespace _01_3_TestPersonalausweis { - public class UnitTest1 + public class TestKlassePersonalausweis { [Fact] - public void TestPerso() + public void TestPersonalausweisAnlegen() { Personalausweis.NaechsteID = 2000; Personalausweis perso = new Personalausweis("Anton"); - + string result = "Name: Anton, ID: 2000"; Assert.Equal(result, perso.ToString()); } [Fact] - public void TestID() + public void TestIDweiterschalten() { + Personalausweis.NaechsteID = 1000; Personalausweis perso = new Personalausweis("Anton"); Assert.Equal(1001, Personalausweis.NaechsteID); - } } + public class TestKlasseBuergeramt + { + } } diff --git a/01-V IntroIndexer/Person.cs b/01-V IntroIndexer/Person.cs index ea3b046..c0050dc 100644 --- a/01-V IntroIndexer/Person.cs +++ b/01-V IntroIndexer/Person.cs @@ -5,14 +5,15 @@ namespace _01_V_IntroIndexer class Person { #region Datenfelder - private string name, vorname; + public string Name { get; private set; } + private string vorname; private int alter; #endregion #region Konstruktoren public Person(string Name) // Aufruf z.B. bei "Claudia" { - name = Name; + this.Name = Name; vorname = null; alter = -1; } @@ -20,7 +21,7 @@ namespace _01_V_IntroIndexer { if (Alter < 0) throw new ArgumentOutOfRangeException("Alter muss eine positive Zahl sein!"); - this.name = name; // this.name ~ Datenfeld name; "nur" name ~ Parameter!!! + this.Name = name; // this.name ~ Datenfeld name; "nur" name ~ Parameter!!! vorname = Vorname; alter = Alter; } @@ -60,15 +61,15 @@ namespace _01_V_IntroIndexer public void Ausgabe() // Ausgabe hat den impliziten Parameter this { //Console.WriteLine($"Person: {this.vorname} {this.name}, Alter: {this.alter}"); - Console.WriteLine($"Person: {vorname} {name}, Alter: {alter}"); + Console.WriteLine($"Person: {vorname} {Name}, Alter: {alter}"); } public static void Ausgabe(Person p) // Ausgabe hat ausschließlich den Parameter p { - Console.WriteLine($"Person: {p.vorname} {p.name}, Alter: {p.alter}"); + Console.WriteLine($"Person: {p.vorname} {p.Name}, Alter: {p.alter}"); } public override string ToString() { - return $">>> Person: {vorname} {name}, Alter: {alter}"; + return $">>> Person: {vorname} {Name}, Alter: {alter}"; } #endregion } diff --git a/01-V IntroIndexer/Personenliste.cs b/01-V IntroIndexer/Personenliste.cs index 4ae2063..7af51f1 100644 --- a/01-V IntroIndexer/Personenliste.cs +++ b/01-V IntroIndexer/Personenliste.cs @@ -35,8 +35,25 @@ namespace _01_V_IntroIndexer personen[index] = value; } } + public Person this[string name] + // this = Name für einen Indexer + // index-Parameter kann von beliebigem Typ sein und es + // können beliebig viele Parameter angegeben werden + { + get + { + for (int i = 0; i < capacity; i++) + { + if (personen[i] != null && personen[i].Name == name) + return personen[i]; + } + return null; + } + } public IEnumerator<Person> GetEnumerator() + // GetEnumerator --> Returntyp IEnumerator + // Spezifischer Returntyp IEnumerator<hier Person> { for (int i = 0; i < capacity; i++) { diff --git a/01-V IntroIndexer/Program.cs b/01-V IntroIndexer/Program.cs index e2cc8e6..3883421 100644 --- a/01-V IntroIndexer/Program.cs +++ b/01-V IntroIndexer/Program.cs @@ -16,6 +16,7 @@ namespace _01_V_IntroIndexer Console.WriteLine(anton); Console.WriteLine(anton.ToString()); + // Indexer: Analog zum Feldzugriff: //int[] f = new int[30]; //f[1] = 10; //int x = f[2]; @@ -23,13 +24,14 @@ namespace _01_V_IntroIndexer Personenliste freunde = new Personenliste(10); freunde.Add(anton); freunde.Add(new Person("Schneider", "Dieter")); - Console.WriteLine("-----------------"); + Console.WriteLine("*****************"); Console.WriteLine(freunde[0]); Console.WriteLine(freunde[1]); + Console.WriteLine(freunde["Meier"]); freunde[5] = berta; Console.WriteLine("-----------------"); - foreach (Person p in freunde) + foreach (var p in freunde) { Console.WriteLine(p); } diff --git a/02-1 OpUeberladungBrueche/02-1 OpUeberladungBrueche.csproj b/02-1 OpUeberladungBrueche/02-1 OpUeberladungBrueche.csproj new file mode 100644 index 0000000..10cdbed --- /dev/null +++ b/02-1 OpUeberladungBrueche/02-1 OpUeberladungBrueche.csproj @@ -0,0 +1,9 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>netcoreapp3.1</TargetFramework> + <RootNamespace>_02_1_OpUeberladungBrueche</RootNamespace> + </PropertyGroup> + +</Project> diff --git a/02-1 OpUeberladungBrueche/Bruch.cs b/02-1 OpUeberladungBrueche/Bruch.cs new file mode 100644 index 0000000..cd69308 --- /dev/null +++ b/02-1 OpUeberladungBrueche/Bruch.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace _02_1_OpUeberladungBrueche +{ + class Bruch + { + int z, n; + public Bruch (int Z,int N=1) + { + z = Z; + n = N; + } + + //public void Mult_V1(Bruch b) + //{ + // // Ergebnisse müssten in z,n gespeichert werden + // // Folge: Originalwerte werden überschrieben + //} + public Bruch Mult_V2(Bruch b) + { + int zneu = z * b.z; + int nneu = n * b.n; + Bruch neu = new Bruch(zneu, nneu); + return neu; + } + public Bruch Mult_V3(Bruch b) + { + return new Bruch(z * b.z, n * b.n); + } + public Bruch Mult_V4(Bruch b) + => new Bruch(this.z * b.z, this.n * b.n); + public Bruch Mult_V4(int k) + => new Bruch(z * k, n); + + + public static Bruch Mult_V5(Bruch b1, Bruch b2) + => new Bruch(b1.z * b2.z, b1.n * b2.n); + //public static Bruch Mult_V5(Bruch b1, int k) + // => new Bruch(b1.z * k, b1.n ); + //public static Bruch Mult_V5(int k, Bruch b2) + // => new Bruch(k * b2.z, b2.n); + public static Bruch operator *(Bruch b1, Bruch b2) + => new Bruch(b1.z * b2.z, b1.n * b2.n); + + // Konvertierungsoperatoren + public static implicit operator Bruch (int k) => new Bruch (k); + // Macht aus einem int einen Bruch + public static explicit operator int (Bruch b) => b.z/b.n; + // Macht aus einem Bruch einen int + // Da etwas verloren gehen kann, explizite Konvertierung + } +} diff --git a/02-1 OpUeberladungBrueche/Program.cs b/02-1 OpUeberladungBrueche/Program.cs new file mode 100644 index 0000000..14102ad --- /dev/null +++ b/02-1 OpUeberladungBrueche/Program.cs @@ -0,0 +1,36 @@ +using System; + +namespace _02_1_OpUeberladungBrueche +{ + class Program + { + static void Main(string[] args) + { + Bruch b1 = new Bruch(2, 3); + Bruch b2 = new Bruch(3, 4); + Bruch b3 = new Bruch(2); + + //int k1 = 3; + //int k2 = 4; + //k1 * k2; + + // b1.Mult_V1(b2); -- NEIN! b1 wird zerstört + + Bruch e1 = b1.Mult_V2(b2); + Bruch e2 = b1.Mult_V3(b2); + Bruch e3 = b1.Mult_V4(b2); + + Bruch e4 = b1.Mult_V4(3); + //Bruch e5 = 3.Mult_V4(b1); // geht nicht, da 3 aus Klasse int und nicht Bruch ist + + Bruch e5 = Bruch.Mult_V5(b1, b2); + Bruch e6 = Bruch.Mult_V5(b1, 2); + Bruch e7 = Bruch.Mult_V5(2, b2); + int k3 = (int)b1; + + Bruch e8 = b1 * b2; + Bruch e9 = b1 * 2; + Bruch e10 = 2 * b2 * 3; + } + } +} diff --git a/Prog2WienkopSS2021.sln b/Prog2WienkopSS2021.sln index 23162f5..baea8f1 100644 --- a/Prog2WienkopSS2021.sln +++ b/Prog2WienkopSS2021.sln @@ -9,11 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-1T WdhlgKlassen", "01-T EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-2 IntroIndexer", "01-V IntroIndexer\01-2 IntroIndexer.csproj", "{6781DE0D-C861-40AC-9C33-5EA5D917425D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProject1", "TestProject1\TestProject1.csproj", "{24B954CA-BD5E-42BF-9396-E973BCC8A8B5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-3 Personalausweis", "01-3 Personalausweis\01-3 Personalausweis.csproj", "{99D1542B-82B4-4848-967A-C5B7BB1ECD29}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "01-3 Personalausweis", "01-3 Personalausweis\01-3 Personalausweis.csproj", "{99D1542B-82B4-4848-967A-C5B7BB1ECD29}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-3 TestPersonalausweis", "01-3 TestPersonalausweis\01-3 TestPersonalausweis.csproj", "{03421B8B-5B64-4997-9BA7-F558D50799EB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "01-3 TestPersonalausweis", "01-3 TestPersonalausweis\01-3 TestPersonalausweis.csproj", "{03421B8B-5B64-4997-9BA7-F558D50799EB}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "02-1 OpUeberladungBrueche", "02-1 OpUeberladungBrueche\02-1 OpUeberladungBrueche.csproj", "{0CEC8CDB-1B65-4A3D-B6BD-5DE722BEE96D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,10 +33,6 @@ Global {6781DE0D-C861-40AC-9C33-5EA5D917425D}.Debug|Any CPU.Build.0 = Debug|Any CPU {6781DE0D-C861-40AC-9C33-5EA5D917425D}.Release|Any CPU.ActiveCfg = Release|Any CPU {6781DE0D-C861-40AC-9C33-5EA5D917425D}.Release|Any CPU.Build.0 = Release|Any CPU - {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {24B954CA-BD5E-42BF-9396-E973BCC8A8B5}.Release|Any CPU.Build.0 = Release|Any CPU {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Debug|Any CPU.Build.0 = Debug|Any CPU {99D1542B-82B4-4848-967A-C5B7BB1ECD29}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -45,6 +41,10 @@ Global {03421B8B-5B64-4997-9BA7-F558D50799EB}.Debug|Any CPU.Build.0 = Debug|Any CPU {03421B8B-5B64-4997-9BA7-F558D50799EB}.Release|Any CPU.ActiveCfg = Release|Any CPU {03421B8B-5B64-4997-9BA7-F558D50799EB}.Release|Any CPU.Build.0 = Release|Any CPU + {0CEC8CDB-1B65-4A3D-B6BD-5DE722BEE96D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0CEC8CDB-1B65-4A3D-B6BD-5DE722BEE96D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0CEC8CDB-1B65-4A3D-B6BD-5DE722BEE96D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0CEC8CDB-1B65-4A3D-B6BD-5DE722BEE96D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TestProject1/TestProject1.csproj b/TestProject1/TestProject1.csproj deleted file mode 100644 index 63194a8..0000000 --- a/TestProject1/TestProject1.csproj +++ /dev/null @@ -1,20 +0,0 @@ -<Project Sdk="Microsoft.NET.Sdk"> - - <PropertyGroup> - <TargetFramework>netcoreapp3.1</TargetFramework> - - <IsPackable>false</IsPackable> - </PropertyGroup> - - <ItemGroup> - <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> - <PackageReference Include="xunit" Version="2.4.0" /> - <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> - <PackageReference Include="coverlet.collector" Version="1.2.0" /> - </ItemGroup> - - <ItemGroup> - <ProjectReference Include="..\01-V WdhlgKlassen\01-1 WdhlgKlassen.csproj" /> - </ItemGroup> - -</Project> diff --git a/TestProject1/UnitTest1.cs b/TestProject1/UnitTest1.cs deleted file mode 100644 index 5757740..0000000 --- a/TestProject1/UnitTest1.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using Xunit; - -namespace TestProject1 -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - Assert.Equal(7, _01_V_WdhlgKlassen.Program.Add(3, 4)); - } - } -} -- GitLab