示例#1
0
        /// <summary>
        /// Syntax analysis of reverse-polish notation
        /// </summary>
        /// <param name="Stack">Stack which contains operands</param>
        /// <param name="Token">Token</param>
        /// <returns>Stack which contains operands</returns>
        void SyntaxAnalysisRPN(Stack<double> Stack, Token Token)
        {
            // if it's operand then just push it to stack
            if (Token.IsNumber)
                Stack.Push((Constant)Token);

            else if (Token.IsVariable)
                Stack.Push(((Variable)Token).Value);

            // Otherwise apply operator or function to elements in stack
            else if (Token.IsUnaryOperator)
                Stack.Push((Token as UnaryOperator).Invoke(Stack.Pop()));

            else if (Token.IsTrigonometricFunction)
                Stack.Push((Token as TrigonometricFunction).Invoke(Stack.Pop(), AngleType));

            else if (Token.IsUnaryFunction || Token.IsPostfixFunction)
                Stack.Push((Token as UnaryFunction).Invoke(Stack.Pop()));

            else if (Token.IsBinaryOperator)
            {
                double Argument2 = Stack.Pop();
                double Argument1 = Stack.Pop();

                Stack.Push((Token as BinaryOperator).Invoke(Argument1, Argument2));
            }

            else if (Token.IsBinaryFuncion)
            {
                double Argument2 = Stack.Pop();
                double Argument1 = Stack.Pop();

                Stack.Push((Token as BinaryFunction).Invoke(Argument1, Argument2));
            }

            else if (Token.IsTernaryFuncion)
            {
                double Argument3 = Stack.Pop();
                double Argument2 = Stack.Pop();
                double Argument1 = Stack.Pop();

                Stack.Push((Token as TernaryFunction).Invoke(Argument1, Argument2, Argument3));
            }
        }
示例#2
0
        /// <summary>
        /// Syntax analysis of infix notation
        /// </summary>
        /// <param name="Token">Token</param>
        /// <param name="OutputList">Token List (math expression in RPN)</param>
        /// <param name="Stack">Stack which contains operators (or functions)</param>
        /// <returns>Token List (math expression in RPN)</returns>
        void SyntaxAnalysisInfixNotation(Token Token, List<Token> OutputList, Stack<Token> Stack)
        {
            if (Token == Comma) return;

            // If it's a number just put to list
            else if (Token.IsNumber || Token.IsVariable) OutputList.Add(Token);

            // if it's a function push to stack
            else if (Token.IsFunction)
            {
                if (Token.IsPostfixFunction) OutputList.Add(Token);
                else Stack.Push(Token);
            }

            // If its '(' push to stack
            else if (Token == LeftParenthesis) Stack.Push(Token);

            else if (Token == RightParenthesis)
            {
                // If its ')' pop elements from stack to output list
                // until find the '('
                Token Element;
                while ((Element = Stack.Pop()) != LeftParenthesis) OutputList.Add(Element);

                // if after this a function is in the peek of stack then put it to list
                if (Stack.Count > 0 && Stack.Peek().IsFunction)
                    OutputList.Add(Stack.Pop());
            }
            else
            {
                // While priority of elements at peek of stack >= (>) token's priority
                // put these elements to output list
                while (Stack.Count > 0 && (Token <= Stack.Peek()))
                    OutputList.Add(Stack.Pop());

                Stack.Push(Token);
            }
        }