Example #1
0
        // The main method that converts given infix expression
        // to postfix expression.
        public static String infixToPostfix(String exp)
        {
            // initializing empty String for result
            var result = "";

            // initializing empty stack
            var stack = new GenericStack <char>();

            for (int i = 0; i < exp.Length; ++i)
            {
                var c = exp[i];

                // If the scanned character is an operand, add it to output.
                if (Char.IsLetterOrDigit(c))
                {
                    result += c;
                }

                // If the scanned character is an '(', push it to the stack.
                else if (c == '(')
                {
                    stack.push(c);
                }

                //  If the scanned character is an ')', pop and output from the stack
                // until an '(' is encountered.
                else if (c == ')')
                {
                    while (!stack.isEmpty() && stack.peek() != '(')
                    {
                        result += stack.pop();
                    }

                    if (!stack.isEmpty() && stack.peek() != '(')
                    {
                        return("Invalid Expression"); // invalid expression
                    }
                    else
                    {
                        stack.pop();
                    }
                }
                else // an operator is encountered
                {
                    while (!stack.isEmpty() && Prec(c) <= Prec(stack.peek()))
                    {
                        result += stack.pop();
                    }
                    stack.push(c);
                }
            }

            // pop all the operators from the stack
            while (!stack.isEmpty())
            {
                result += stack.pop();
            }

            return(result);
        }
 public static void ConverTo(GenericStack <int> dStack, GenericQueue <int> dQueue)
 {
     while (!dStack.isEmpty())
     {
         var tmp = dStack.peek();
         dQueue.Enqueue(tmp);
         dStack.pop();
     }
     Console.WriteLine(dQueue);
 }
        public static void MulBase(int n, int b)
        {
            var Digits = new GenericStack <int>();

            do
            {
                Digits.push(n % b);
                n /= b;
            } while (n != 0);
            while (Digits.Size > 0)
            {
                Console.Write(Digits.pop());
            }
        }
 public static bool KiemTraXauNgoac(string str, GenericStack <char> a)
 {
     //string str là chuỗi xâu ngoặc sẽ nhập vào
     for (int i = 0; i < str.Length; i++)                     //duyệt lần lượt hết chuỗi
     {
         if (str[i] == '(' || str[i] == '[' || str[i] == '{') //nếu gặp dấu mở ngoặc
         {
             a.push(str[i]);                                  //push hết vào stack
         }
         else //nếu gặp dấu đóng ngoặc
         {
             if (!a.isEmpty())           //nếu stack khác rỗng
             {
                 if (str[i] == ']')      //kiểm tra xem phần tử ngoặc đỉnh stack có hợp với str[i] hay không
                 {
                     if (a.TOP() != '[') //không hợp
                     {
                         return(false);  //sai
                     }
                 }
                 if (str[i] == ')')      //kiểm tra xem phần tử ngoặc đỉnh stack có hợp với str[i] hay không
                 {
                     if (a.TOP() != '(') //không hợp
                     {
                         return(false);  //sai
                     }
                 }
                 if (str[i] == '}')      //kiểm tra xem phần tử ngoặc đỉnh stack có hợp với str[i] hay không
                 {
                     if (a.TOP() != '{') //không hợp
                     {
                         return(false);  //sai
                     }
                 }
                 a.pop();//kiểm tra xong xóa nó đi
             }
             else //nếu như stack rỗng, không hợp lệ, có dấu mở mà không có đóng
             {
                 return(false);
             }
         }
     }
     return(a.isEmpty() == true);
     //nếu như cuối cùng stack vẫn rỗng các phần tử đã lấy ra kiểm tra phù hợp hết
 }
Example #5
0
        public static double EvaluateInfix(String input)
        {
            var expr = "(" + input + ")";
            var ops  = new GenericStack <String>();
            var vals = new GenericStack <Double>();

            for (int i = 0; i < expr.Length; i++)
            {
                var s = expr.Substring(i, 1);
                if (s.Equals("("))
                {
                }
                else if (s.Equals("+"))
                {
                    ops.push(s);
                }
                else if (s.Equals("-"))
                {
                    ops.push(s);
                }
                else if (s.Equals("*"))
                {
                    ops.push(s);
                }
                else if (s.Equals("/"))
                {
                    ops.push(s);
                }
                else if (s.Equals("sqrt"))
                {
                    ops.push(s);
                }
                else if (s.Equals(")"))
                {
                    var count = ops.Size;
                    while (count > 0)
                    {
                        var op = ops.pop();
                        var v  = vals.pop();
                        if (op.Equals("+"))
                        {
                            v = vals.pop() + v;
                        }
                        else if (op.Equals("-"))
                        {
                            v = vals.pop() - v;
                        }
                        else if (op.Equals("*"))
                        {
                            v = vals.pop() * v;
                        }
                        else if (op.Equals("/"))
                        {
                            v = vals.pop() / v;
                        }
                        else if (op.Equals("sqrt"))
                        {
                            v = Math.Sqrt(v);
                        }
                        vals.push(v);

                        count--;
                    }
                }
                else
                {
                    vals.push(Double.Parse(s));
                }
            }
            return(vals.pop());
        }