Пример #1
0
        private string CalcTokensSubset(List <string> tks)
        {
            List <string> tokens = new List <string>(tks);

            while (tokens.Count > 1)
            {
                SortedDictionary <double, int> weightIndexDict = new SortedDictionary <double, int>();
                MathExpression expression = new MathExpression();
                for (int p = 0; p < tokens.Count; p++)
                {
                    try
                    {
                        weightIndexDict.Add(_operandDictionary[tokens[p]].OperationTier, p);
                    }
                    catch (Exception ex)
                    {}
                }
                int indexToCalc = weightIndexDict.Values.First();
                expression.Operand = tokens[indexToCalc];
                expression.Left    = Convert.ToDecimal(tokens[indexToCalc - 1]);
                expression.Right   = Convert.ToDecimal(tokens[indexToCalc + 1]);
                string result = RunExpression(expression);
                tokens = ReplaceIndexRangeWithValue(tokens, indexToCalc - 1, indexToCalc + 1, result);
            }
            string finalResult = tokens[0];

            return(finalResult);
        }
Пример #2
0
        private string RunExpression(MathExpression expression)
        {
            string result = _operandDictionary[expression.Operand].Calculate(expression.Left, expression.Right).ToString();

            return(result);
        }