Advertisement
Guest User

Untitled

a guest
May 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 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 zadanie11
  8. {
  9. class FIFO
  10. {
  11. public LinkedList<int> lista;
  12. private int zakres;
  13. private int rozmiar;
  14. public FIFO(int N, int rozmiar1)
  15. {
  16. lista = new LinkedList<int>();
  17. this.zakres = N;
  18. this.rozmiar = rozmiar1;
  19. }
  20. public void push(int x)
  21. {
  22. if (lista.Count == this.rozmiar)
  23. throw new PelnyWyjatek();//wyjątek gdy lista jest pełna
  24. if ((x >= 0) && (x <= this.zakres))
  25. throw new LiczbaWyjatek(x);//wyjatek nieodpowiednia liczba
  26. lista.AddLast(x);
  27. }
  28. public void pop()
  29. {
  30. if (lista.Count == 0)
  31. throw new PustyWyjatek();
  32. lista.RemoveFirst();
  33. }
  34. public void empty()
  35. {
  36. if (lista.Count == 0) throw new PustyWyjatek();
  37. }
  38. public int size()
  39. {
  40. return lista.Count;
  41. }
  42. public void full()
  43. {
  44. if (lista.Count == this.rozmiar) throw new PelnyWyjatek();
  45. }
  46. public void wypisz()
  47. {
  48. foreach(var temp in lista)
  49. {
  50. Console.Write(temp);
  51. Console.Write(" ");
  52. }
  53. }
  54. }
  55. class PustyWyjatek : Exception
  56. {
  57. public PustyWyjatek() { }
  58. }
  59. class PelnyWyjatek : Exception
  60. {
  61. public PelnyWyjatek() { }
  62. }
  63. class LiczbaWyjatek : Exception
  64. {
  65. private int zlaLiczba;
  66. public LiczbaWyjatek(int x)
  67. {
  68. this.zlaLiczba = x;
  69. }
  70. public int zwrocWyjatek()
  71. {
  72. return this.zlaLiczba;
  73. }
  74. }
  75. class Program
  76. {
  77. static void Main(string[] args)
  78. {
  79. FIFO kolejka;
  80. Console.WriteLine("MENU:");
  81. Console.WriteLine("1.Stworz kolejke.");
  82. Console.WriteLine("2.Push - dodaj element na koncu kolejki.");
  83. Console.WriteLine("3.Pop - usun element z poczatku kolejki.");
  84. Console.WriteLine("4.Empty - sprawdz, czy kolejka jest pusta.");
  85. Console.WriteLine("5.Size - sprawdz rozmiar kolejki.");
  86. Console.WriteLine("6.Full - sprawdz, czy kolejka jest pelna.");
  87. Console.WriteLine("7.Wypisz kolejke.");
  88. Console.WriteLine("8.Zakoncz program.");
  89. bool wynik = true;
  90. bool dopoki = true;
  91. int N;
  92. int rozmiar1;
  93. while(wynik)
  94. {
  95. switch(Convert.ToInt32(Console.ReadLine()))
  96. {
  97. case 1:
  98. Console.WriteLine("Podaj gorny zakres liczb w kolejce.");
  99. N = Convert.ToInt32(Console.ReadLine());
  100. Console.WriteLine("Podaj rozmiar kolejki.");
  101. rozmiar1 = Convert.ToInt32(Console.ReadLine());
  102. kolejka = new FIFO(N, rozmiar1);
  103. break;
  104. case 2:
  105. while (dopoki)
  106. {
  107. try
  108. {
  109. kolejka.push(Convert.ToInt32(Console.ReadLine()));
  110. dopoki = false;
  111. }
  112. catch (PelnyWyjatek e)
  113. {
  114. Console.WriteLine("Kolejka jest pelna.");
  115. Console.WriteLine(e.StackTrace);
  116. }
  117. catch (LiczbaWyjatek e)
  118. {
  119. Console.Write("Nieodpowiednia liczba: ");
  120. Console.WriteLine(e.zwrocWyjatek());
  121. }
  122. }
  123. break;
  124. case 3:
  125. try
  126. {
  127. kolejka.pop();
  128. }
  129. catch (PustyWyjatek e)
  130. {
  131. Console.WriteLine(e.StackTrace);
  132. Console.ReadKey();
  133. wynik = false;
  134. }
  135. break;
  136. case 4:
  137. try
  138. {
  139. Console.WriteLine("Kolejka nie jest pusta.");
  140. }
  141. catch (PustyWyjatek e)
  142. {
  143. Console.WriteLine("Kolejka jest pusta.");
  144. }
  145. break;
  146. case 5:
  147. Console.WriteLine(kolejka.size());
  148. break;
  149. case 6:
  150. try
  151. {
  152. Console.WriteLine("Kolejka nie jest pelna.");
  153. }
  154. catch (PelnyWyjatek e)
  155. {
  156. Console.WriteLine("Kolejka jest pelna.");
  157. }
  158. break;
  159. case 7:
  160. kolejka.wypisz();
  161. break;
  162. case 8:
  163. wynik = false;
  164. break;
  165. }
  166.  
  167. }
  168. }
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement