public PostFix SetVariable(string variable, string number) { int length = _input.Length; for (int i = 0; i < length; i++) { RPN.Token token = _input[i]; if (token.Type == RPN.Type.Variable && token.Value == variable) { _input[i] = new RPN.Token(number, 0, RPN.Type.Number); } } return(this); }
public double Compute(RPN.Token[] input) { _stopwatch.Start(); for (int i = 0; i < input.Length; i++) { RPN.Token token = input[i]; switch (token.Type) { case RPN.Type.Number: _stack.Push(double.Parse(token.Value)); break; case RPN.Type.Variable: _variables.Push(token); break; case RPN.Type.Operator: { Operator Operator = _dataStore.Operators[token.Value]; double[] arguments = GetArguments(token.Arguments); double ans = Operator.Compute(arguments); _stack.Push(ans); break; } case RPN.Type.Function: { //Looks up the function in the Dict Function function = _dataStore.Functions[token.Value]; double[] arguments = GetArguments(token.Arguments); double ans = function.Compute(arguments); _stack.Push(ans); break; } default: throw new NotImplementedException(token + " " + token.ToString().Length); } } _stopwatch.Stop(); _dataStore.AddTimeRecord("Evaluation", _stopwatch); if (_dataStore.DebugMode) { Write(_dataStore.TimeRecords().ToString()); Write($"Frequency: {Stopwatch.Frequency}"); Write(""); } if (_stack.Count != 1) { return(double.NaN); } if (_dataStore.Format.ContainsKey(_stack.Peek())) { Write($"The answer may also be written in the following manner: {_dataStore.Format[_stack.Peek()]}"); } return(_stack.Pop()); }