コード例 #1
0
        private Summand CreateSummand(string lexem)
        {
            float coefficient = 1.0f;

            Variable[] variables = null;

            using (var lexemEnumerator = new LexemEnumerator(lexem))
            {
                while (lexemEnumerator.MoveNext())
                {
                    if (lexemEnumerator.Current == Symbol.Minus)
                    {
                        coefficient *= -1;
                    }

                    if (char.IsDigit(lexemEnumerator.Current))
                    {
                        coefficient *= ReadNextFloatDigit(lexemEnumerator);
                    }

                    if (lexemEnumerator.IsHasValue && char.IsLetter(lexemEnumerator.Current))
                    {
                        variables = ReadVariables(lexemEnumerator);
                    }
                }
            }
            return(new Summand(variables, coefficient));
        }
コード例 #2
0
        private Variable[] ReadVariables(LexemEnumerator enumerator)
        {
            var resultVariables = new List <Variable>();

            while (enumerator.IsHasValue)
            {
                resultVariables.Add(ReadNextVariable(enumerator));
            }

            return(resultVariables.ToArray());
        }
コード例 #3
0
        private Variable ReadNextVariable(LexemEnumerator enumerator)
        {
            var variableLexem = enumerator.Current;

            if (enumerator.MoveNext() &&
                enumerator.Current == Symbol.DegreeSymbol)
            {
                enumerator.MoveNext();
                var degree = ReadNextIntDigit(enumerator);

                return(new Variable(variableLexem, degree));
            }

            return(new Variable(variableLexem));
        }
コード例 #4
0
        private int ReadNextIntDigit(LexemEnumerator enumerator)
        {
            var lexem = enumerator.Current.ToString();

            while (enumerator.MoveNext())
            {
                if (char.IsDigit(enumerator.Current))
                {
                    lexem += enumerator.Current;
                }
                else
                {
                    break;
                }
            }
            return(int.Parse(lexem));
        }
コード例 #5
0
        private float ReadNextFloatDigit(LexemEnumerator enumerator)
        {
            var lexem = enumerator.Current.ToString();

            while (enumerator.MoveNext())
            {
                var ch = enumerator.Current;
                if (char.IsDigit(ch) || ch == Symbol.DotSeparator)
                {
                    lexem += ch;
                }
                else
                {
                    break;
                }
            }
            return(ParseToFloat(lexem));
        }