Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cstdlib>
  4. #include <limits>
  5.  
  6. using namespace std;
  7.  
  8. class Liczba: public exception {
  9. virtual const char* what() const throw() {
  10. return "Zła wartość.";
  11. }
  12. };
  13.  
  14. class Stos: public exception {
  15. virtual const char* what() const throw() {
  16. return "Stos jest pusty.";
  17. }
  18. };
  19.  
  20.  
  21.  
  22. class ObslugaStosu {
  23. list<int>stos;
  24. public:
  25. friend class Liczba;
  26. friend class Stos;
  27.  
  28. int umiesc()
  29. {
  30. cout << "Wprowadz liczbe ktora chcesz umiescic na stosie: ";
  31. while(1) {
  32. try{
  33. int n;
  34. Liczba blad;
  35. cin>>n;
  36. stos.push_front(n);
  37. if(cin.fail()!=0) throw blad;
  38. return n;
  39. }
  40. catch(exception &blad) {
  41. cout<<blad.what()<<endl;
  42. }
  43. cin.clear();
  44. cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
  45.  
  46. }
  47.  
  48. }
  49.  
  50. void usun()
  51. {
  52.  
  53. try{
  54. Stos blad;
  55. if(stos.empty()) throw blad;
  56. cout << "Usunieta liczba: " << stos.front() << "." << endl;
  57. stos.pop_front();
  58.  
  59. }
  60. catch(exception &blad) {
  61. cout<<blad.what()<<endl;
  62. }
  63. cin.clear();
  64. cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
  65.  
  66. }
  67.  
  68. void wyswietl()
  69. {
  70. cout << "Elementy stosu:" <<endl;
  71. list<int>::iterator it;
  72.  
  73. try{
  74. Stos blad;
  75. if(stos.empty()) throw blad;
  76.  
  77. for(it=stos.begin(); it!= stos.end(); ++it)
  78. {
  79. cout << *it << " " <<endl;
  80. }
  81. }
  82. catch(exception &blad) {
  83. cout<<blad.what()<<endl;
  84. }
  85. cin.clear();
  86. cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
  87.  
  88.  
  89. }
  90. };
  91.  
  92.  
  93. int main()
  94. {
  95. ObslugaStosu stosik;
  96. int w;
  97. while(1)
  98. {
  99. try{
  100. cout << "-OPJCE- " << endl;
  101. cout << "1. Dodanie elementu na stos." << endl;
  102. cout << "2. Usuniecie elementu ze stosu." << endl;
  103. cout << "3. Wyswietlenie stosu." << endl;
  104. cout << "4. Koniec." << endl << endl;
  105. cout << "Wybor: ";
  106. Liczba blad;
  107. if(!(cin>>w)) throw blad;
  108.  
  109. switch (w)
  110. {
  111. case 1:
  112. {
  113. stosik.umiesc();
  114. cin.clear();
  115. break;
  116. }
  117. case 2:
  118. {
  119. stosik.usun();
  120. cout<<"- Naciśnij ENTER -"<<endl;
  121. cin.clear();
  122. break;
  123. }
  124. case 3:
  125. {
  126. stosik.wyswietl();
  127. cout<<"- Naciśnij ENTER -"<<endl;
  128. cin.clear();
  129. break;
  130. }
  131. case 4:
  132. {
  133. cin.clear();
  134. exit(1);
  135. break;
  136. }
  137. default:
  138. {
  139. cout << "Nie ma takiej opcji, sprobuj ponownie." << endl;
  140. cin.clear();
  141. break;
  142. }
  143. }
  144. }
  145. catch(exception &blad) {
  146. cout<<blad.what()<<endl;
  147. }
  148. cin.clear();
  149. cin.ignore( std::numeric_limits < std::streamsize >::max(), '\n' );
  150. }
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement