Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <cstdlib>
- #include <limits>
- using namespace std;
- class Liczba: public exception {
- virtual const char* what() const throw() {
- return "Zła wartość.";
- }
- };
- class Stos: public exception {
- virtual const char* what() const throw() {
- return "Stos jest pusty.";
- }
- };
- class ObslugaStosu {
- list<int>stos;
- public:
- friend class Liczba;
- friend class Stos;
- int umiesc()
- {
- cout << "Wprowadz liczbe ktora chcesz umiescic na stosie: ";
- while(1) {
- try{
- int n;
- Liczba blad;
- cin>>n;
- stos.push_front(n);
- if(cin.fail()!=0) throw blad;
- return n;
- }
- catch(exception &blad) {
- cout<<blad.what()<<endl;
- }
- cin.clear();
- cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
- }
- }
- void usun()
- {
- try{
- Stos blad;
- if(stos.empty()) throw blad;
- cout << "Usunieta liczba: " << stos.front() << "." << endl;
- stos.pop_front();
- }
- catch(exception &blad) {
- cout<<blad.what()<<endl;
- }
- cin.clear();
- cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
- }
- void wyswietl()
- {
- cout << "Elementy stosu:" <<endl;
- list<int>::iterator it;
- try{
- Stos blad;
- if(stos.empty()) throw blad;
- for(it=stos.begin(); it!= stos.end(); ++it)
- {
- cout << *it << " " <<endl;
- }
- }
- catch(exception &blad) {
- cout<<blad.what()<<endl;
- }
- cin.clear();
- cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
- }
- };
- int main()
- {
- ObslugaStosu stosik;
- int w;
- while(1)
- {
- try{
- cout << "-OPJCE- " << endl;
- cout << "1. Dodanie elementu na stos." << endl;
- cout << "2. Usuniecie elementu ze stosu." << endl;
- cout << "3. Wyswietlenie stosu." << endl;
- cout << "4. Koniec." << endl << endl;
- cout << "Wybor: ";
- Liczba blad;
- if(!(cin>>w)) throw blad;
- switch (w)
- {
- case 1:
- {
- stosik.umiesc();
- cin.clear();
- break;
- }
- case 2:
- {
- stosik.usun();
- cout<<"- Naciśnij ENTER -"<<endl;
- cin.clear();
- break;
- }
- case 3:
- {
- stosik.wyswietl();
- cout<<"- Naciśnij ENTER -"<<endl;
- cin.clear();
- break;
- }
- case 4:
- {
- cin.clear();
- exit(1);
- break;
- }
- default:
- {
- cout << "Nie ma takiej opcji, sprobuj ponownie." << endl;
- cin.clear();
- break;
- }
- }
- }
- catch(exception &blad) {
- cout<<blad.what()<<endl;
- }
- cin.clear();
- cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement