Esempio n. 1
0
        /// <summary>
        /// 获取下一个计算节点
        /// </summary>
        /// <returns></returns>
        public CalculateNode GetNextNode()
        {
            char ch;

            switch (1)
            {
            case 1:
                if (_currentIndex == _expressionStr.Length)
                {
                    return(new CalculateNode(CalculationSymbol.EOF));
                }

                ch = _expressionStr[_currentIndex++];
                if (ch == ' ')
                {
                    goto case 1;
                }
                else if (ch == '(')
                {
                    return(new CalculateNode(CalculationSymbol.OpenBracket));
                }
                else if (ch == ')')
                {
                    return(new CalculateNode(CalculationSymbol.CloseBracket));
                }
                else if (ch == '+')
                {
                    return(new CalculateNode(CalculationSymbol.Add));
                }
                else if (ch == '-')
                {
                    return(new CalculateNode(CalculationSymbol.Sub));
                }
                else if (ch == '*')
                {
                    return(new CalculateNode(CalculationSymbol.Mul));
                }
                else if (ch == '/')
                {
                    return(new CalculateNode(CalculationSymbol.Div));
                }
                else if (char.IsDigit(ch))
                {
                    _currentIndex--;
                    goto case 2;
                }
                else
                {
                    throw new MathEvaluatorException();
                }

            case 2:
                var node     = new CalculateNode(CalculationSymbol.Number);
                var dotIndex = _currentIndex + 1;
                while (dotIndex < _expressionStr.Length && char.IsDigit(_expressionStr[dotIndex]))
                {
                    dotIndex++;
                }

                while (_currentIndex < _expressionStr.Length && char.IsDigit(ch = _expressionStr[_currentIndex]))
                {
                    var digit = ch - 48;
                    if (digit > 0)
                    {
                        node.Value += _m1[digit - 1, dotIndex - _currentIndex - 1];
                    }
                    _currentIndex++;
                }

                if (ch == ',' || ch == '.')
                {
                    _currentIndex++;
                    while (_currentIndex < _expressionStr.Length && char.IsDigit(ch = _expressionStr[_currentIndex]))
                    {
                        var digit = ch - 48;
                        if (digit > 0)
                        {
                            node.Value += _m2[digit - 1, _currentIndex - dotIndex - 1];
                        }
                        _currentIndex++;
                    }
                }

                return(node);
            }
        }
Esempio n. 2
0
 /// <summary>
 /// 获取下一个计算节点
 /// </summary>
 private void NextNode() => _node = _parser.GetNextNode();