Example #1
0
 public string Calculate()
 {
     try
     {
         int_stack = new My_Stack <double>();
         List <string> calculate_text = new List <string>();
         calculate_text = polish_text.Split(' ').ToList();
         foreach (string x in calculate_text)
         {
             if (x == " " || x == "")
             {
                 continue;
             }
             foreach (var v in operators)
             {
                 if (v.name == x)
                 {
                     int_stack.push(v.Calculate(ref int_stack));
                     goto end2;
                 }
             }
             int_stack.push(Convert.ToDouble(x));
             end2 :;
         }
         return(Convert.ToString(int_stack.last()));
     }
     catch
     {
         return(text);
     }
 }
Example #2
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);
        }
 public override double Calculate(ref My_Stack <double> stack)
 {
     return(value);
 }
 public override double Calculate(ref My_Stack <double> stack)
 {
     return(stack.pop() * stack.pop());
 }
Example #5
0
 public override double Calculate(ref My_Stack <double> stack)
 {
     return(Convert.ToDouble(stack.pop() == stack.pop()));
 }
        public override double Calculate(ref My_Stack <double> stack)
        {
            double a = stack.pop();

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

            return(Math.Pow(stack.pop(), a));
        }
Example #8
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("  ", " ");
            }
        }
 virtual public double Calculate(ref My_Stack <double> stack)
 {
     return(0);
 }