Beispiel #1
0
        private string TransformBigNumbers(string str)
        {
            var pieces = str.Split(' ').ToList();

            for (var i = 0; i < pieces.Count; ++i)
            {
                if (_bigNumberDict.ContainsKey(pieces[i]))
                {
                    var t = _bigNumberDict[pieces[i]];

                    double value;
                    if (i != 0 && DoubleParser.TryParse(pieces[i - 1], out value))
                    {
                        t        *= value;
                        pieces[i] = t.ToString();
                        pieces.RemoveAt(i - 1);
                    }
                    else
                    {
                        pieces[i] = t.ToString();
                    }
                }
            }

            for (int i = 1; i < pieces.Count; ++i)
            {
                double value, value2;
                if (DoubleParser.TryParse(pieces[i - 1], out value) && DoubleParser.TryParse(pieces[i], out value2))
                {
                    value2       += value;
                    pieces[i]     = value2.ToString();
                    pieces[i - 1] = "";
                }
            }

            var sb = new StringBuilder();

            foreach (var p in pieces)
            {
                if (p == "")
                {
                    continue;
                }
                sb.Append(p + " ");
            }
            return(sb.ToString().Trim(' '));
        }
Beispiel #2
0
        public static List <string> Steps(string expression)
        {
            Success   = true;
            AllValues = new List <string>();
            var    output = new List <string>();
            var    parts  = expression.Split(' ');
            double res;

            if (DoubleParser.TryParse(parts[0], out res))
            {
                for (var i = 1; i < parts.Length - 1; i += 2)
                {
                    var    step = res.ToString(Culture.EngInfo) + " " + parts[i] + " " + parts[i + 1] + " = ";
                    double nextOperand;
                    if (DoubleParser.TryParse(parts[i + 1], out nextOperand))
                    {
                        if (parts[i] == "+")
                        {
                            res += nextOperand;
                        }
                        else if (parts[i] == "-")
                        {
                            res -= nextOperand;
                        }
                        else if (parts[i] == "*")
                        {
                            res *= nextOperand;
                        }
                        else
                        {
                            if (nextOperand == 0)
                            {
                                Error(output, expression);
                                break;
                            }
                            res /= nextOperand;
                            res  = Math.Round(res, 3);
                        }
                        step += res.ToString(Culture.EngInfo);
                        AllValues.Add(res.ToString(Culture.EngInfo));
                        output.Add(step);
                    }
                    else
                    {
                        Error(output, expression);
                        break;
                    }
                }
            }
            else
            {
                Error(output, expression);
            }

            if (output.Count == 0)
            {
                AllValues.Add(res.ToString(Culture.EngInfo));
                output.Add(res.ToString(Culture.EngInfo));
            }
            return(output);
        }