Exemplo n.º 1
0
        public static string RunEstimate()
        {
            ArrayList   Stack = CreateStack();
            Stack <int> tmp   = new Stack <int>();
            int         n;

            foreach (string s in Stack)
            {
                if (int.TryParse(s, out n))
                {
                    tmp.Push(n);
                }
                else
                {
                    switch (s)
                    {
                    case "*":
                    {
                        tmp.Push(MathOperations.Mult(tmp.Pop(), tmp.Pop()));
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    case "/":
                    {
                        n = tmp.Pop();
                        tmp.Push(MathOperations.Div(tmp.Pop(), n));
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    case "%":
                    {
                        n = tmp.Pop();
                        tmp.Push(MathOperations.Mod(tmp.Pop(), n));
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    case "+":
                    {
                        tmp.Push(MathOperations.Add(tmp.Pop(), tmp.Pop()));
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    case "-":
                    {
                        n = tmp.Pop();
                        if (tmp.Count == 0)
                        {
                            tmp.Push(MathOperations.IABS(n));
                        }
                        else
                        {
                            tmp.Push(MathOperations.Sub(tmp.Pop(), n));
                        }
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    case "m":
                    {
                        tmp.Push(MathOperations.IABS(tmp.Pop()));
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    case "p":
                    {
                        tmp.Push(MathOperations.ABS(tmp.Pop()));
                        if (MathOperations.lastError.Length > 0)
                        {
                            ShowMessage = true;
                            Expression  = MathOperations.lastError;
                            return(Expression);
                        }
                        break;
                    }

                    default:
                    {
                        ShowMessage = true;
                        Expression  = "Error in calculating";
                        return(Expression);
                    }
                    }
                }
            }

            return(tmp.Pop().ToString());
        }