Node_P PlusMinus()
        {
            Node_P left = MultDiv();

            while (true)
            {
                //Identify the operation
                Operation op = Operation.Null;
                if (splitter.current_sign == Sign.Plus)
                {
                    op = Operation.Addition;
                }
                else if (splitter.current_sign == Sign.Minus)
                {
                    op = Operation.Substraction;
                }

                //If no plus or minus sign is found
                if (op == Operation.Null)
                {
                    return(left);
                }

                splitter.Move_sign(); //move to the next sign

                Node_P right = MultDiv();

                //Create a node with a correspondent binary operation
                left = new NodePolBinary(left, right, op);
            }
        }
        Node_P MultDiv()
        {
            //Check whether the current node is a unary operator
            Node_P left = Unary();

            while (true)
            {
                //Identify the operation
                Operation op = Operation.Null;
                if (splitter.current_sign == Sign.Divide)
                {
                    op = Operation.Division;
                }
                else if (splitter.current_sign == Sign.Multiply)
                {
                    op = Operation.Multiplication;
                }

                //If no multiply or divide sign is found
                if (op == Operation.Null)
                {
                    return(left);
                }

                splitter.Move_sign(); //move to the next sign

                //Check whether the current node is a unary operator
                Node_P right = Unary();

                //Create a node with a correspondent binary operation
                left = new NodePolBinary(left, right, op);
            }
        }
        Node_P Unary()
        {
            while (true)
            {
                //In case of unary plus
                //Unary plus doesn't need any operation
                if (splitter.current_sign == Sign.Plus)
                {
                    splitter.Move_sign(); //Move to the next sign
                    continue;
                }

                //In case of unary minus
                else if (splitter.current_sign == Sign.Minus)
                {
                    splitter.Move_sign(); //Move to the next sign

                    //Recursive call to find the sequence of unary operators (if exists)
                    //eg -+--10 = -10
                    Node_P a = Unary();

                    //Node for unary operator
                    return(new NodePolUnary(a, Operation.UnaryMinus));
                }

                //Just return number if no unary operator is found
                return(Part());
            }
        }
        //Find and create the leaf with polynomial
        Node_P Part()
        {
            //Polynomial
            if (splitter.current_sign == Sign.Pol)
            {
                Node_P part = new NodePolynomial(splitter.pol); //create the node with found polynomial
                splitter.Move_sign();                           //move to the next sign
                return(part);
            }

            //Parenthesis
            if (splitter.current_sign == Sign.Open)
            {
                splitter.Move_sign(); //move to the next sign

                //Parse sequentially all the expression after the (
                Node_P part = PlusMinus();

                // Find the )
                if (splitter.current_sign != Sign.Close) //if there's no )
                {
                    Console.WriteLine("Missing close parenthesis");
                }
                splitter.Move_sign(); //move to the next sign

                return(part);
            }

            //If the sign is not classified
            Console.WriteLine($"Unknown sign: {splitter.current_sign}");
            throw new SystemException($"Unknown sign: {splitter.current_sign}");
        }
        //Method that parses the whole expression, which calls other methods one by one
        //in order to build a correct binary tree of expression
        public Node_P CheckExpression()
        {
            Node_P node = PlusMinus();

            if (splitter.current_sign != Sign.EOF)
            {
                Console.WriteLine("Unexpected characters at end of expression");
                Environment.Exit(0);
            }

            return(node);
        }
Beispiel #6
0
        Operation op; //operation between sides (eg. sum, substration, etc.)

        //Constructor
        public NodePolBinary(Node_P L, Node_P R, Operation O)
        {
            left  = L;
            right = R;
            op    = O;
        }
Beispiel #7
0
 public NodePolUnary(Node_P R, Operation O)
 {
     right = R;
     op    = O;
 }