From f89f308c525e9deebc6d2cf6416e27dfe1a299dc Mon Sep 17 00:00:00 2001 From: Dennis Kobert Date: Sun, 19 May 2019 16:03:38 +0200 Subject: Cleanup DiscoBot Project --- DSACore/Auxiliary/Calculator/Argument.cs | 17 +++--- DSACore/Auxiliary/Calculator/ISolvable.cs | 2 +- DSACore/Auxiliary/Calculator/Operator.cs | 16 ++--- DSACore/Auxiliary/Calculator/Ops.cs | 2 +- DSACore/Auxiliary/Calculator/StringSolver.cs | 90 ++++++++++------------------ 5 files changed, 49 insertions(+), 78 deletions(-) (limited to 'DSACore/Auxiliary/Calculator') diff --git a/DSACore/Auxiliary/Calculator/Argument.cs b/DSACore/Auxiliary/Calculator/Argument.cs index 52f33a9..14982aa 100644 --- a/DSACore/Auxiliary/Calculator/Argument.cs +++ b/DSACore/Auxiliary/Calculator/Argument.cs @@ -1,7 +1,7 @@ namespace DSACore.Auxiliary.Calculator { using System; - + /// /// Provides an ISolvable class to save numbers. The class handles Argument checking and conversion from string to int. /// @@ -12,27 +12,24 @@ public Argument(string value) { // check whether the value given is an empty string - if (string.IsNullOrEmpty(value)) - { - throw new ArgumentException("Argument kann nicht mit einem leeren string instanziert werden. ", nameof(value)); - } + if (string.IsNullOrEmpty(value)) + throw new ArgumentException("Argument kann nicht mit einem leeren string instanziert werden. ", + nameof(value)); - if (!int.TryParse(value, out int result)) - { + if (!int.TryParse(value, out var result)) throw new ArgumentException($"Kann {value} nicht in Integer konvertieren"); - } this.value = result; } public int Solve() { - return this.value; + return value; } public override string ToString() { - return this.value.ToString(); + return value.ToString(); } } } \ No newline at end of file diff --git a/DSACore/Auxiliary/Calculator/ISolvable.cs b/DSACore/Auxiliary/Calculator/ISolvable.cs index 1f571d0..2acbd11 100644 --- a/DSACore/Auxiliary/Calculator/ISolvable.cs +++ b/DSACore/Auxiliary/Calculator/ISolvable.cs @@ -7,4 +7,4 @@ { int Solve(); } -} +} \ No newline at end of file diff --git a/DSACore/Auxiliary/Calculator/Operator.cs b/DSACore/Auxiliary/Calculator/Operator.cs index 440e21e..ed46186 100644 --- a/DSACore/Auxiliary/Calculator/Operator.cs +++ b/DSACore/Auxiliary/Calculator/Operator.cs @@ -14,7 +14,7 @@ namespace DSACore.Auxiliary.Calculator { this.arg1 = arg1; this.arg2 = arg2; - this.OperatorType = operatorType; + OperatorType = operatorType; } public Ops OperatorType { get; set; } @@ -22,19 +22,19 @@ namespace DSACore.Auxiliary.Calculator public int Solve() { int result; - switch (this.OperatorType) + switch (OperatorType) { case Ops.Dice: - result = Dice.Roll(this.arg1.Solve(), this.arg2.Solve()); + result = Dice.Roll(arg1.Solve(), arg2.Solve()); break; case Ops.Multiply: - result = this.arg1.Solve() * this.arg2.Solve(); + result = arg1.Solve() * arg2.Solve(); break; case Ops.Add: - result = this.arg1.Solve() + this.arg2.Solve(); + result = arg1.Solve() + arg2.Solve(); break; case Ops.Subtract: - result = this.arg1.Solve() - this.arg2.Solve(); + result = arg1.Solve() - arg2.Solve(); break; default: throw new ArgumentOutOfRangeException(); @@ -45,7 +45,7 @@ namespace DSACore.Auxiliary.Calculator public override string ToString() { - return $"({this.arg1} {this.OperatorType} {this.arg2})"; + return $"({arg1} {OperatorType} {arg2})"; } } -} +} \ No newline at end of file diff --git a/DSACore/Auxiliary/Calculator/Ops.cs b/DSACore/Auxiliary/Calculator/Ops.cs index 702558d..c804257 100644 --- a/DSACore/Auxiliary/Calculator/Ops.cs +++ b/DSACore/Auxiliary/Calculator/Ops.cs @@ -10,4 +10,4 @@ Subtract, Add } -} +} \ No newline at end of file diff --git a/DSACore/Auxiliary/Calculator/StringSolver.cs b/DSACore/Auxiliary/Calculator/StringSolver.cs index 2eff5b4..212f812 100644 --- a/DSACore/Auxiliary/Calculator/StringSolver.cs +++ b/DSACore/Auxiliary/Calculator/StringSolver.cs @@ -24,30 +24,31 @@ namespace DSACore.Auxiliary.Calculator public override string ToString() { - return "(0+" + this.input.Replace(" ", string.Empty).ToLower() + ")"; + return "(0+" + input.Replace(" ", string.Empty).ToLower() + ")"; } public int Solve() { - string workInput = "0+" + this.input.Replace(" ", string.Empty).ToLower(); + var workInput = "0+" + input.Replace(" ", string.Empty).ToLower(); workInput = ExpandParentheses(workInput); - + // Create a List of the different parts of the calculation, e.g.:{"0", "+", "(5+6)", "d", "3"}. - this.AtomizeOperations(workInput); + AtomizeOperations(workInput); // traverse the List in order of Operation to Create the binary operation tree . - this.NestOperations(); + NestOperations(); // the List now contains only the top operation node, witch can be solved recursively, - return ((ISolvable)this.arguments.First()).Solve(); + return ((ISolvable) arguments.First()).Solve(); } - private static string GetInner(ref string input) // extract the inner bracket an remove the section from the input string + private static string + GetInner(ref string input) // extract the inner bracket an remove the section from the input string { - int depth = 0; + var depth = 0; for (var index = 1; index < input.Length; index++) { - char c = input[index]; + var c = input[index]; switch (c) { case '(': @@ -92,21 +93,13 @@ namespace DSACore.Auxiliary.Calculator private static string ExpandParentheses(string input) // insert * between Parentheses and digits { - for (int i = 0; i < input.Length - 1; i++) - { + for (var i = 0; i < input.Length - 1; i++) if (input[i + 1] == '(' && char.IsNumber(input[i])) - { input = input.Insert(i + 1, "*"); - } - } - for (int i = 1; i < input.Length; i++) - { + for (var i = 1; i < input.Length; i++) if (input[i - 1] == ')' && char.IsNumber(input[i])) - { input = input.Insert(i, "*"); - } - } return input; } @@ -115,16 +108,14 @@ namespace DSACore.Auxiliary.Calculator { for (var index = 0; index < workInput.Length; index++) { - char c = workInput[index]; + var c = workInput[index]; if (char.IsNumber(c)) { // if char number, check if at end of string, else continue looping if (index == workInput.Length - 1) - { // if at end of string; add remaining number to arguments - this.arguments.Add(new Argument(workInput.Substring(0, index + 1))); - } + arguments.Add(new Argument(workInput.Substring(0, index + 1))); continue; } @@ -134,16 +125,13 @@ namespace DSACore.Auxiliary.Calculator case ')': throw new ArgumentException($"Unmögliche Anordnung von Klammern"); case '(': - this.arguments.Add(new StringSolver(GetInner(ref workInput))); + arguments.Add(new StringSolver(GetInner(ref workInput))); index = -1; break; default: - if (index > 0) - { - this.arguments.Add(new Argument(workInput.Substring(0, index))); - } + if (index > 0) arguments.Add(new Argument(workInput.Substring(0, index))); - this.arguments.Add(GetOps(c)); + arguments.Add(GetOps(c)); workInput = workInput.Remove(0, index + 1); index = -1; break; @@ -154,58 +142,44 @@ namespace DSACore.Auxiliary.Calculator private void NestOperations() { foreach (Ops currentOp in Enum.GetValues(typeof(Ops))) - { // cycle through operators in operational order - for (var index = 0; index < this.arguments.Count; index++) + for (var index = 0; index < arguments.Count; index++) { - var arg = this.arguments[index]; + var arg = arguments[index]; - if (arg.GetType() != typeof(Ops)) - { - continue; - } + if (arg.GetType() != typeof(Ops)) continue; // arg is of type Ops - var op = (Ops)arg; + var op = (Ops) arg; - if (op != currentOp) - { - continue; - } + if (op != currentOp) continue; // arg describes the current operation - this.HandleSpecialFormatting(ref index, op); // Deal with special needs... + HandleSpecialFormatting(ref index, op); // Deal with special needs... // replace the previous current and next Element in the List with one Operation object - var temp = new Operator((ISolvable)this.arguments[index - 1], (ISolvable)this.arguments[index + 1], op); - this.arguments[index - 1] = temp; - this.arguments.RemoveRange(index, 2); + var temp = new Operator((ISolvable) arguments[index - 1], (ISolvable) arguments[index + 1], op); + arguments[index - 1] = temp; + arguments.RemoveRange(index, 2); index--; } - } } private void HandleSpecialFormatting(ref int index, Ops op) { - var arg1 = this.arguments[index - 1]; + var arg1 = arguments[index - 1]; if (arg1.GetType() == typeof(Ops)) { - if (op == Ops.Dice) - { - this.arguments.Insert(index++, new Argument("1")); // w6 -> 1w6 - } + if (op == Ops.Dice) arguments.Insert(index++, new Argument("1")); // w6 -> 1w6 - if (op == Ops.Subtract) - { - this.arguments.Insert(index++, new Argument("0")); // +-3 -> +0-3 - } + if (op == Ops.Subtract) arguments.Insert(index++, new Argument("0")); // +-3 -> +0-3 } - var arg2 = this.arguments[index + 1]; // 3+-5 -> 3+(0-5) + var arg2 = arguments[index + 1]; // 3+-5 -> 3+(0-5) if (arg2.GetType() == typeof(Ops)) { - this.arguments[index + 1] = new Operator(new Argument("0"), (ISolvable)this.arguments[index + 2], (Ops)arg2); - this.arguments.RemoveAt(index + 2); + arguments[index + 1] = new Operator(new Argument("0"), (ISolvable) arguments[index + 2], (Ops) arg2); + arguments.RemoveAt(index + 2); } } } -- cgit v1.2.3-54-g00ecf