Esempio n. 1
0
        /// <summary>
        /// Converts a given postfix array to a infix expression.
        /// </summary>
        /// <param name="postfixExp"></param>
        /// <returns></returns>
        public static string Convert2Infix(string[] postfixExp)
        {
            Stack <string> tokenStack  = new Stack <string>();
            List <string>  postfixList = new List <string>();

            //Operator lists for different number of operands
            List <string> unaryOperators  = new List <string>();
            List <string> binaryOperators = new List <string>();

            OperatorFunctions.UnaryOperators(unaryOperators);
            OperatorFunctions.BinaryOperators(binaryOperators);

            //Recreate postfix list
            for (int i = 0; i < postfixExp.Length; i++)
            {
                postfixList.Add(postfixExp[i]);
            }
            postfixList.Reverse();

            //Iterate over all tokens in postfix expression
            foreach (string token in postfixList)
            {
                //Binary operators
                if (binaryOperators.Any(token.Contains))
                {
                    string x   = tokenStack.Pop();
                    string y   = tokenStack.Pop();
                    string exp = "(" + y + token + x + ")";

                    tokenStack.Push(exp);
                }
                //Unary operators
                else if (unaryOperators.Any(token.Contains))
                {
                    string x   = tokenStack.Pop();
                    string exp = "(" + token + "(" + x + ")" + ")";

                    tokenStack.Push(exp);
                }

                /*
                 * Add Numbers directly to string, the first
                 * one doesn't need a buffer space in-between
                 */
                else
                {
                    tokenStack.Push(token);
                }
            }

            return(tokenStack.Pop());
        }
Esempio n. 2
0
        /// <summary>
        /// Takes a given postfix expression and evaluates it
        /// and returns the value to the main program to test
        /// for convergence.
        /// </summary>
        /// <param name="postfixExp"></param>
        /// <returns></returns>
        public static double EvaluatePostFix(string postfixExp)
        {
            string x, y, ans;

            string[]       postfixArray = postfixExp.Split(null);
            Stack <string> tokenStack   = new Stack <string>();

            //Operator lists for different number of operands
            List <string> unaryOperators  = new List <string>();
            List <string> binaryOperators = new List <string>();

            OperatorFunctions.UnaryOperators(unaryOperators);
            OperatorFunctions.BinaryOperators(binaryOperators);

            //Iterate over all tokens in postfix expression
            foreach (string token in postfixArray)
            {
                if (binaryOperators.Any(token.Contains))
                {
                    //Evaluate functions that take two arguments
                    y = tokenStack.Pop();
                    x = tokenStack.Pop();

                    ans = OperatorFunctions.EvaluateExp(x, y, token).ToString();

                    tokenStack.Push(ans);
                }
                else if (unaryOperators.Any(token.Contains))
                {
                    //Evaluate functions that only take a single argument
                    x = "temp";
                    y = tokenStack.Pop();

                    ans = OperatorFunctions.EvaluateExp(x, y, token).ToString();

                    tokenStack.Push(ans);
                }
                else
                {
                    tokenStack.Push(token);
                }
            }

            //Last thing on stack is the answer
            ans = tokenStack.Pop();
            return(double.Parse(ans));
        }
        /// <summary>
        /// Takes in a reverse postfix array and recursively grabs
        /// an operator and two operands to be derivated such
        /// that they are all complete expressions.
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        private static string CompleteExpressions(string[] postfixArray)
        {
            //Operator lists for different number of operands
            List <string> unaryOperators  = new List <string>();
            List <string> binaryOperators = new List <string>();

            OperatorFunctions.UnaryOperators(unaryOperators);
            OperatorFunctions.BinaryOperators(binaryOperators);

            //Initialize
            int    i     = 0;
            string token = postfixArray[0];

            //Split between no, unary, and binary operators
            if (unaryOperators.Any(token.Contains))
            {
                string A = postfixArray[1];
                string B = "temp";

                bool completeA = IsComplete(A);

                //Loop until complete expressons
                while (!completeA)
                {
                    i++;

                    if (!completeA)
                    {
                        A += " " + postfixArray[i + 1];
                    }

                    //Recheck for completeness and increment
                    completeA = IsComplete(A);
                }

                string[] ArrA = A.Split(' ');
                string[] ArrB = B.Split(' ');
                return(EvaluateDerivative(ArrA, ArrB, token));
            }
            else if (binaryOperators.Any(token.Contains))
            {
                string B = postfixArray[1];
                string A = postfixArray[2];

                bool completeA = IsComplete(A);
                bool completeB = IsComplete(B);

                //Loop until complete expressons
                while ((!completeA) || (!completeB))
                {
                    //Try to fix completeness
                    if (!completeB)
                    {
                        B += " " + A;
                        A  = postfixArray[i + 3];
                    }
                    else if (!completeA)
                    {
                        A += " " + postfixArray[i + 3];
                    }

                    //Recheck for completeness and increment
                    completeA = IsComplete(A);
                    completeB = IsComplete(B);
                    i++;
                }

                string[] ArrA = A.Split(' ');
                string[] ArrB = B.Split(' ');
                return(EvaluateDerivative(ArrA, ArrB, token));
            }
            else
            {
                string[] tempList = { "temp" };
                return(EvaluateDerivative(tempList, postfixArray, "temp"));
            }
        }