Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            StackInheritance stack    = new StackInheritance();
            Double           anDouble = 34567;

            // use method Push to add items to stack
            stack.Push(12);
            stack.Display();
            stack.Push(25);
            stack.Display();
            stack.Push(anDouble);
            stack.Display();
            stack.Push(35.25);
            stack.Display();
            Console.WriteLine("Top one is " + stack.Peek());


            // remove items from stack
            try
            {
                while (true)
                {
                    Double removedObject = stack.Pop();
                    Console.WriteLine($"{removedObject} popped");
                    stack.Display();
                }
            }
            catch (EmptyListException emptyListException)
            {
                // if exception occurs, write stack trace
                //Console.Error.WriteLine(emptyListException.StackTrace);
                Console.Error.WriteLine(emptyListException.Message);
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            StackInheritance stack = new StackInheritance();

            for (int i = 0; i < 5; ++i)
            {
                Console.Write("Enter a value: ");
                var userInput = Console.ReadLine();
                stack.Push(userInput);
                stack.Display();
            }

            try
            {
                while (true)
                {
                    object removedObject = stack.Pop();
                    Console.WriteLine(removedObject + " popped");
                    stack.Display();
                }
            }
            catch (EmptyListException emptyListException)
            {
                Console.Error.WriteLine(emptyListException);
            }

            // hold
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            var stack = new StackInheritance();

            // create data to store in List
            // create data to store in List
            double[] numbers = { 1, 2, 3 };



            for (int i = 0; i < numbers.Length; i++)
            {
                stack.Push(numbers[i]);
            }

            Console.WriteLine("Get the stack using Push() method----");
            stack.Display();
            Console.WriteLine();

            Console.WriteLine("Get the first item of stack using Peek() method---");
            Console.WriteLine("The first item is " + stack.Peek());
            Console.WriteLine();

            Console.WriteLine("After using the Pop() method----");
            stack.Pop();
            stack.Display();
        }
Ejemplo n.º 4
0
        private static void Main()
        {
            var stack = new StackInheritance <dynamic>();

            // Create objects to store in the stack
            const char   character = '$';
            const int    integer   = 34567;
            const string str       = "hello";

            // Add items to the stack
            stack.Push(true);
            WriteLine(stack);
            stack.Push(character);
            WriteLine(stack);
            stack.Push(integer);
            WriteLine(stack);
            stack.Push(str);
            WriteLine(stack);

            // remove items from the stack
            try
            {
                while (true)
                {
                    var removedObject = stack.Pop();
                    WriteLine($"{removedObject} popped");
                    WriteLine(stack);
                }
            }
            catch (EmptyListException emptyListEx)
            {
                Error.WriteLine(emptyListEx.StackTrace);
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            StackInheritance stack = new StackInheritance();

            Console.WriteLine("Stacking Fibonatti's numbers divided by 10");
            stack.Push(0.1);
            stack.Push(0.1);
            stack.Push(0.2);
            stack.Push(0.3);
            stack.Push(0.5);
            stack.Push(0.8);
            stack.Push(1.3);
            stack.Push(2.1);
            Console.WriteLine("First 8 Fibonatti's numbers divided by 10");
            stack.Display();
            Console.WriteLine("Popping 4");
            stack.Pop();
            stack.Pop();
            stack.Pop();
            stack.Pop();
            stack.Display();
            Console.WriteLine("Peeking the result..." + LinkedListLibrary.MyToString.ToString(stack.Peek()));
            Console.WriteLine("Popping 2 more...");
            stack.Pop();
            stack.Pop();
            Console.WriteLine("Showing the stack.");
            stack.Display();
        }
Ejemplo n.º 6
0
    public static void Main()
    {
        StackInheritance stack = new StackInheritance();

        stack.Push(24.8);
        stack.Display();
        stack.Push(84.64);
        stack.Display();
        stack.Push(334.442);
        stack.Display();
        stack.Push(354.1);
        stack.Display();
    }
    public static void test()
    {
        StackInheritance stack = new StackInheritance();

        stack.Push(1);
        Console.WriteLine(stack.Display());
        stack.Push(2);
        Console.WriteLine(stack.Display()); stack.Display();
        stack.Pop();
        Console.WriteLine(stack.Display());
        double peekValue = Convert.ToDouble(stack.Peek());

        Console.WriteLine("peek result: " + stack.Peek());
        Console.WriteLine(stack.Display());
        Console.ReadKey();
    }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            StackInheritance stack    = new StackInheritance();
            Double           anDouble = 34567;

            Console.WriteLine("-----Empty Stack-----");
            stack.Display();
            // use method Push to add items to stack
            Console.WriteLine("-----Adding first Item----");
            stack.Push(12);
            stack.Display();
            Console.WriteLine("-----Adding Second Item----");
            stack.Push(25);
            stack.Display();
            Console.WriteLine("-----Adding Third Item----");
            stack.Push(anDouble);
            stack.Display();
            Console.WriteLine("-----Adding Fourth Item----");
            stack.Push(35.25);
            stack.Display();
            Console.WriteLine("-----Peeking First Item-----");
            Console.WriteLine("Top item: " + stack.Peek());
            Console.WriteLine();


            // remove items from stack
            try
            {
                while (true)
                {
                    Console.WriteLine("----Removed Item-----");
                    Double removedObject = stack.Pop();
                    Console.WriteLine($"{removedObject} popped");
                    stack.Display();
                }
            }
            catch (EmptyListException emptyListException)
            {
                // if exception occurs, write stack trace
                //Console.Error.WriteLine(emptyListException.StackTrace);
                Console.Error.WriteLine(emptyListException.Message);
            }
        }
    static void Main()
    {
        StackInheritance stack = new StackInheritance();

        // create objects to store in the stack
        double a = 25.02;
        double b = 20.35;
        double c = 34.56;
        double d = 65.25;

        //string aString = "hello";

        // use method Push to add items to stack
        stack.Push(a);
        stack.Display();
        stack.Push(b);
        stack.Display();
        stack.Push(c);
        stack.Display();
        stack.Push(d);
        stack.Display();
        var f = stack.peek();

        Console.WriteLine("\nTop element of the stack iS  " + f + "\n");


        // remove items from stack
        try
        {
            while (true)
            {
                double removedObject = stack.Pop();
                Console.WriteLine($"{removedObject} popped");
                stack.Display();
            }
        }
        catch (EmptyListException emptyListException)
        {
            // if exception occurs, write stack trace
            Console.Error.WriteLine(emptyListException.StackTrace);
        }
    }
    static void Main()
    {
        StackInheritance stack = new StackInheritance();

        // create objects to store in the stack
        bool   aBoolean   = true;
        char   aCharacter = '$';
        int    anInteger  = 34567;
        string aString    = "hello";

        // use method Push to add items to stack
        stack.Push(aBoolean);
        stack.Display();
        stack.Push(aCharacter);
        stack.Display();
        stack.Push(anInteger);
        //stack.Peek();
        stack.Display();
        stack.Push(aString);
        stack.Display();

        // remove items from stack
        try
        {
            while (true)
            {
                object removedObject = stack.Pop();
                Console.WriteLine($"{removedObject} popped");
                stack.Display();
            }
        }
        catch (EmptyListException emptyListException)
        {
            // if exception occurs, write stack trace
            Console.Error.WriteLine(emptyListException.StackTrace);
        }
    }
Ejemplo n.º 11
0
    static void Main()
    {
        StackInheritance stack = new StackInheritance();

        double aDouble_1 = .4;
        double aDouble_2 = 0.3;
        double aDouble_3 = 03.4;
        double aDouble_4 = 20.4;

        stack.Push(aDouble_1);
        stack.Push(aDouble_2);
        stack.Display();
        stack.Push(aDouble_3);
        stack.Display();
        stack.Push(aDouble_4);
        stack.Display();

        while (true)
        {
            Console.WriteLine("\nStack - Test");
            Console.WriteLine("--------------");
            Console.WriteLine("1. Push");
            Console.WriteLine("2. Peek");
            Console.WriteLine("3. Pop");
            Console.WriteLine("4. EXIT");

            int option = int.Parse(Console.ReadLine());

            switch (option)
            {
            case 1:
                Console.Write("Enter an Element : ");
                try
                {
                    string numberToPush = Console.ReadLine();
                    double number_1     = double.Parse(numberToPush);
                    stack.Push(number_1);
                }
                catch (EmptyListException emptyListException)
                {
                    Console.Error.WriteLine(emptyListException.StackTrace);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0} Exception caught.", ex);
                }
                stack.Display();
                break;

            case 2:
                Console.WriteLine("Enter an Element to Peek : ");
                try
                {
                    //string numberToPeek = Console.ReadLine();
                    //double number_2 = double.Parse(numberToPeek);
                    stack.Peek();
                }
                catch (EmptyListException emptyListException)
                {
                    Console.Error.WriteLine(emptyListException.StackTrace);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0} Exception caught.", ex);
                }
                stack.Display();
                break;

            case 3:
                Console.WriteLine("Test Pop : ");
                try
                {
                    object element = stack.Pop();
                    Console.WriteLine($"{element} popped");
                }
                catch (EmptyListException emptyListException)
                {
                    Console.Error.WriteLine(emptyListException.StackTrace);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0} Exception caught.", ex);
                }
                stack.Display();
                break;

            case 4:
                Environment.Exit(0);
                break;

            default:
                throw new ArgumentException("Value not supported :" + option);
            }
        }
    }
        // Method: ConvertToPostfix: converts the infix expression to postfix notation
        public string ConvertToPostfix(string UserInput)
        {
            StackInheritance stack = new StackInheritance();
            stack.Push('(');
            infix.Append(')');
            infix.Insert(0,UserInput,1);
            while(!(stack.IsEmpty()))
            {
                for(int i = 0; i < infix.Length; i++)
                {

                    if ((infix[i] == '0') | (infix[i] == '1') | (infix[i] == '2') | (infix[i] == '3') | (infix[i] == '4') | (infix[i] == '5') | (infix[i] == '6') | (infix[i] == '7') | (infix[i] == '8') | (infix[i] == '9'))

                    {
                        postfix.Append(infix[i]);

                    }

                    else if (infix[i] == '(')
                    {
                        stack.Push(infix[i]);

                    }
                    else if (stack.IsOperator(infix[i])) // checks if infix is operator, not to be confused with the stack being checked.
                    {
                        // While there is an operator at the top of the stack
                        while (stack.IsOperator((char)stack.Peek()))
                        {
                            // if the operator at the top of the stack is equal or greater precedence
                            // then pop the operator at the top of the stack
                            if (stack.Precedence(infix[i], (char)stack.Peek()))
                            {
                                postfix.Append(stack.Pop());

                            }

                            // else append the operator from infix to postfix
                            //else
                            {

                            }
                        }
                        stack.Push(infix[i]);
                    }
                    else if(infix[i] == ')')
                    {
                        while (stack.IsOperator((char)stack.Peek()))
                        {
                            postfix.Append(stack.Pop());
                        }
                        Console.WriteLine("The character you are about to Pop is:{0}", stack.Peek());

                        char parenthesisCheck = (char)stack.Peek();
                        if (parenthesisCheck == '(')
                        {
                            stack.Pop();

                        }
                        else
                        {
                            Console.WriteLine("For some reason you have not reached the final leftparenthesis");
                        }
                    }
                }
            }

            return postfix.ToString();
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            //ENVIRONMENT SETUP++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //create stack container
            StackInheritance stack = new StackInheritance();

            //create data values to be added to the stack
            double num1 = 23.42;
            double num2 = -923.11;
            double num3 = 0.0001;
            double num4 = 92323231;

            //TESTING PUSH+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            Console.WriteLine("PUSHING items onto the stack!");
            Console.WriteLine("---------------------------------------------------------------");
            try
            {
                Console.WriteLine($"PUSHING {num1} onto the stack.");
                stack.Push(num1);
                Console.WriteLine($"Currently, the top item in the stack is {stack.Peek()}");
                Console.WriteLine("Using List's Display method to see entire contents of the stack:");
                stack.Display();

                Console.WriteLine($"PUSHING {num2} onto the stack.");
                stack.Push(num2);
                Console.WriteLine($"Currently, the top item in the stack is {stack.Peek()}");
                Console.WriteLine("Using List's Display method to see entire contents of the stack:");
                stack.Display();

                Console.WriteLine($"PUSHING {num3} onto the stack.");
                stack.Push(num3);
                Console.WriteLine($"Currently, the top item in the stack is {stack.Peek()}");
                Console.WriteLine("Using List's Display method to see entire contents of the stack:");
                stack.Display();

                Console.WriteLine($"PUSHING {num4} onto the stack.");
                stack.Push(num4);
                Console.WriteLine($"Currently, the top item in the stack is {stack.Peek()}");
                Console.WriteLine("Using List's Display method to see entire contents of the stack:");
                stack.Display();
            }
            catch (EmptyListException e)
            {
                Console.WriteLine(e.Message);
            }


            //TESTING POP++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            Console.WriteLine("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
            Console.WriteLine("POPPING items from the stack!");
            Console.WriteLine("---------------------------------------------------------------");
            try
            {
                while (true)
                {
                    Console.WriteLine($"POPPING {stack.Pop()} from the stack.");
                    Console.WriteLine($"Currently, the top item in the stack is {stack.Peek()}");
                    Console.WriteLine("Using List's Display method to see entire contents of the stack:");
                    stack.Display();
                }
            }
            catch (EmptyListException e)
            {
                Console.WriteLine(e.Message);
            }

            //TESTING PEEK+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            Console.WriteLine("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
            Console.WriteLine("NOTE: Peek was used throughout this program to look at the top of the stack.");
        }