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 FIFO
- {
- public class FULL : Exception {
- public FULL() { }
- public FULL(string message) : base(message) { }
- }
- public class EMPTY : Exception
- {
- public EMPTY() { }
- public EMPTY(string message) : base(message) { }
- }
- public class OUTOFSCOPE : Exception
- {
- public OUTOFSCOPE() { }
- public OUTOFSCOPE(string message) : base(message) { }
- }
- public class Kolejka
- {
- List<int> lista;
- int max_size;
- int current_size;
- int max_number;
- public Kolejka() { this.max_size = 0; this.current_size = 0; this.max_number = 0; lista = new List<int>(); }
- public Kolejka(int N, int _size)
- {
- this.max_number = N;
- this.max_size = _size;
- this.current_size = 0;
- lista = new List<int>();
- }
- public void add(int n)
- {
- if (current_size+1 > this.max_size)
- {
- throw new FULL("Kolejka jest pełna");
- }
- if(n<0 || n > this.max_number) { throw new OUTOFSCOPE("Liczba spoza zakresu (od 0 do " + this.max_number+")"); }
- lista.Add(n);
- this.current_size++;
- }
- public void print()
- {
- if (this.current_size == 0) { throw new EMPTY("Kolejka jest pusta"); }
- Console.WriteLine("\n\n=====KOLEJKA=====\n");
- for(int i = 0; i < lista.Count; i++)
- {
- Console.WriteLine(lista[i]);
- }
- }
- public void del()
- {
- if (this.current_size == 0) { throw new EMPTY("Kolejka jest pusta"); }
- lista.RemoveAt(0);
- this.current_size--;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Podaj rozmiar kolejki: ");
- string bufor = Console.ReadLine();
- int result,res2;
- bool res_parse = Int32.TryParse(bufor, out result);
- if (!res_parse || result <= 0)
- {
- Console.WriteLine("Wprowadzono niepoprawny rozmiar.");
- Console.ReadKey();
- Environment.Exit(0);
- }
- Console.Write("Podaj górną granicę zakresu liczb (dolna granica wynosi 0): ");
- bufor = Console.ReadLine();
- res_parse = Int32.TryParse(bufor, out res2);
- if (!res_parse || res2 <= 0)
- {
- Console.WriteLine("Wprowadzono niepoprawną wartość.");
- Console.ReadKey();
- Environment.Exit(0);
- }
- Console.Clear();
- Kolejka kol = new Kolejka(res2, result);
- result = -1;
- do
- {
- Console.WriteLine("[1] Dodaj do kolejki");
- Console.WriteLine("[2] Usun z kolejki");
- Console.WriteLine("[3] Wypisz kolejkę");
- Console.WriteLine("[0] Wyjdź");
- Console.Write("Twój wybór: ");
- bufor = Console.ReadLine();
- res_parse = Int32.TryParse(bufor, out result);
- if (!res_parse) result = -1;
- switch (result)
- {
- case 1:
- Console.Write("\nPodaj liczbę: ");
- string buf = Console.ReadLine();
- int number;
- bool parse = Int32.TryParse(buf, out number);
- if (parse)
- {
- try { kol.add(number); }
- catch (FULL ex) { Console.WriteLine(ex.Message); }
- catch (OUTOFSCOPE ex) { Console.WriteLine(ex.Message);}
- }
- else { Console.WriteLine("Nie jest to liczba"); }
- Console.Write("\n");
- break;
- case 2:
- try { kol.del(); }
- catch (EMPTY ex) { Console.WriteLine(ex.Message); }
- Console.Write("\n");
- break;
- case 3:
- try { kol.print(); }
- catch (EMPTY ex) { Console.WriteLine(ex.Message); }
- Console.Write("\n");
- break;
- }
- } while (result != 0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement