Ejemplo n.º 1
0
        /// <summary>
        /// построение дерева разбора
        /// </summary>
        /// <param name="str">выражение</param>
        private Operator BuildTree(string str)
        {
            TreeElement leftChild  = null;
            TreeElement rightChild = null;

            int index = 0;

            if (str[index] == '(')
            {
                string newStr = str.Substring(1, str.Length - 2);
                str = newStr;
            }

            if (str[index] == ' ')
            {
                index++;
            }

            char operate = str[index];

            index++;
            if (str[index] == ' ')
            {
                index++;
            }

            if (str[index] == '(')
            {
                string newstr = GetExpression(str, ref index);

                leftChild = BuildTree(newstr);

                index++;
                if (str[index] == ' ')
                {
                    index++;
                }
            }
            else
            {
                if (str[index] == ' ')
                {
                    index++;
                }

                if ((str[index] >= '0') && (str[index] <= '9'))
                {
                    leftChild = new Operand(Convert.ToInt32(str[index]) - Convert.ToInt32('0'));
                }

                index++;

                if (str[index] == ' ')
                {
                    index++;
                }
            }

            if (str[index] == ' ')
            {
                index++;
            }

            if (str[index] == '(')
            {
                string newstr = GetExpression(str, ref index);

                rightChild = BuildTree(newstr);

                if (str[index] == ' ')
                {
                    index++;
                }
            }
            else
            {
                if (str[index] == ' ')
                {
                    index++;
                }

                if ((str[index] >= '0') && (str[index] <= '9'))
                {
                    rightChild = new Operand(Convert.ToInt32(str[index]) - Convert.ToInt32('0'));
                }

                index++;

                if ((index < str.Length - 1) && (str[index] == ' '))
                {
                    index++;
                }
            }

            var newOperator = new Operator(operate);

            newOperator.Left  = leftChild;
            newOperator.Right = rightChild;
            return(newOperator);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// построить дерево разбора
 /// </summary>
 /// <param name="expression">выражение</param>
 public void Build(string expression) => this.root = BuildTree(expression);