Advertisement
Fenrir112

%u041A%u043B%u0430%u0441%u0441 %u0421%u0442%u0435%u043A

Dec 5th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.61 KB | None | 0 0
  1. namespace Stack
  2. {
  3.     public class Stack
  4.     {
  5.         private int[] stack;
  6.         private int index;
  7.         public Stack (int n)
  8.         {
  9.             stack = new int[n];
  10.             index = 0;
  11.         }
  12.         public void Clear ()
  13.         {
  14.             for (int i = 0; i < stack.Length; i++)
  15.                 stack[i] = 0;
  16.             index = 0;
  17.         }
  18.         public int Pop ()
  19.         {
  20.             try {
  21.                 index--;
  22.                 return stack [index];
  23.             }
  24.             catch (IndexOutOfRangeException) {
  25.                 Clear();
  26.                 throw new index0 ("Ошибка! Попытка чтения из пустого стека");
  27.                 return 0;
  28.             }
  29.         }
  30.         public void Print ()
  31.         {
  32.         if (index == 0)
  33.             Console.WriteLine ("Стек пуст");
  34.         else {
  35.                 Console.Write ("Стек: ");
  36.                 for (int i = 0; i < index-1; i++)
  37.                     Console.Write ("{0}, ", stack[i]);
  38.                 Console.Write (stack[index-1]+";");
  39.                 Console.WriteLine();
  40.              }
  41.         }
  42.         public void Push (int elem)
  43.         {
  44.             try {
  45.                 stack[index] = elem;
  46.                 index++;
  47.             }
  48.             catch (IndexOutOfRangeException) {
  49.                 throw new overkill("Ошибка! Индекс вышел за пределы. Часть данных была потеряна. Увеличьте стек.");
  50.             }
  51.         }
  52.         public int Top()
  53.         {
  54.             return index;
  55.         }
  56.     }
  57.  
  58.     class index0 : System.Exception
  59.     {
  60.         string message;
  61.         public index0 (string _message)
  62.         {
  63.             message = _message;
  64.             Console.WriteLine(message);
  65.         }
  66.  
  67.     }
  68.  
  69.     class overkill : System.Exception
  70.     {
  71.         string message;
  72.         public overkill(string _message)
  73.         {
  74.             message = _message;
  75.             Console.WriteLine(message);
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement