Пример #1
3
        private static void stackTest()
        {
            Stack s = new Stack();
            string popped = null;
            bool fullList = false;

            Console.Out.WriteLine("======>Stack Test 1<======");
            Console.Out.WriteLine(">>Tests whether or not the isEmpty method works with an empty stack");
            Console.Out.Write("isEmpty() returned: ");
            Console.Out.WriteLine(s.isEmpty());
            Console.Out.Write("Test Passed: ");
            Console.Out.WriteLine(s.isEmpty() == true);

            Console.Out.WriteLine("");
            Console.Out.WriteLine("======>Stack Test 2<======");
            Console.Out.WriteLine(">>Tests whether or not the push and pop methods work for an one element stack");
            s.push("element 1");
            popped = s.pop();
            Console.Out.WriteLine("pop() returned: " + popped);
            Console.Out.Write("Test Passed: ");
            Console.Out.WriteLine(String.Equals(popped, "element 1"));

            Console.Out.WriteLine("");
            Console.Out.WriteLine("======>Stack Test 3<======");
            Console.Out.WriteLine(">>Tests whether or not the multiple pushes and top methods work");
            s.push(popped);
            s.push("element 2");
            Console.Out.WriteLine("top() returned: " + s.top());
            Console.Out.Write("Test Passed: ");
            Console.Out.WriteLine(String.Equals(s.top(), "element 2"));

            Console.Out.WriteLine("");
            Console.Out.WriteLine("======>Stack Test 4<======");
            Console.Out.WriteLine(">>Tests whether or not multiple pushes and pops work and isEmpty works for a stack with items in it" +
                "and a stack with all of its items removed");
            s.push("element 3");
            fullList = s.isEmpty();
            Console.Out.Write("isEmpty() (on stack with 3 items in it) returned: ");
            Console.Out.WriteLine(fullList);
            s.pop();
            s.pop();
            s.pop();
            Console.Out.Write("isEmpty() (on stack with all items removed) returned: ");
            Console.Out.WriteLine(s.isEmpty());
            Console.Out.Write("Test Passed: ");
            Console.Out.WriteLine(s.isEmpty() == true && fullList == false);
        }
Пример #2
0
 public void Clear()
 {
     Stack<int> stack = new Stack<int>();
     stack.Push(10);
     stack.Clear();
     Assert.IsTrue(stack.Size() == 0);
 }
Пример #3
0
        static void TraverseTree(string root)
        {
            Stack<string> dirs = new Stack<string>(20);
            if (!System.IO.Directory.Exists(root))
            {
                throw new ArgumentException();
            }
            dirs.Push(root);
            while (dirs.Count > 0)
            {
                string currentDir = dirs.Pop();
                string[] subDirs;
                try
                {
                    subDirs = System.IO.Directory.GetDirectories(currentDir);
                   
                }
                catch (UnauthorizedAccessException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                catch (System.IO.DirectoryNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                string[] files = null;
                try
                {
                    files = System.IO.Directory.GetFiles(currentDir);
                }
                catch (UnauthorizedAccessException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                catch (System.IO.DirectoryNotFoundException e)
                {
                    Console.WriteLine(e.Message);
                    continue;
                }
                foreach (string file in files)
                {
                    try
                    {
                        System.IO.FileInfo fi = new System.IO.FileInfo(file);
                        Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);

                    }
                    catch(System.IO.FileNotFoundException e)
                    {
                        Console.WriteLine(e.Message);
                        continue;
                    }
                }
                foreach (string str in subDirs)
                    dirs.Push(str);
            }
        }
Пример #4
0
        public static void Main()
        {
            try
            {
                Stack<int> stack = new Stack<int>();
                stack.Push(1);
                stack.Push(2);
                stack.Push(3);
                stack.Push(4);
                stack.Push(5);

                int[] array = stack.ToArray();
                Console.WriteLine(string.Join(", ", array));

                Console.WriteLine(stack.Peek());
                Console.WriteLine(stack.Pop());
                Console.WriteLine(stack.Contains(0));
                stack.Clear();
                Console.WriteLine(stack.Peek());
            }
            catch (ArgumentNullException aex)
            {
                Console.WriteLine(aex.TargetSite + " -> " + aex.Message);
            }
        }
Пример #5
0
        static void Main(string[] args)
        {
            // Variables
            Stack.Stack pila = new Stack.Stack();
            string      exp  = "5+6*7-8*9";
            string      res  = "";

            // Recorremos caracter por caracter
            foreach (char item in exp)
            {
                if (char.IsDigit(item))
                {
                    res += item;
                }
                else
                {
                    while (pila.Cantidad() > 0 && Precedencia(pila.Peek(), item))
                    {
                        res += pila.Pop();
                    }
                    pila.Push(item);
                }
            }
            int restantes = pila.Cantidad();

            if (restantes > 0)
            {
                while (restantes != 0)
                {
                    res += pila.Pop();
                    restantes--;
                }
            }
            Console.WriteLine(res);
        }
Пример #6
0
 public void PopGoodTest()
 {
     Stack<int> stack = new Stack<int>();
     stack.Push(10);
     stack.Push(9);
     stack.Pop();
     Assert.IsTrue(stack.Top() == 10);
 }
Пример #7
0
 public void Write()
 {
     Stack<int> stack = new Stack<int>();
     stack.Push(10);
     stack.Push(0);
     stack.Push(100);
     string test = "100 0 10 ";
     Assert.IsTrue(stack.Write() == test);
 }
 public static void Main(string[] args)
 {
     var stack = new Stack();
     stack.Push(1);
     stack.Push(2);
     stack.Push(3);
     Console.WriteLine(stack.Pop());
     Console.WriteLine(stack.Pop());
     Console.WriteLine(stack.Pop());
     //output should be 3,2,1
 }
Пример #9
0
 static void Main(string[] args)
 {
     Stack<int> stack = new Stack<int>();
     stack.Push(1);
     stack.Push(2);
     stack.Push(2);
     //stack.Pop();
     Console.WriteLine(stack.Write());
     //Console.WriteLine(stack.Top());
     Console.WriteLine("{0}, " ,stack.Size());
 }
Пример #10
0
        static void Main()
        {
            Stack<string> st = new Stack<string>();
            st.Push("one");
            st.Push("two");
            st.Push("three");

            foreach (string s in st)
            {
                string str = st.Pop();
                Console.WriteLine(str);
            }
        }
Пример #11
0
        static void Main(string[] args)
        {
            Stack<int> test = new Stack<int>();
            test.Push(1);
            test.Push(2);
            test.Push(3);
            test.Push(4);
            test.Push(5);
            test.Push(6);
            test.Push(7);
            Console.WriteLine(test.Contains(2));

        }
Пример #12
0
        public static void Main()
        {
            Stack<int> stack = new Stack<int>();
            for (int i = 0; i < 10; i++)
            {
                stack.Push(i + 1);
            }

            while (stack.Count > 0)
            {
                Console.WriteLine(stack.Pop());
            }
        }
Пример #13
0
        static void Main(string[] args)
        {
            var stack = new Stack();
            stack.Push("hello");
            stack.Push("pussy");
            stack.Push("cat");

               // stack.Clear();

            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
            Console.WriteLine(stack.Pop());
        }
Пример #14
0
        static void Main()
        {
            Stack<int> test = new Stack<int>();

            for (int i = 1; i <= 20; i++)
            {
                test.Push(i);
            }

            for (int i = 0; i < 20; i++)
            {
                Console.WriteLine("{0} -- Count: {1}", test.Pop(), test.Count());
            }
        }
Пример #15
0
        static void DirectoryTree(string path)
        {
            Stack<string> st = new Stack<string>();
            Console.WriteLine("In folder with path: " + path  + " there are " + Directory.GetFiles(path).Length + "files");
            st.Push(path);

            while (st.Count > 0)
            {
                string[] subfolders = Directory.GetDirectories(st.Pop());
                foreach (string s in subfolders)
                {
                    Console.WriteLine("In Folder with path :" + s + " there is" + Directory.GetFiles(s).Length + " file");
                    st.Push(s);
                    Console.ReadKey();
                }
            }
        }
Пример #16
0
        static void Main(string[] args)
        {
            Stack stack = new Stack(5);

            stack.Push("test");

            stack.Push(100);
            stack.Push(200);

            stack.Push(3.6);

            Console.WriteLine("Values in the Stack are: " +
                (double)stack.Pop() + ", " +
                (int)stack.Pop() + ", " +
                (int)stack.Pop() + ", " +
                (string)stack.Pop());
            Console.ReadLine();
        }
Пример #17
0
        public static void Main()
        {
            Stack<int> myTestStack = new Stack<int>();
            myTestStack.Push(11);
            myTestStack.Push(22);
            myTestStack.Push(33);
            myTestStack.Push(44);
            myTestStack.Push(55);

            Console.WriteLine("Peek test: {0}", myTestStack.Peek());
            Console.WriteLine("Peek test: {0}", myTestStack.Peek());
            Console.WriteLine("Peek test: {0}", myTestStack.Peek());
            Console.WriteLine("Pop test: {0}", myTestStack.Pop());
            Console.WriteLine("Pop test: {0}", myTestStack.Pop());
            Console.WriteLine("Pop test: {0}", myTestStack.Pop());
            Console.WriteLine("Pop test: {0}", myTestStack.Pop());
            Console.WriteLine("Pop test: {0}", myTestStack.Pop());
        }
Пример #18
0
        public static void Main()
        {
            Stack<int> stack = new Stack<int>();

            stack.Push(1, 2, 3, 4, 5, 4, 3);

            while (stack.Length > 0)
            {
                if (stack.Length > 1)
                {
                    Console.Write("{0}, ", stack.Pop());
                }
                else
                {
                    Console.WriteLine(stack.Pop());
                }
            }

            Console.WriteLine();
        }
Пример #19
0
        static void LookIn(string path)
        {
            Stack<string> drs = new Stack<string>();
            drs.Push(path);
            while (drs.Count > 0)
            {
                DirectoryInfo current = new DirectoryInfo(drs.Pop());
                DirectoryInfo[] subdrs = current.GetDirectories();
                FileInfo[] files = current.GetFiles();
                Console.WriteLine(current);
                foreach (FileInfo file in files)
                {
                    Console.WriteLine(file);
                }
                foreach (DirectoryInfo s in subdrs)
                {
                    drs.Push(s.FullName);
                }
            }

        }
        internal static void Main()
        {
            Stack<int> stack = new Stack<int>();

            // Четат се цели числа от клавиатурата до прочитане на 0 и се включват в стека
            int number = int.Parse(Console.ReadLine());

            while (number != 0)
            {
                stack.Push(number);
                number = int.Parse(Console.ReadLine());
            }

            // Изключват се последователно всички елементи от стека и се печатат. Това ще
            // доведе до отпечатване на първоначално въведената последователност в обратен ред
            while (!stack.IsEmpty())
            {
                int numberOnTop = stack.Pop();
                Console.WriteLine(numberOnTop);
            }
        }
Пример #21
0
 public void init()
 {
     stack = new Stack<int>();
 }
Пример #22
0
 public TwoStackAlgo()
 {
     values = new Stack<int>();
     operators = new Stack<string>();
 }
Пример #23
0
 public void PopBadTest()
 {
     Stack<int> stack = new Stack<int>();
     stack.Pop();
 }
Пример #24
0
 public void PushTest()
 {
     Stack<int> stack = new Stack<int>();
     stack.Push(1);
     Assert.IsTrue(stack.Size() == 1);
 }
Пример #25
0
        static void Main(string[] args)
        {
            String[] resultado;
            // Evaluación de Postfix
            Stack.Stack pila = new Stack.Stack();
            int         n    = 0;
            int         a    = 0;
            int         b    = 0;
            // -+3*52*73
            // 352*+73*-
            String expresion = "33 5 2 * + 7 3 * - 100 *";

            // Para Postfix primero b luego a en pila.Pop();
            // Para PreFix primero a luego b en pila.Pop();
            resultado = expresion.Split(' ');
            try
            {
                foreach (string item in resultado)
                {
                    if (item == "*" || item == "/" || item == "+" || item == "-")
                    {
                        b = pila.Pop();
                        a = pila.Pop();
                        switch (item)
                        {
                        case "+":
                            n = a + b;
                            break;

                        case "-":
                            n = a - b;
                            break;

                        case "*":
                            n = a * b;
                            break;

                        case "/":
                            n = a / b;
                            break;

                        default:
                            break;
                        }
                        pila.Push(Convert.ToInt32(n));
                    }
                    else if (Convert.ToInt32(item) >= 0)
                    {
                        pila.Push(Convert.ToInt32((item)));
                    }
                }
                if (pila.Cantidad() == 1)
                {
                    Console.Write("Resultado de la Expresión {0} es = ", expresion);
                    pila.Transversa();
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("Hay un error con la expresión, verifica que cumpla la norma PostFix\nQue contenga espacios entre operadores.\n");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error " + e.Message);
            }
        }
Пример #26
0
        public static void Main(string[] args)
        {
            IStack<int> stack = new Stack<int>(10);

            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("Add element " + i);
                stack.addElement(i);
                Console.WriteLine("New length is: " + stack.Length);

            }

            for (int i = 1; i <= 5; i++)
            {
                Console.WriteLine("View last added element " + stack.viewElement());
                Console.WriteLine("Remove last added element " + stack.removeElement());
                Console.WriteLine("New length is " + stack.Length);

            }

            try
            {
                stack.viewElement();
            }
            catch (InvalidOperationException exc)
            {
                Console.WriteLine("Error " + exc.Message);
            }

            try
            {
                stack.removeElement();
            }
            catch (InvalidOperationException exc)
            {
                Console.WriteLine("Error " + exc.Message);
            }

            // test clear stack
            stack.clearStack();
            Console.WriteLine("Stack: " + stack.Length);
            var item = stack.printAllElements();
            foreach (var i in item)
            {
                Console.WriteLine(i);
            }

            stack.addElement(12);
            Console.WriteLine(stack.Length);
            Console.WriteLine(stack.viewElement());

            for (int i = 1; i <= 50; i++)
            {
                Console.WriteLine("Add element " + i);
                stack.addElement(i);
                Console.WriteLine("New length is: " + stack.Length);

            }

            Console.WriteLine("Press any key to continue ...");
            Console.ReadLine();
        }
Пример #27
0
 public void SizeTest()
 {
     Stack<int> stack = new Stack<int>();
     Assert.IsTrue(stack.Size() == 0);
 }
Пример #28
0
 public void Top()
 {
     Stack<int> stack = new Stack<int>();
     stack.Push(10);
     Assert.IsTrue(stack.Top() == 10);
 }
Пример #29
0
 public CalculateExpression()
 {
     initStack = new Stack<string>();
 }
Пример #30
0
 public void Initialize()
 {
     stack = new Stack<int>();
 }