Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- class stos
- {
- private:
- int pojemnosc;
- int ile;
- std::string* tablica;
- public:
- stos()
- {
- this->pojemnosc = 1;
- tablica = new std::string[this->pojemnosc];
- ile = 0;
- }
- stos(int pojemnosc)
- {
- if(pojemnosc < 1 || pojemnosc > 100)
- throw std::invalid_argument("pojemnosc musi byc dododatnia i nie wieksza od 100");
- this->pojemnosc = pojemnosc;
- tablica = new std::string[this->pojemnosc];
- ile = 0;
- }
- stos(const std::initializer_list<std::string>& napisy)
- {
- if(napisy.size() == 0)
- throw std::invalid_argument("lista inicjalizacyjna musi byc niepusta");
- std::initializer_list<std::string>::iterator it;
- for(it = napisy.begin(); it != napisy.end(); ++it)
- {
- wloz(*it);
- }
- }
- void wloz(std::string input)
- {
- if(ile == pojemnosc)//grow array
- {
- pojemnosc *= 2;
- std::string* newArray = new std::string[pojemnosc];
- std::copy(tablica,tablica + ile, newArray);
- delete[] tablica;
- tablica = newArray;
- }
- ++ile;
- tablica[ile-1] = input;
- }
- std::string sciagnij()
- {
- //mozna wzic pod uwage przypadek gdy mamy array duzy po uzyciu stos(capacity)
- if(ile <= pojemnosc/2)//shrink array
- {
- pojemnosc /= 2;
- std::string* newArray = new std::string[pojemnosc];
- std::copy(tablica,tablica + ile, newArray);
- delete[] tablica;
- tablica = newArray;
- }
- --ile;
- return tablica[ile];
- }
- std::string sprawdz()
- {
- if(ile == 0)
- throw std::invalid_argument("stos jest pusty");
- return tablica[ile-1];
- }
- int rozmiar()
- {
- return ile;
- }
- int getCapacity()
- {
- return this->pojemnosc;
- }
- };
- int main()
- {
- try
- {
- stos nowyStos = stos(10);
- nowyStos.wloz("napis");
- std::cout << nowyStos.rozmiar();
- std::cout << nowyStos.sciagnij();
- std::cout << nowyStos.getCapacity();
- stos drugiStos = stos({"pierwszy","drugi","trzeci"});
- //std::cout << nowyStos.sprawdz();
- }
- catch(const std::exception& e)
- {
- std::clog << e.what();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement