removed "equation" field from PeverseNotationSolver because it is not really used and made this class static

This commit is contained in:
2026-01-11 02:54:21 +05:00
parent 46dc513e87
commit 490dc23027
2 changed files with 20 additions and 34 deletions

View File

@@ -3,7 +3,6 @@
class EquationManager class EquationManager
{ {
private static readonly HashSet<string> _operators = new() { "+", "-", "*", "/", "^", "√" }; private static readonly HashSet<string> _operators = new() { "+", "-", "*", "/", "^", "√" };
private ReverseNotationSolver reverseNotationSolver = new();
List<Token> equation = new(); List<Token> equation = new();
string currentOperand = ""; string currentOperand = "";
@@ -145,7 +144,7 @@
{ {
if (currentOperand != "") equation.Add(new Token(Token.TokenType.Operand, currentOperand)); if (currentOperand != "") equation.Add(new Token(Token.TokenType.Operand, currentOperand));
buttonEquals.Text = "#"; buttonEquals.Text = "#";
currentOperand = reverseNotationSolver.Solve(equation); currentOperand = ReverseNotationSolver.Solve(equation);
UpdateLabels(); UpdateLabels();
} }
} }

View File

@@ -1,57 +1,55 @@
namespace Lunar.Evil_Calculator namespace Lunar.Evil_Calculator
{ {
internal class ReverseNotationSolver internal static class ReverseNotationSolver
{ {
List<Token> equation = new(); private static List<Token> ToReverseNotation(List<Token> equation)
public string ToReverseNotation(List<Token> equation)
{ {
List<Token> TEMP = new List<Token>(); List<Token> reversedEquation = new List<Token>();
Stack<Token> OPERATORS = new Stack<Token>(); Stack<Token> operators = new Stack<Token>();
foreach (Token token in equation) foreach (Token token in equation)
{ {
if (token.tokenType == Token.TokenType.Operand) if (token.tokenType == Token.TokenType.Operand)
{ {
TEMP.Add(token); reversedEquation.Add(token);
} }
if (token.tokenType == Token.TokenType.Operator) if (token.tokenType == Token.TokenType.Operator)
{ {
if (token.value == "(") if (token.value == "(")
{ {
OPERATORS.Push(token); operators.Push(token);
} }
else if (token.value == ")") 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 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<Token> equation) public static string Solve(List<Token> originalEquation)
{ {
ToReverseNotation(equation); List<Token> reversedEquation = ToReverseNotation(originalEquation);
Stack<Token> stack = new Stack<Token>(); Stack<Token> stack = new Stack<Token>();
foreach (Token token in this.equation) foreach (Token token in reversedEquation)
{ {
double result, left, right; double result, left, right;
if (token.tokenType == Token.TokenType.Operand) if (token.tokenType == Token.TokenType.Operand)
@@ -97,16 +95,5 @@
} }
return stack.Pop().value; return stack.Pop().value;
} }
public string equationToString()
{
string equationString = "";
foreach (Token token in equation)
{
equationString += token.value + " ";
}
return equationString;
}
} }
} }