diff --git a/Exercise Exception Vector/Exercise Exception Vector.csproj b/Exercise Exception Vector/Exercise Exception Vector.csproj new file mode 100644 index 0000000000000000000000000000000000000000..eb38ced257b07f9cf9379b763e7539608a2b9a20 --- /dev/null +++ b/Exercise Exception Vector/Exercise Exception Vector.csproj @@ -0,0 +1,56 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07}</ProjectGuid> + <OutputType>Exe</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>Exercise__Exception_Vector</RootNamespace> + <AssemblyName>Exercise__Exception_Vector</AssemblyName> + <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="VectorException.cs" /> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Vector.cs" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> + +</Project> diff --git a/Exercise Exception Vector/Program.cs b/Exercise Exception Vector/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..b97ac1a9281c92732b3def2344b5554fc92d1f3f --- /dev/null +++ b/Exercise Exception Vector/Program.cs @@ -0,0 +1,92 @@ +/* +MIT License + +Copyright (c) 2022 Alexander Schneider + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +using System.Diagnostics; +using static System.Console; + +namespace Schneider.Exercise.ExceptionVector +{ + class MainClass + { + private static Vector parseVector(string input) + { + string[] components = input.Split(','); + float x = float.Parse(components[0]); + float y = float.Parse(components[1]); + return new Vector(x, y); + } + + private static bool shallStop(string input) + { + return input == "fertig"; + } + + public static void Main (string[] args) + { + // Aufgabe fordert ewige Schleife und Abbruch bei Eingabe von fertig. + while (true) + { + + Write("Geben Sie einen Vektor ein: "); + string input = ReadLine(); + if (shallStop(input)) + { + break; + } + Vector a = parseVector(input); + + Write("Geben Sie eine Operation ein: "); + string vectorOperator = ReadLine(); + if (shallStop(vectorOperator)) + { + break; + } + + Write("Geben Sie einen Vektor ein: "); + input = ReadLine(); + if (shallStop(input)) + { + break; + } + Vector b = parseVector(input); + + // Passende Operation ausführen Ergebnis ausführen. + switch (vectorOperator) + { + case "+": + WriteLine($"Ergebnis: { a + b}");; + break; + case "-": + WriteLine($"Ergebnis: { a - b}");; + break; + case "*": + WriteLine($"Ergebnis: { a * b}");; + break; + } + + } + WriteLine("Ciao"); + } + } +} \ No newline at end of file diff --git a/Exercise Exception Vector/Properties/AssemblyInfo.cs b/Exercise Exception Vector/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..d9901c0b96755462f49ae9eeff297f456e443372 --- /dev/null +++ b/Exercise Exception Vector/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Exercise__Exception_Vector")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Exercise__Exception_Vector")] +[assembly: AssemblyCopyright("Copyright © 2022")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/Exercise Exception Vector/Vector.cs b/Exercise Exception Vector/Vector.cs new file mode 100644 index 0000000000000000000000000000000000000000..8c133af05093b89bba2585ece68ff35065ce78d8 --- /dev/null +++ b/Exercise Exception Vector/Vector.cs @@ -0,0 +1,113 @@ +/* +MIT License + +Copyright (c) 2022 Alexander Schneider + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +using System; + +namespace Schneider.Exercise.ExceptionVector +{ + public class Vector + { + /// <summary> + /// Liefert X Komponente, aber lässt diese nicht verändern, wie in Aufgabe gefordert. + /// </summary> + /// <value>The x.</value> + public float X { get; private set; } + + /// <summary> + /// Liefert Y Komponente, aber lässt diese nicht verändern, wie in Aufgabe gefordert. + /// </summary> + /// <value>The x.</value> + public float Y { get; private set; } + + /// <summary> + /// Von Aufgabe geforderter einziger Konstruktor. Falls Nullvektor wird das erstellen mit + /// passender VectorKonfigurationException verhindert. + /// </summary> + /// <param name="x">The x coordinate.</param> + /// <param name="y">The y coordinate.</param> + public Vector (float x, float y) + { + // Nullvektor vehindern + if (x == 0 && y == 0) { + throw new VektorKonfigurationsException (); + } + + if (calcLength(x, y) > 10) + { + // Hier war die Aufgabe etwas unklar, ob es niemals bei einer Rechenoperation länger + // als 10 sein darf oder niemals auch beim Erzeugen mit new. + throw new VektorKonfigurationsException(); + } + + X = x; + Y = y; + } + + public static Vector operator+ (Vector a, Vector b) + { + Vector result = new Vector (a.X + b.X, a.Y + b.Y); + if (!checkIfLengthIsOk (result)) { + throw new VektorRechenException (a, b); + } + return result; + } + + public static Vector operator- (Vector a, Vector b) + { + Vector result = new Vector (a.X - b.X, a.Y - b.Y); + if (!checkIfLengthIsOk (result)) { + throw new VektorRechenException (a, b); + } + return result; + } + + public static float operator* (Vector a, Vector b) + { + float result = a.X * b.X + a.Y * b.Y; + if (result == 0) { + throw new VektorRechenException (a, b); + } + return result; + } + + private static bool checkIfLengthIsOk(Vector a) + { + // Die Länge darf nicht größer als 10 sein. + if (calcLength (a.X, a.Y) > 10) { + return false; + } + return true; + } + + private static double calcLength(float a, float b) + { + return Math.Sqrt (a*a + b*b); + } + + public override string ToString() + { + return $"{X},{Y}"; + } + } +} \ No newline at end of file diff --git a/Exercise Exception Vector/VectorException.cs b/Exercise Exception Vector/VectorException.cs new file mode 100644 index 0000000000000000000000000000000000000000..0254579acc9bebbbc18706d7a59070f49c4fd0a3 --- /dev/null +++ b/Exercise Exception Vector/VectorException.cs @@ -0,0 +1,45 @@ +/* +MIT License + +Copyright (c) 2015 Alexander Schneider + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ +using System; + +namespace Schneider.Exercise.ExceptionVector +{ + public abstract class VektorException : Exception + {} + + class VektorRechenException : VektorException + { + public Vector VectorA { get; set; } + public Vector VectorB { get; set; } + + public VektorRechenException(Vector a, Vector b) + { + VectorA = a; + VectorB = b; + } + } + + class VektorKonfigurationsException : VektorException + {} +} \ No newline at end of file diff --git a/Prog2-SS2022-Schneider.sln b/Prog2-SS2022-Schneider.sln index 1cd1982afe2e3172679c6d3dca1e7a30781577ba..7d4068593198d5db68945a3ab2aa6c22a03e059c 100644 --- a/Prog2-SS2022-Schneider.sln +++ b/Prog2-SS2022-Schneider.sln @@ -10,6 +10,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise BookCollection", " EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise Employee Management", "Exercise Employee Management\Exercise Employee Management.csproj", "{7D4AF42D-030C-4B8E-9CBF-0D08CB79FA37}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise Generics Dictionary", "Exercise Generics Dictionary\Exercise Generics Dictionary.csproj", "{EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise Exception Vector", "Exercise Exception Vector\Exercise Exception Vector.csproj", "{F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,5 +40,13 @@ Global {7D4AF42D-030C-4B8E-9CBF-0D08CB79FA37}.Debug|Any CPU.Build.0 = Debug|Any CPU {7D4AF42D-030C-4B8E-9CBF-0D08CB79FA37}.Release|Any CPU.ActiveCfg = Release|Any CPU {7D4AF42D-030C-4B8E-9CBF-0D08CB79FA37}.Release|Any CPU.Build.0 = Release|Any CPU + {EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EBD4CA3D-A7CF-4ADD-BBB1-1251AC73C851}.Release|Any CPU.Build.0 = Release|Any CPU + {F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F15AB3CE-0032-4E7C-83AE-E1A55FFA4B07}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal