public int CalculateResult(string input) { var shuntingYardMath = new ShuntingYardSimpleMath(); string convertedInput = RulesTranslator.Translate(input); var tokens = _tokeniser.Convert(convertedInput); var result = shuntingYardMath.Execute(tokens.ToList(), null); return (int)result; }
public FormulaReturnValue EvaluateFormulaFromTokens(ISheet sheet, List <ExcelFormulaToken> tokens, string formulaName) { m_Sheet = sheet; List <ExcelFormulaToken> tokensToEvaluate = new List <ExcelFormulaToken>(); List <ExcelFormulaToken> tokensInFormula = new List <ExcelFormulaToken>(); bool insideFormula = false; for (int i = 0; i < tokens.Count; i++) { ExcelFormulaToken token = tokens[i]; switch (token.Type) { case ExcelFormulaTokenType.Noop: break; case ExcelFormulaTokenType.Operand: if (insideFormula) { tokensInFormula.Add(token); } else { tokensToEvaluate.Add(token); } break; case ExcelFormulaTokenType.Function: { if (token.Value == string.Empty) { if (!insideFormula) { Console.WriteLine("Got end of formula outside of formula"); } else { //float val = EvaluateFormulaFromTokens(workbook, sheet, tokensToEvaluate, formulaName); //ExcelFormulaToken fToken = new ExcelFormulaToken(val.ToString("R"), ExcelFormulaTokenType.Operand); //tokensToEvaluate.Add(fToken); FormulaReturnValue retValue = MakeFormulaCall(formulaName, tokensInFormula); if (retValue != null) { ExcelFormulaToken fToken = new ExcelFormulaToken(string.Empty, ExcelFormulaTokenType.Noop); if (retValue.returnType == FormulaReturnType.floatFormula) { fToken = new ExcelFormulaToken(retValue.floatValue.ToString("R"), ExcelFormulaTokenType.Operand); } else { fToken = new ExcelFormulaToken(retValue.stringValue, ExcelFormulaTokenType.Operand); } tokensToEvaluate.Add(fToken); } insideFormula = false; } } else { formulaName = token.Value; insideFormula = true; } } break; case ExcelFormulaTokenType.Subexpression: if (insideFormula) { tokensInFormula.Add(token); } else { tokensToEvaluate.Add(token); } break; case ExcelFormulaTokenType.Argument: if (insideFormula) { tokensInFormula.Add(token); } else { tokensToEvaluate.Add(token); } break; case ExcelFormulaTokenType.OperatorPrefix: if (insideFormula) { tokensInFormula.Add(token); } else { tokensToEvaluate.Add(token); } break; case ExcelFormulaTokenType.OperatorInfix: if (insideFormula) { tokensInFormula.Add(token); } else { tokensToEvaluate.Add(token); } break; case ExcelFormulaTokenType.OperatorPostfix: if (insideFormula) { tokensInFormula.Add(token); } else { tokensToEvaluate.Add(token); } break; case ExcelFormulaTokenType.Whitespace: break; case ExcelFormulaTokenType.Unknown: break; default: break; } //Console.WriteLine("Token type \"" + token.Type.ToString() + "\" value \"" + token.Value + "\""); } bool formulaIsNumeric = true; StringBuilder sb = new StringBuilder(); for (int i = 0; i < tokensToEvaluate.Count; i++) { ExcelFormulaToken token = tokensToEvaluate[i]; switch (token.Type) { case ExcelFormulaTokenType.Noop: break; case ExcelFormulaTokenType.Operand: { float val = 0; if (float.TryParse(token.Value, out val)) { sb.Append(token.Value); } else { CellArgument cellArg = ParseCellArgument(token.Value); switch (cellArg.caType) { case CellArgumentType.NA: break; case CellArgumentType.CAString: formulaIsNumeric = false; sb.Append(token.Value); break; case CellArgumentType.CAValue: sb.Append(token.Value); break; case CellArgumentType.CACell: { string sheetName = m_Sheet.SheetName; int row = cellArg.row0; int col = cellArg.col0; ICell cell = FetchCellFromSheet(sheetName, row, col); if (cell != null) { if (cell.CellType != CellType.Numeric) { formulaIsNumeric = false; sb.Append(CellValueAsString(cell)); } else { sb.Append(cell.NumericCellValue.ToString("R")); } } } break; case CellArgumentType.CARange: sb.Append(token.Value); break; case CellArgumentType.CASheetCell: { string sheetName = cellArg.sheetName; int row = cellArg.row0; int col = cellArg.col0; ICell cell = FetchCellFromSheet(sheetName, row, col); if (cell != null) { if (cell.CellType != CellType.Numeric) { formulaIsNumeric = false; sb.Append(CellValueAsString(cell)); } else { sb.Append(cell.NumericCellValue.ToString("R")); } } } break; case CellArgumentType.CASheetRange: sb.Append(token.Value); break; default: break; } } if (i < (tokensToEvaluate.Count - 1)) { sb.Append(" "); } } break; case ExcelFormulaTokenType.Function: break; case ExcelFormulaTokenType.Subexpression: break; case ExcelFormulaTokenType.Argument: break; case ExcelFormulaTokenType.OperatorPrefix: sb.Append(token.Value); break; case ExcelFormulaTokenType.OperatorInfix: sb.Append(token.Value); if (i < (tokensToEvaluate.Count - 1)) { sb.Append(" "); } break; case ExcelFormulaTokenType.OperatorPostfix: sb.Append(token.Value); if (i < (tokensToEvaluate.Count - 1)) { sb.Append(" "); } break; case ExcelFormulaTokenType.Whitespace: break; case ExcelFormulaTokenType.Unknown: break; default: break; } } FormulaReturnValue returnValue = new FormulaReturnValue(); if (formulaIsNumeric) { string formulaString = sb.ToString(); //Console.WriteLine("formulaString \"" + formulaString + "\""); ShuntingYardSimpleMath SY = new ShuntingYardSimpleMath(); List <String> ss = formulaString.Split(' ').ToList(); //for (int i = 0; i < ss.Count; i++) //{ // Console.WriteLine("ss " + i.ToString() + ": \"" + ss[i] + "\""); //} Double res = SY.Execute(ss, null); //Console.WriteLine("SY = " + res.ToString("R")); returnValue.returnType = FormulaReturnType.floatFormula; returnValue.floatValue = (float)res; } else { returnValue.returnType = FormulaReturnType.stringFormula; returnValue.stringValue = sb.ToString(); } return(returnValue); }
public UnitTest1() { SY = new ShuntingYardSimpleMath(); }