예제 #1
0
        public override double Calculate(ref My_Stack <double> stack)
        {
            double a = stack.pop();

            if (stack.is_empty())
            {
                return(-a);
            }
            double b = stack.pop();

            return(b - a);
        }
예제 #2
0
 private void AddOp(string op)
 {
     if (string_stack.is_empty())
     {
         string_stack.push(op);
     }
     else
     {
         string chr = string_stack.last();
         while (charPriority(chr) >= charPriority(op))
         {
             polish_text = (polish_text + " " + chr + " ");
             string_stack.pop();
             if (string_stack.is_empty())
             {
                 break;
             }
             chr = string_stack.last();
         }
         string_stack.push(op);
     }
 }
예제 #3
0
 public override double Calculate(ref My_Stack <double> stack)
 {
     return(stack.pop() * stack.pop());
 }
예제 #4
0
 public override double Calculate(ref My_Stack <double> stack)
 {
     return(Convert.ToDouble(stack.pop() == stack.pop()));
 }
예제 #5
0
        public override double Calculate(ref My_Stack <double> stack)
        {
            double a = stack.pop();

            return(stack.pop() / a);
        }
예제 #6
0
        public override double Calculate(ref My_Stack <double> stack)
        {
            double a = stack.pop();

            return(Math.Pow(stack.pop(), a));
        }
예제 #7
0
        public ReversePolishNotation(string text, List <Operator> operators)
        {
            for (int x = 0; x < text.Length; x++)
            {
                if (text[x] == '-')
                {
                    if (x > 0 && !char.IsNumber(text[x - 1]))
                    {
                        text = text.Substring(0, x) + "$" + text.Substring(x + 1, text.Length - x - 1);
                    }
                }
            }

            this.operators = operators;
            this.operators.Add(new u_Multi("$", int.MaxValue));
            string s_operator = "";

            this.text    = text;
            string_stack = new My_Stack <string>(text.Length);
            text.Replace(" ", "");
            for (int x = 0; x < text.Length; x++)
            {
                if (char.IsDigit(text[x]) || text[x] == ',')
                {
                    if (s_operator != "")
                    {
                        AddOp(s_operator);
                        s_operator = "";
                    }
                    polish_text += text[x];
                }
                else if (text[x] == ')')
                {
                    try
                    {
                        for (string s = string_stack.pop(); s != "("; s = string_stack.pop())   //выгружаем стек
                        {
                            polish_text += " " + s;
                        }
                    }
                    catch
                    {
                        //return;
                        throw new Exception("Целостность скобок нарушена !");
                    }
                }
                else if (text[x] == '(')
                {
                    if (s_operator != "")
                    {
                        AddOp(s_operator);
                        s_operator = "";
                    }
                    string_stack.push("(");
                }
                else
                {
                    if (s_operator == "")
                    {
                        polish_text += " ";
                    }
                    s_operator += text[x];
                    foreach (var c in operators)
                    {
                        if (c.name == s_operator)
                        {
                            AddOp(c.name);
                            s_operator = "";
                            break;
                        }
                    }
                }
            }
            if (s_operator != "")
            {
                AddOp(s_operator);
            }
            while (!string_stack.is_empty())
            {
                polish_text += " " + string_stack.pop();
            }

            while (polish_text.Contains("  "))
            {
                polish_text = polish_text.Replace("  ", " ");
            }
        }