Advertisement
Guest User

Untitled

a guest
May 31st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 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 FIFO
  8. {
  9.  
  10. public class FULL : Exception {
  11. public FULL() { }
  12. public FULL(string message) : base(message) { }
  13. }
  14. public class EMPTY : Exception
  15. {
  16. public EMPTY() { }
  17. public EMPTY(string message) : base(message) { }
  18. }
  19.  
  20. public class OUTOFSCOPE : Exception
  21. {
  22. public OUTOFSCOPE() { }
  23. public OUTOFSCOPE(string message) : base(message) { }
  24. }
  25. public class Kolejka
  26. {
  27. List<int> lista;
  28. int max_size;
  29. int current_size;
  30. int max_number;
  31. public Kolejka() { this.max_size = 0; this.current_size = 0; this.max_number = 0; lista = new List<int>(); }
  32. public Kolejka(int N, int _size)
  33. {
  34. this.max_number = N;
  35. this.max_size = _size;
  36. this.current_size = 0;
  37. lista = new List<int>();
  38. }
  39.  
  40. public void add(int n)
  41. {
  42. if (current_size+1 > this.max_size)
  43. {
  44. throw new FULL("Kolejka jest pełna");
  45. }
  46. if(n<0 || n > this.max_number) { throw new OUTOFSCOPE("Liczba spoza zakresu (od 0 do " + this.max_number+")"); }
  47. lista.Add(n);
  48. this.current_size++;
  49. }
  50.  
  51. public void print()
  52. {
  53. if (this.current_size == 0) { throw new EMPTY("Kolejka jest pusta"); }
  54. Console.WriteLine("\n\n=====KOLEJKA=====\n");
  55. for(int i = 0; i < lista.Count; i++)
  56. {
  57. Console.WriteLine(lista[i]);
  58. }
  59. }
  60. public void del()
  61. {
  62. if (this.current_size == 0) { throw new EMPTY("Kolejka jest pusta"); }
  63. lista.RemoveAt(0);
  64. this.current_size--;
  65. }
  66. }
  67.  
  68.  
  69.  
  70. class Program
  71. {
  72. static void Main(string[] args)
  73. {
  74. Console.Write("Podaj rozmiar kolejki: ");
  75. string bufor = Console.ReadLine();
  76. int result,res2;
  77. bool res_parse = Int32.TryParse(bufor, out result);
  78. if (!res_parse || result <= 0)
  79. {
  80. Console.WriteLine("Wprowadzono niepoprawny rozmiar.");
  81. Console.ReadKey();
  82. Environment.Exit(0);
  83. }
  84. Console.Write("Podaj górną granicę zakresu liczb (dolna granica wynosi 0): ");
  85. bufor = Console.ReadLine();
  86. res_parse = Int32.TryParse(bufor, out res2);
  87. if (!res_parse || res2 <= 0)
  88. {
  89. Console.WriteLine("Wprowadzono niepoprawną wartość.");
  90. Console.ReadKey();
  91. Environment.Exit(0);
  92. }
  93. Console.Clear();
  94.  
  95. Kolejka kol = new Kolejka(res2, result);
  96. result = -1;
  97. do
  98. {
  99. Console.WriteLine("[1] Dodaj do kolejki");
  100. Console.WriteLine("[2] Usun z kolejki");
  101. Console.WriteLine("[3] Wypisz kolejkę");
  102. Console.WriteLine("[0] Wyjdź");
  103.  
  104. Console.Write("Twój wybór: ");
  105. bufor = Console.ReadLine();
  106. res_parse = Int32.TryParse(bufor, out result);
  107. if (!res_parse) result = -1;
  108.  
  109. switch (result)
  110. {
  111. case 1:
  112. Console.Write("\nPodaj liczbę: ");
  113. string buf = Console.ReadLine();
  114. int number;
  115. bool parse = Int32.TryParse(buf, out number);
  116. if (parse)
  117. {
  118. try { kol.add(number); }
  119. catch (FULL ex) { Console.WriteLine(ex.Message); }
  120. catch (OUTOFSCOPE ex) { Console.WriteLine(ex.Message);}
  121. }
  122. else { Console.WriteLine("Nie jest to liczba"); }
  123. Console.Write("\n");
  124. break;
  125. case 2:
  126. try { kol.del(); }
  127. catch (EMPTY ex) { Console.WriteLine(ex.Message); }
  128. Console.Write("\n");
  129. break;
  130. case 3:
  131. try { kol.print(); }
  132. catch (EMPTY ex) { Console.WriteLine(ex.Message); }
  133. Console.Write("\n");
  134. break;
  135. }
  136.  
  137. } while (result != 0);
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement