diff --git a/Evil Calculator/EquationManager.cs b/Evil Calculator/EquationManager.cs index ed8d0ac..4d29c7a 100644 --- a/Evil Calculator/EquationManager.cs +++ b/Evil Calculator/EquationManager.cs @@ -3,7 +3,6 @@ class EquationManager { private static readonly HashSet _operators = new() { "+", "-", "*", "/", "^", "√" }; - private ReverseNotationSolver reverseNotationSolver = new(); List equation = new(); string currentOperand = ""; @@ -145,7 +144,7 @@ { if (currentOperand != "") equation.Add(new Token(Token.TokenType.Operand, currentOperand)); buttonEquals.Text = "#"; - currentOperand = reverseNotationSolver.Solve(equation); + currentOperand = ReverseNotationSolver.Solve(equation); UpdateLabels(); } } diff --git a/Evil Calculator/ReverseNotattionSolver.cs b/Evil Calculator/ReverseNotattionSolver.cs index f68b4a3..0350ac2 100644 --- a/Evil Calculator/ReverseNotattionSolver.cs +++ b/Evil Calculator/ReverseNotattionSolver.cs @@ -1,57 +1,55 @@ namespace Lunar.Evil_Calculator { - internal class ReverseNotationSolver + internal static class ReverseNotationSolver { - List equation = new(); - - public string ToReverseNotation(List equation) + private static List ToReverseNotation(List equation) { - List TEMP = new List(); - Stack OPERATORS = new Stack(); + List reversedEquation = new List(); + Stack operators = new Stack(); foreach (Token token in equation) { if (token.tokenType == Token.TokenType.Operand) { - TEMP.Add(token); + reversedEquation.Add(token); } if (token.tokenType == Token.TokenType.Operator) { if (token.value == "(") { - OPERATORS.Push(token); + operators.Push(token); } else if (token.value == ")") { - while (OPERATORS.Peek().value != "(") + while (operators.Peek().value != "(") { - TEMP.Add(OPERATORS.Pop()); + reversedEquation.Add(operators.Pop()); } - OPERATORS.Pop(); + operators.Pop(); } else { - while (OPERATORS.Count != 0 && OPERATORS.Peek().priority >= token.priority) + while (operators.Count != 0 && operators.Peek().priority >= token.priority) { - TEMP.Add(OPERATORS.Pop()); + reversedEquation.Add(operators.Pop()); } - OPERATORS.Push(token); + operators.Push(token); } } } - while (OPERATORS.Count != 0) + while (operators.Count != 0) { - TEMP.Add(OPERATORS.Pop()); + reversedEquation.Add(operators.Pop()); } - this.equation = TEMP; - return equationToString(); + + return reversedEquation; } - public string Solve(List equation) + public static string Solve(List originalEquation) { - ToReverseNotation(equation); + List reversedEquation = ToReverseNotation(originalEquation); Stack stack = new Stack(); - foreach (Token token in this.equation) + foreach (Token token in reversedEquation) { double result, left, right; if (token.tokenType == Token.TokenType.Operand) @@ -97,16 +95,5 @@ } return stack.Pop().value; } - - - public string equationToString() - { - string equationString = ""; - foreach (Token token in equation) - { - equationString += token.value + " "; - } - return equationString; - } } }