From 27ca688e233ec39c71067a8f9a32c34e78f1af3b Mon Sep 17 00:00:00 2001 From: Jens Albrecht <jens.albrecht@th-nuernberg.de> Date: Fri, 8 Nov 2024 20:07:32 +0100 Subject: [PATCH] . --- Bezugspreis/Bezugspreis.cs | 35 ++++++++++++++++++++ Bezugspreis/Bezugspreis.csproj | 10 ++++++ PotenzFunktion/PotenzFunktion.cs | 28 ++++++++++++++++ PotenzFunktion/PotenzFunktion.csproj | 10 ++++++ QuerZahlen/QuerZahlen.cs | 48 ++++++++++++++++++++++++++++ QuerZahlen/QuerZahlen.csproj | 10 ++++++ Twist/Twist.cs | 19 +++++++++++ Twist/Twist.csproj | 10 ++++++ Uebung05.sln | 43 +++++++++++++++++++++++++ ZeichneDreieck/Dreieck.cs | 42 ++++++++++++++++++++++++ ZeichneDreieck/ZeichneDreieck.csproj | 10 ++++++ 11 files changed, 265 insertions(+) create mode 100644 Bezugspreis/Bezugspreis.cs create mode 100644 Bezugspreis/Bezugspreis.csproj create mode 100644 PotenzFunktion/PotenzFunktion.cs create mode 100644 PotenzFunktion/PotenzFunktion.csproj create mode 100644 QuerZahlen/QuerZahlen.cs create mode 100644 QuerZahlen/QuerZahlen.csproj create mode 100644 Twist/Twist.cs create mode 100644 Twist/Twist.csproj create mode 100644 Uebung05.sln create mode 100644 ZeichneDreieck/Dreieck.cs create mode 100644 ZeichneDreieck/ZeichneDreieck.csproj diff --git a/Bezugspreis/Bezugspreis.cs b/Bezugspreis/Bezugspreis.cs new file mode 100644 index 0000000..c441b2b --- /dev/null +++ b/Bezugspreis/Bezugspreis.cs @@ -0,0 +1,35 @@ +// Funktion zur Berechnung eines Bezugspreises unter Berücksichtigung von +// Versandkosten und Rabatt +static double Bezugspreis(double listenPreis, int versandArt, double rabatt = 0) +{ + // Variable für Return-Wert + double preis = listenPreis; + + // Rabatt-Berechnung + if (rabatt < 0) + return -1; // Fehlerfall + preis -= preis * rabatt / 100.0; + + // Versandkosten-Berechnung - hier mal mit switch-Expression + double porto = versandArt switch + { + 1 => 4.90, // Päckchen + 2 => 6.50, // Paket < 10kg + 3 => 12.00, // Paket > 10kg + _ => -1 // Fehlerfall + }; + if (porto < 0) + return -1; // Fehlerfall + + preis += porto; + + return preis; +} + +Console.WriteLine($"Fall 1: Preis = {Bezugspreis(125, 2, 10):0.00} Euro"); +Console.WriteLine($"Fall 2: Preis = {Bezugspreis(200, 1, 3):0.00} Euro"); +Console.WriteLine($"Fall 3: Preis = {Bezugspreis(375, 3, 0):0.00} Euro"); +Console.WriteLine($"Fehler: Preis = {Bezugspreis(50, 4, 0):0.00} Euro"); +Console.WriteLine($"Fehler: Preis = {Bezugspreis(150, 1, -1):0.00} Euro"); + +Console.ReadKey(); diff --git a/Bezugspreis/Bezugspreis.csproj b/Bezugspreis/Bezugspreis.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/Bezugspreis/Bezugspreis.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net8.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/PotenzFunktion/PotenzFunktion.cs b/PotenzFunktion/PotenzFunktion.cs new file mode 100644 index 0000000..dc98737 --- /dev/null +++ b/PotenzFunktion/PotenzFunktion.cs @@ -0,0 +1,28 @@ +namespace Uebung +{ + class Program + { + // Potenzfunktion + static double Potenz(double x, int n) + { + double potenz = 1; + + for (int i = 1; i <= n; i++) + potenz *= x; + + return potenz; + } + + // Hauptprogramm + static void Main() + { + Console.Write("Basis eingeben: "); + double basis = Convert.ToDouble(Console.ReadLine()); + Console.Write("Exponent eingeben: "); + int exponent = Convert.ToInt32(Console.ReadLine()); + + for (int i = 0; i <= exponent; i++) + Console.WriteLine($"{basis}^{i}: {Potenz(basis, i),4}"); + } + } +} \ No newline at end of file diff --git a/PotenzFunktion/PotenzFunktion.csproj b/PotenzFunktion/PotenzFunktion.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/PotenzFunktion/PotenzFunktion.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/QuerZahlen/QuerZahlen.cs b/QuerZahlen/QuerZahlen.cs new file mode 100644 index 0000000..d764bff --- /dev/null +++ b/QuerZahlen/QuerZahlen.cs @@ -0,0 +1,48 @@ +namespace Uebung +{ + class Program + { + static int QuerSumme(int zahl) + { + int summe = 0; + while (zahl > 0) + { + summe += zahl % 10; + zahl /= 10; + } + + return summe; + } + + static int QuerProdukt(int zahl) + { + if (zahl == 0) + return 0; + + int produkt = 1; + while (zahl > 0) + { + produkt *= zahl % 10; + zahl /= 10; + } + + return produkt; + } + + static bool IstQuerZahl(int zahl) + { + return QuerSumme(zahl) + QuerProdukt(zahl) == zahl; + } + + // Hauptprogramm + static void Main() + { + for (int zahl = 2; zahl < 1000; zahl++) + { + // Console.WriteLine($"{zahl,5} {QuerSumme(zahl),5} {QuerProdukt(zahl),5}"); + if (IstQuerZahl(zahl)) + Console.WriteLine($"{zahl} ist eine Querzahl!"); + } + } + } +} \ No newline at end of file diff --git a/QuerZahlen/QuerZahlen.csproj b/QuerZahlen/QuerZahlen.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/QuerZahlen/QuerZahlen.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Twist/Twist.cs b/Twist/Twist.cs new file mode 100644 index 0000000..3d1e2e5 --- /dev/null +++ b/Twist/Twist.cs @@ -0,0 +1,19 @@ +static int Twist(int zahl) +{ + int twist = 0; + + while (zahl > 0) + { + int ziffer = zahl % 10; + twist = twist * 10 + ziffer; + zahl /= 10; + } + + return twist; +} + +// Hauptprogramm +int zahl = 4567; +Console.WriteLine($"{zahl} umgekehrt: {Twist(zahl)}"); +zahl = 1234567890; +Console.WriteLine($"{zahl} umgekehrt: {Twist(zahl)}"); diff --git a/Twist/Twist.csproj b/Twist/Twist.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/Twist/Twist.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> diff --git a/Uebung05.sln b/Uebung05.sln new file mode 100644 index 0000000..89a5a1a --- /dev/null +++ b/Uebung05.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32929.385 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PotenzFunktion", "PotenzFunktion\PotenzFunktion.csproj", "{EC9A3624-8777-41A5-A708-E3765BE52E5A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuerZahlen", "QuerZahlen\QuerZahlen.csproj", "{CC129094-DD5F-4AB8-B685-59F8A9E6694A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Twist", "Twist\Twist.csproj", "{2EB67C10-2D6D-4283-AD11-240C8CE010C3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bezugspreis", "Bezugspreis\Bezugspreis.csproj", "{92656707-21E5-48D2-9FB1-E43A584DDD9D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {EC9A3624-8777-41A5-A708-E3765BE52E5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC9A3624-8777-41A5-A708-E3765BE52E5A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC9A3624-8777-41A5-A708-E3765BE52E5A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC9A3624-8777-41A5-A708-E3765BE52E5A}.Release|Any CPU.Build.0 = Release|Any CPU + {CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC129094-DD5F-4AB8-B685-59F8A9E6694A}.Release|Any CPU.Build.0 = Release|Any CPU + {2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2EB67C10-2D6D-4283-AD11-240C8CE010C3}.Release|Any CPU.Build.0 = Release|Any CPU + {92656707-21E5-48D2-9FB1-E43A584DDD9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {92656707-21E5-48D2-9FB1-E43A584DDD9D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {92656707-21E5-48D2-9FB1-E43A584DDD9D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {92656707-21E5-48D2-9FB1-E43A584DDD9D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {C3F003FC-52A5-4A88-9F70-94B7949BBBFC} + EndGlobalSection +EndGlobal diff --git a/ZeichneDreieck/Dreieck.cs b/ZeichneDreieck/Dreieck.cs new file mode 100644 index 0000000..99ee393 --- /dev/null +++ b/ZeichneDreieck/Dreieck.cs @@ -0,0 +1,42 @@ +class Program +{ + static void ZeichneDreieck(int n, char c = '*', bool zentriert = false) + { + // Man könnte jetzt die beiden Code-Blöcke auch in einen zusammenfassen, + // weil die Schleifen ja im Prinzip das Gleiche machen. + // Versuchen Sie mal, den Code umzustellen! + if (!zentriert) + { + for (int i = 1; i <= n; i++) + { + for (int j = 1; j <= i; j++) + Console.Write(c); + + Console.WriteLine(); + } + } + else // zentriert + { + for (int i = 1, leer = n-1; i <= n; i++, leer--) + { + // Leerzeichen vorher einfügen + for (int j = 1; j <= leer; j++) + Console.Write(' '); + + for (int j = 1; j <= 2 * i - 1; j++) + Console.Write(c); + + Console.WriteLine(); + } + } + } + + static void Main() + { + ZeichneDreieck(6, '$'); + Console.WriteLine(); + ZeichneDreieck(3); + Console.WriteLine(); + ZeichneDreieck(5, '#', true); + } +} \ No newline at end of file diff --git a/ZeichneDreieck/ZeichneDreieck.csproj b/ZeichneDreieck/ZeichneDreieck.csproj new file mode 100644 index 0000000..40c60dd --- /dev/null +++ b/ZeichneDreieck/ZeichneDreieck.csproj @@ -0,0 +1,10 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <OutputType>Exe</OutputType> + <TargetFramework>net6.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + +</Project> -- GitLab