Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace zadanie11
- {
- class FIFO
- {
- public LinkedList<int> lista;
- private int zakres;
- private int rozmiar;
- public FIFO(int N, int rozmiar1)
- {
- lista = new LinkedList<int>();
- this.zakres = N;
- this.rozmiar = rozmiar1;
- }
- public void push(int x)
- {
- if (lista.Count == this.rozmiar)
- throw new PelnyWyjatek();//wyjątek gdy lista jest pełna
- if ((x >= 0) && (x <= this.zakres))
- throw new LiczbaWyjatek(x);//wyjatek nieodpowiednia liczba
- lista.AddLast(x);
- }
- public void pop()
- {
- if (lista.Count == 0)
- throw new PustyWyjatek();
- lista.RemoveFirst();
- }
- public void empty()
- {
- if (lista.Count == 0) throw new PustyWyjatek();
- }
- public int size()
- {
- return lista.Count;
- }
- public void full()
- {
- if (lista.Count == this.rozmiar) throw new PelnyWyjatek();
- }
- public void wypisz()
- {
- foreach(var temp in lista)
- {
- Console.Write(temp);
- Console.Write(" ");
- }
- }
- }
- class PustyWyjatek : Exception
- {
- public PustyWyjatek() { }
- }
- class PelnyWyjatek : Exception
- {
- public PelnyWyjatek() { }
- }
- class LiczbaWyjatek : Exception
- {
- private int zlaLiczba;
- public LiczbaWyjatek(int x)
- {
- this.zlaLiczba = x;
- }
- public int zwrocWyjatek()
- {
- return this.zlaLiczba;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- FIFO kolejka;
- Console.WriteLine("MENU:");
- Console.WriteLine("1.Stworz kolejke.");
- Console.WriteLine("2.Push - dodaj element na koncu kolejki.");
- Console.WriteLine("3.Pop - usun element z poczatku kolejki.");
- Console.WriteLine("4.Empty - sprawdz, czy kolejka jest pusta.");
- Console.WriteLine("5.Size - sprawdz rozmiar kolejki.");
- Console.WriteLine("6.Full - sprawdz, czy kolejka jest pelna.");
- Console.WriteLine("7.Wypisz kolejke.");
- Console.WriteLine("8.Zakoncz program.");
- bool wynik = true;
- bool dopoki = true;
- int N;
- int rozmiar1;
- while(wynik)
- {
- switch(Convert.ToInt32(Console.ReadLine()))
- {
- case 1:
- Console.WriteLine("Podaj gorny zakres liczb w kolejce.");
- N = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Podaj rozmiar kolejki.");
- rozmiar1 = Convert.ToInt32(Console.ReadLine());
- kolejka = new FIFO(N, rozmiar1);
- break;
- case 2:
- while (dopoki)
- {
- try
- {
- kolejka.push(Convert.ToInt32(Console.ReadLine()));
- dopoki = false;
- }
- catch (PelnyWyjatek e)
- {
- Console.WriteLine("Kolejka jest pelna.");
- Console.WriteLine(e.StackTrace);
- }
- catch (LiczbaWyjatek e)
- {
- Console.Write("Nieodpowiednia liczba: ");
- Console.WriteLine(e.zwrocWyjatek());
- }
- }
- break;
- case 3:
- try
- {
- kolejka.pop();
- }
- catch (PustyWyjatek e)
- {
- Console.WriteLine(e.StackTrace);
- Console.ReadKey();
- wynik = false;
- }
- break;
- case 4:
- try
- {
- Console.WriteLine("Kolejka nie jest pusta.");
- }
- catch (PustyWyjatek e)
- {
- Console.WriteLine("Kolejka jest pusta.");
- }
- break;
- case 5:
- Console.WriteLine(kolejka.size());
- break;
- case 6:
- try
- {
- Console.WriteLine("Kolejka nie jest pelna.");
- }
- catch (PelnyWyjatek e)
- {
- Console.WriteLine("Kolejka jest pelna.");
- }
- break;
- case 7:
- kolejka.wypisz();
- break;
- case 8:
- wynik = false;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement