Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Stosik
  8. {
  9.     class Stosik
  10.     {
  11.         private int _rozmiar = 0;
  12.         private List<int> _outLittleStack;
  13.  
  14.         //konstruktor prywatny zeby korzystać z Init
  15.         private Stosik(int rozmiar)
  16.         {
  17.             _rozmiar = rozmiar;
  18.             _outLittleStack = new List<int>(rozmiar);
  19.         }
  20.  
  21.         //metoda statyczna tworzy nowy obiekt i zwraca go
  22.         public static Stosik Init(int rozmiar)
  23.         {
  24.             return new Stosik(rozmiar);
  25.         }
  26.  
  27.         public Stosik Clone()
  28.         {
  29.             //tworzymy nowy obiekt stosu
  30.             Stosik stosik = Stosik.Init(_rozmiar);
  31.             //przepisujemy wartości
  32.             foreach (var number in _outLittleStack)
  33.             {
  34.                 stosik._outLittleStack.Add(number);
  35.             }
  36.             //zwracamy sklonowany stosik
  37.             return stosik;
  38.         }
  39.  
  40.         public void Destroy()
  41.         {
  42.             _outLittleStack.Clear();
  43.             _rozmiar = 0;
  44.         }
  45.  
  46.         public void Push(int liczba)
  47.         {
  48.             if (_rozmiar == 0)
  49.                 throw new Exception("Stos nie zainicjalizowany");
  50.             if (_outLittleStack.Count() < _rozmiar)
  51.                 _outLittleStack.Add(liczba);
  52.             else
  53.             {
  54.                 Console.WriteLine("O TY W MORDE STOSIK PELNY NIE DODASZ:///");
  55.             }
  56.  
  57.         }
  58.  
  59.         public int Pop()
  60.         {
  61.             if (_rozmiar == 0)
  62.                 throw new Exception("Stos nie zainicjalizowany");
  63.             if (_outLittleStack.Any())
  64.             {
  65.                 var number = _outLittleStack.Last();
  66.                 _outLittleStack.Remove(number);
  67.                 return number;
  68.             }
  69.             else
  70.             {
  71.                 throw new Exception("MORDO JAK STOS PUSTY!!!!");
  72.             }
  73.  
  74.  
  75.         }
  76.  
  77.         public int Top()
  78.         {
  79.             if (_rozmiar == 0)
  80.                 throw new Exception("Stos nie zainicjalizowany");
  81.             if (_outLittleStack.Any())
  82.                 return _outLittleStack.Last();
  83.             else
  84.             {
  85.                 throw new Exception("MORDO JAK STOS PUSTY!!!!");
  86.             }
  87.         }
  88.  
  89.         public bool Empty()
  90.         {
  91.             if (_rozmiar == 0)
  92.                 throw new Exception("Stos nie zainicjalizowany");
  93.             return _outLittleStack.Count == 0;
  94.         }
  95.  
  96.         public bool Full()
  97.         {
  98.             if (_rozmiar == 0)
  99.                 throw new Exception("Stos nie zainicjalizowany");
  100.             return _outLittleStack.Count == _rozmiar;
  101.         }
  102.     }
  103.     class Program
  104.     {
  105.         static void Main(string[] args)
  106.         {
  107.             //stworzenie 1 stosu
  108.             Stosik stosik1 = Stosik.Init(10);
  109.             for (int i = 0; i < 10; i++)
  110.                 stosik1.Push(i);
  111.  
  112.             //zrobienie kopii
  113.             Stosik stosik2 = stosik1.Clone();
  114.             //wypisanie stosu 2 całego
  115.             Console.WriteLine("WYpisanie sklonowanego stosu");
  116.             while(!stosik2.Empty())
  117.                 Console.WriteLine(stosik2.Pop());
  118.  
  119.             //sprawdzenie, czy stos 1 jest cały
  120.             Console.WriteLine("Sprawdzenie elementu ze stosu pierwotnego");
  121.             Console.WriteLine(stosik1.Top());
  122.  
  123.             //jak widac, wypisuje 9 wiec stosik1 jest git
  124.  
  125.             Console.ReadKey();
  126.  
  127.  
  128.  
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement