fixed most obvious stuff #1
@@ -3,7 +3,6 @@
|
||||
class EquationManager
|
||||
{
|
||||
private static readonly HashSet<string> _operators = new() { "+", "-", "*", "/", "^", "√" };
|
||||
private ReverseNotationSolver reverseNotationSolver = new();
|
||||
|
||||
List<Token> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,55 @@
|
||||
namespace Lunar.Evil_Calculator
|
||||
{
|
||||
internal class ReverseNotationSolver
|
||||
internal static class ReverseNotationSolver
|
||||
{
|
||||
List<Token> equation = new();
|
||||
|
||||
public string ToReverseNotation(List<Token> equation)
|
||||
private static List<Token> ToReverseNotation(List<Token> equation)
|
||||
{
|
||||
List<Token> TEMP = new List<Token>();
|
||||
Stack<Token> OPERATORS = new Stack<Token>();
|
||||
List<Token> reversedEquation = new List<Token>();
|
||||
Stack<Token> operators = new Stack<Token>();
|
||||
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<Token> equation)
|
||||
public static string Solve(List<Token> originalEquation)
|
||||
{
|
||||
ToReverseNotation(equation);
|
||||
List<Token> reversedEquation = ToReverseNotation(originalEquation);
|
||||
Stack<Token> stack = new Stack<Token>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user