Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. //Proširi drugi zadatak iz vježbe br. 3 (predložak klase stog) tako da se podigne
  2. //iznimka StogExc izvedena iz osnovne klase exception u slučaju da se pokuša
  3. //umetnuti element na pun stog ili skinuti element sa praznog stoga.Napisati
  4. //odgovarajuću main funkciju, koja će sadržavati blokove pokušaja i hvatanja.
  5. #include <iostream>
  6. #include<cstdlib>
  7. #include<ctime>
  8. class StackExc : public std::exception{
  9. private:
  10.     const char* poruka;
  11. public:
  12.     StackExc(const char*);
  13.     virtual const char* what() const throw();
  14. };
  15. StackExc::StackExc(const char* tekst) : poruka(tekst) {}
  16. const char* StackExc::what() const throw() { return poruka; }
  17.  
  18. template <class Tip>
  19. class Stack
  20. {
  21.     int size;
  22.     int brElementa = 0;
  23.     Tip* polje;
  24. public:
  25.     Stack() :size(0)
  26.     {
  27.         polje = new Tip[0];
  28.     }
  29.     Stack(int a) :size(a)
  30.     {
  31.         polje = new Tip[a];
  32.     }
  33.     ~Stack()
  34.     {
  35.         delete[] polje;
  36.     }
  37.     Stack(const Stack& ref)
  38.     {
  39.         size = ref.size;
  40.         brElementa = ref.brElementa;
  41.         polje = new Tip[size];
  42.         for (int i = 0; i < size; i++)
  43.         {
  44.             polje[i] = ref.polje[i];
  45.         }
  46.     }
  47.     Stack& operator=(const Stack& ref)
  48.     {
  49.         if (this != &ref)
  50.         {
  51.             size = ref.size;
  52.             brElementa = ref.brElementa;
  53.             delete[]polje;
  54.             polje = new Tip[size];
  55.             for (int i = 0; i < size; i++)
  56.             {
  57.                 polje[i] = ref.polje[i];
  58.             }
  59.         }
  60.         return *this;
  61.     }
  62.     void push(Tip a)
  63.     {
  64.         if (brElementa < size)
  65.         {
  66.             polje[brElementa] = a;
  67.             brElementa++;
  68.         }
  69.         else
  70.         {
  71.             throw StackExc("Stog je pun.");
  72.         }
  73.  
  74.     }
  75.     void pop()
  76.     {
  77.         if (size == -1) { throw StackExc("Stog je prazan."); }
  78.         else
  79.         brElementa--;
  80.     }
  81.     Tip peek()
  82.     {
  83.         return polje[brElementa - 1];
  84.     }
  85. };
  86. int main()
  87. {
  88.     srand(time(NULL));
  89.     Stack<int> s1(10);
  90.     Stack<float> s2(5);
  91.     Stack<int> s3;
  92.     try {
  93.         s1.pop();
  94.         for (int i = 0; i < 7; i++) {
  95.             s1.push(rand());
  96.             std::cout << " " << std::endl;
  97.             std::cout << s1.peek() << std::endl;
  98.         }
  99.         for (int i = 0; i < 7; i++)
  100.         {
  101.             s2.push((float)rand());
  102.         }
  103.         for (int i = 0; i < 7; i++) {
  104.             s3.push((float)rand());
  105.         }
  106.     }
  107.     catch (StackExc & except) {
  108.         std::cout << except.what() << std::endl;
  109.         std::cout << "Closing program!" << std::endl;
  110.     }
  111.     catch (...) {
  112.         std::cout << "Error!\nClosing program!" << std::endl;
  113.     }
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement