Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string[] expressions = new string[]
            {
                "1+2",
                "3+4*5+6",
                "(1+2)*(3+4)",
                "(1-2/3)*(4/5-6)"
            };

            InfixToPostfix  infixToPostfix  = new InfixToPostfix();
            InfixToPrefix   infixToPrefix   = new InfixToPrefix();
            PrefixToPostfix prefixToPostfix = new PrefixToPostfix();
            PostfixToPrefix postfixToPrefix = new PostfixToPrefix();
            PrefixToInfix   prefixToInfix   = new PrefixToInfix();
            PostfixToInfix  postfixToInfix  = new PostfixToInfix();

            foreach (var expression in expressions)
            {
                Console.WriteLine(expression);

                var prefix  = infixToPrefix.Convert(expression);
                var postfix = infixToPostfix.Convert(expression);

                Console.WriteLine(prefixToInfix.Convert(prefix));
                Console.WriteLine(postfixToInfix.Convert(postfix));
                Console.WriteLine(prefix);
                Console.WriteLine(postfixToPrefix.Convert(postfix));
                Console.WriteLine(postfix);
                Console.WriteLine(prefixToPostfix.Convert(prefix));
                Console.WriteLine();
            }

            Console.ReadKey();
        }
Exemplo n.º 2
0
        public void DisplayStack()
        {
            Console.WriteLine("Select Question[1-12]");
            int choice = int.Parse(Console.ReadLine());

            switch (choice)
            {
                #region CSharp Stack Collection
            case 1:
                Console.WriteLine("--------" + choice + ".CSharp Stack Collection --------");
                StackCollectioninCsharp <int> sk1 = new StackCollectioninCsharp <int>();
                int[] arr1 = { 10, 78, 34, 65 };
                foreach (var v in arr1)
                {
                    sk1.Push(v);
                }
                sk1.Print();
                Console.WriteLine($"Count is :{sk1.myStack.Count}");
                Console.WriteLine($"78 Is Present :{sk1.myStack.Contains(78)}");
                sk1.myStack.Pop();
                sk1.Print();
                break;
                #endregion

                #region Stack using Array
            case 2:
                Console.WriteLine("--------" + choice + ".Stack using Array--------");
                StackUsingArray sk2 = new StackUsingArray(10);
                sk2.Push(10);
                sk2.Push(20);
                sk2.Push(30);
                sk2.Push(40);
                sk2.Print();
                sk2.Pop();
                sk2.Print();
                break;
                #endregion

                #region Stack Using Linked List
            case 3:
                Console.WriteLine("--------" + choice + ".Stack Using Linked List--------");
                StackUsingLinkedList <int> sk3 = new StackUsingLinkedList <int>();
                int[] arr3 = { 10, 20, 30, 40 };
                foreach (var v in arr3)
                {
                    sk3.Push(v);
                }
                sk3.Print();
                sk3.Peek();
                sk3.Print();
                sk3.Pop();
                sk3.Print();
                Console.WriteLine($"Size Of Stack: {sk3.GetSize()}");
                break;
                #endregion

                #region Balanced Expression
            case 4:
                Console.WriteLine("--------" + choice + ".Balanced Expression--------");
                BalancingSymbolPattern sk4 = new BalancingSymbolPattern();
                Console.WriteLine($"Expression is Balanced using Stack: {sk4.CheckPattern("[{(}]", "Stack")}");
                Console.WriteLine($"Expression is Balanced  using Stack Adt: {sk4.CheckPattern("[{)}]", "StackADT")}");
                Console.WriteLine($"Expression is Balanced using Stack: {sk4.CheckPattern("[{()}]", "Stack")}");
                Console.WriteLine($"Expression is Balanced  using Stack Adt: {sk4.CheckPattern("[{()}]", "StackADT")}");
                break;
                #endregion

                #region Postfix Expression Evaluation Using Stack
            case 5:
                Console.WriteLine("--------" + choice + ".Postfix Expression Evaluation  Using Stack--------");
                PostfixImplimentationUsingStack sk5 = new PostfixImplimentationUsingStack();
                Console.WriteLine($"Postfix Expression result is {sk5.EvaluatePostfixExpression("123*+5-")}");
                break;
                #endregion

                #region Infix to Postfix
            case 6:
                Console.WriteLine("--------" + choice + ".Infix to Postfix--------");
                InfixToPostFix sk6 = new InfixToPostFix();
                string         exp = "a+b*(c^d-e)^(f+g*h)-i";
                //string exp ="a*b-(c+d)-e";
                Console.WriteLine($"Infix {exp} of  Postfix is : {sk6.ConvertInfixToPostFix(exp.Replace(" ", string.Empty))}");
                break;
                #endregion

                #region Infix To Prefix
            case 7:
                Console.WriteLine("--------" + choice + ".Infix To Prefix--------");
                InfixToPrefix sk7 = new InfixToPrefix();
                //string exp7 = "a*b-(c+d)-e";
                string exp7 = "((a/b)+c)-(d+(e*f))";
                Console.WriteLine($"Infix {exp7} of Prefix is {sk7.ConvertInfixToPrefix(exp7.Replace(" ", string.Empty))}");
                break;
                #endregion

                #region Convert Prefix and Postfix to Infix
            case 8:
                Console.WriteLine("--------" + choice + ".Convert Prefix and Postfix to Infix--------");
                ConvertToInfix sk8     = new ConvertToInfix();
                string         exppre8 = "*-A/BC-/AKL";
                Console.WriteLine($"Pefix {exppre8} of Infix is {sk8.ConvertPreToInfix(exppre8.Replace(" ", string.Empty))}");
                string expost8 = " ab*c+";
                Console.WriteLine($"Postfix {expost8} of Infix is {sk8.ConvertPostToInfix(expost8.Replace(" ", string.Empty))}");
                break;
                #endregion

                #region Convert Post to Pre and Viceversa
            case 9:
                Console.WriteLine("--------" + choice + ".Convert Post to Pre and Viceversa--------");
                ConvertPreToPost_PostToPre sk9 = new ConvertPreToPost_PostToPre();
                string exp9 = "*-A/BC-/AKL";
                Console.WriteLine(sk9.ConvertExpression(exp9.Replace(" ", string.Empty)));
                break;
                #endregion

                #region Sort Stack using Recursion
            case 10:
                Console.WriteLine("--------" + choice + ".Sort Stack using Recursion--------");
                string    str10 = "-3 14 18 -5 30";
                SortStack sk10  = new SortStack(str10);
                sk10.PrintStack();
                break;
                #endregion

                #region Get Min From Stack with O(1)
            case 11:
                Console.WriteLine("--------" + choice + ". Get Min From Stack with O(1)--------");
                GetMinStack sk11 = new GetMinStack();
                sk11.Push(2);
                sk11.Push(4);
                sk11.Push(6);
                sk11.Push(1);
                sk11.Push(5);
                Console.WriteLine("Stack is 5,1,6,4,2");
                Console.WriteLine($"min value in stack is {sk11.GetMin()} and top of stack is {sk11.Top()}");
                sk11.Pop();
                sk11.Pop();
                Console.WriteLine("After 2 Pops");
                Console.WriteLine($"min value in stack is {sk11.GetMin()} and top of stack is {sk11.Top()}");
                break;
                #endregion

                #region Get MAX From Stack with O(1)
            case 12:
                Console.WriteLine("--------" + choice + ".Get MAX From Stack with O(1)--------");
                GetMaxStack sk12 = new GetMaxStack();
                sk12.Push(2);
                sk12.Push(7);
                sk12.Push(6);
                sk12.Push(8);
                sk12.Push(5);
                Console.WriteLine("Stack is 5,8,6,7,2");
                Console.WriteLine($"Max value in stack is {sk12.GetMax()} and top of stack is {sk12.GetTop()}");
                sk12.Pop();
                sk12.Pop();
                Console.WriteLine("After 2 Pops");
                Console.WriteLine($"Max value in stack is {sk12.GetMax()} and top of stack is {sk12.GetTop()}");
                break;
                #endregion

                #region
            case 13:
                Console.WriteLine("--------" + choice + ".--------");

                break;
                #endregion

                #region
            case 14:
                Console.WriteLine("--------" + choice + ".--------");

                break;
                #endregion

                #region
            case 15:
                Console.WriteLine("--------" + choice + ".--------");

                break;
                #endregion

            default:
                Console.WriteLine("Oops! Inavalid Choice.");
                break;
            }
        }