Beispiel #1
0
        private static double ResolvePostfix(Queue <object> postFix)
        {
            Stack <double> state = new Stack <double>();

            while (postFix.Count > 0)
            {
                while (postFix.Count > 0 && postFix.Peek() is double)
                {
                    state.Push((double)postFix.Dequeue());
                }

                if (postFix.Count > 0)
                {
                    Functions.Function func = postFix.Dequeue() as Functions.Function;

                    List <double> parameters = new List <double>();
                    for (int i = 0; i < func.ParameterCount; i++)
                    {
                        parameters.Add(state.Pop());
                    }

                    parameters.Reverse();

                    state.Push(func.Call(parameters));
                }
            }

            if (state.Count != 1)
            {
                throw new Exception("Failed to evaluate expression.  No result available");
            }

            return(state.Pop());
        }
Beispiel #2
0
        public void Push(Functions.Function in_func)
        {
            if (Count >= in_func.ParameterCount)
            {
                List <double> parameters = new List <double>();

                for (int i = in_func.ParameterCount; i > 0; i--)
                {
                    parameters.Add(this[Count - i].Value);
                    RemoveAt(Count - i);
                }

                string expression = in_func.Name + "(";

                for (int i = 0; i < parameters.Count; i++)
                {
                    expression += parameters[i].ToString();
                    if (i != parameters.Count - 1)
                    {
                        expression += ",";
                    }
                }

                expression += ")";

                Push(in_func.Call(parameters), expression);
            }
            else
            {
                throw new Exception("Not enough operand on the stack.  Add at least two numbers in the stack.");
            }
        }