Ejemplo n.º 1
0
        static int RPNStackMaths(object[] RPNEquation)
        {
            MyStack <int> stack = new MyStack <int>(RPNEquation.Length);

            for (int i = 0; i != RPNEquation.Length; i++)
            {
                if (int.TryParse(RPNEquation[i].ToString(), out int c))
                {
                    stack.Push(c);
                }
                else
                {
                    int a = stack.Pop();
                    int b = stack.Pop();
                    if (RPNEquation[i].ToString() == "+")
                    {
                        c = a + b;
                    }
                    else if (RPNEquation[i].ToString() == "-")
                    {
                        c = a - b;
                    }
                    else if (RPNEquation[i].ToString() == "*")
                    {
                        c = a * b;
                    }
                    else if (RPNEquation[i].ToString() == "/")
                    {
                        c = a / b;
                    }
                    stack.Push(c);
                }
            }
            int result = stack.Pop();

            if (!stack.IsEmpty())
            {
                throw new InvalidOperationException("Input is not in RPN format");
            }
            else
            {
                return(result);
            }
        }
Ejemplo n.º 2
0
        static object[] ShuntingYardAlgorithm(object[] infixEquation)
        {
            List <object> output           = new List <object> {
            };
            MyStack <string> operatorStack = new MyStack <string>();

            for (int i = 0; i < infixEquation.Length; i++)
            {
                if (int.TryParse(infixEquation[i].ToString(), out int c))
                {
                    output.Add(infixEquation[i]);
                }
                else
                {
                    if (infixEquation[i].ToString() == "+")
                    {
                        if (operatorStack.IsEmpty())
                        {
                            if (operatorStack.Top().ToString() == "*" || operatorStack.Top().ToString() == "/")
                            {
                                output.Add(operatorStack.Pop());
                            }
                        }
                        operatorStack.Push("+");
                    }
                    else if (infixEquation[i].ToString() == "-")
                    {
                        if (operatorStack.IsEmpty())
                        {
                            if (operatorStack.Top().ToString() == "*" || operatorStack.Top().ToString() == "/")
                            {
                                output.Add(operatorStack.Pop());
                            }
                        }
                        operatorStack.Push("-");
                    }
                    else if (infixEquation[i].ToString() == "*")
                    {
                        operatorStack.Push("*");
                    }
                    else if (infixEquation[i].ToString() == "/")
                    {
                        operatorStack.Push("/");
                    }
                    else if (infixEquation[i].ToString() == "(")
                    {
                        operatorStack.Push("(");
                    }
                    else if (infixEquation[i].ToString() == ")")
                    {
                        while (operatorStack.Top() != "(")
                        {
                            output.Add(operatorStack.Pop());
                        }
                        operatorStack.Pop();
                    }
                }
            }
            while (!operatorStack.IsEmpty())
            {
                output.Add(operatorStack.Pop());
            }
            return(output.ToArray());
        }