vojta249

Maturita_24 - zasobnicek

May 5th, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Zasobnik
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Zasobnik zasobnik1 = new Zasobnik(10);
  10.             Console.WriteLine("Je prázdný aktuálně?");
  11.             Console.WriteLine(zasobnik1.isEmpty());
  12.             zasobnik1.Push(5);
  13.             zasobnik1.Push(6);
  14.             zasobnik1.Push(85);
  15.             Console.WriteLine("Co je nahoře?");
  16.             zasobnik1.Peek();
  17.             zasobnik1.Push(52);
  18.             Console.WriteLine("Co je nahoře?");
  19.             zasobnik1.Peek();
  20.             zasobnik1.Pop();
  21.             zasobnik1.Push(25);
  22.             Console.WriteLine("Co je nahoře?");
  23.             zasobnik1.Peek();
  24.             zasobnik1.Push(26);
  25.             zasobnik1.Pop();
  26.             Console.WriteLine("Je prázdný aktuálně?");
  27.             Console.WriteLine(zasobnik1.isEmpty());
  28.         }
  29.     }
  30. }  
  31.  
  32.  
  33.  
  34.  
  35. using System;
  36. using System.Collections.Generic;
  37. using System.Text;
  38.  
  39. namespace Zasobnik
  40. {
  41.     class Zasobnik
  42.     {
  43.         private int[] pole = new int[0];
  44.         private bool prazdne;
  45.         private int index = 0;
  46.  
  47.         public Zasobnik(int n)
  48.         {
  49.             pole = new int[n];
  50.         }
  51.  
  52.         public bool isEmpty()
  53.         {
  54.             prazdne = true;
  55.             for (int i = 0; i < pole.Length; i++)
  56.             {
  57.                 if (pole[i] != 0)
  58.                     prazdne = false;
  59.             }
  60.             return prazdne;
  61.         }
  62.  
  63.         public void Push(int prvek)
  64.         {
  65.             {
  66.                 pole[index] = prvek;
  67.                 index++;
  68.             }
  69.         }
  70.  
  71.         public void Peek()
  72.         {
  73.             Console.WriteLine(pole[index - 1]);
  74.         }
  75.  
  76.         public void Pop()
  77.         {
  78.             pole[index - 1] = 0;
  79.         }
  80.         public void vypisPole()
  81.         {
  82.             for (int i = 0; i < pole.Length; i++)
  83.                 Console.Write(pole[i] + " ");
  84.             Console.WriteLine();
  85.         }
  86.         public void nefunguje()
  87.         {
  88.             Console.WriteLine(pole[0]);
  89.         }
  90.     }
  91. }
Add Comment
Please, Sign In to add comment