Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int S[100], n, baza, varf, i, nr;
  5.  
  6. void initializare() {
  7. cout << "Valoare pentru capacitatea stivei, n = "; cin >> n;
  8. for (i = 1; i <= n; i++)
  9. S[i] = 0;
  10. varf = 0;
  11. baza = 0;
  12. }
  13.  
  14. void creare() {
  15. if (varf == 0) {
  16. varf = 1;
  17. cout << "Introduceti valoare pentru elementul din varful stivei S[" << varf << "]="; cin >> S[varf];
  18. }
  19. else
  20. cout << "Operatie fara sens : stiva nu este vida" << endl;
  21. }
  22.  
  23. void intrare() {
  24. varf = varf + 1;
  25. if (varf <= n) {
  26. cout << "Introduceti valoarea pentru elementul din varful stivei S[" << varf << "]="; cin >> S[varf];
  27. }
  28. else
  29. cout << "Operatie imposibila : stiva este plina" << endl;
  30. }
  31.  
  32. void iesire() {
  33. if (varf == 0)
  34. cout << "Operatie imposibila : stiva este goala" << endl;
  35. else {
  36. cout << "Iese elementul din varful stivei S[" << varf << "]" << endl;
  37. S[varf] = 0;
  38. varf = varf - 1;
  39. }
  40. }
  41.  
  42. void parcurgere() {
  43. if (varf == 0)
  44. cout << "Operatie imposibila : stiva este goala" << endl;
  45. else {
  46. baza = 1;
  47. nr = 0;
  48. for (i = varf; i >= baza; i--) {
  49. cout << S[i] << " ";
  50. nr = nr + 1;
  51. }
  52. cout << "Stiva contine " << nr << " elemente" << endl;
  53. }
  54. }
  55. int main()
  56. {
  57. initializare();
  58. int optiune = 0;
  59. do {
  60. do {
  61. cout << "Operatii asupra stivei : " << endl;
  62. cout << "1 creare" << endl;
  63. cout << "2 intrare element nou" << endl;
  64. cout << "3 iesirea unui element" << endl;
  65. cout << "4 parcurgere" << endl;
  66. cout << "5 sfarsit program" << endl;
  67. cout << "Introduceti optiunea (1, 2, 3, 4, 5) : "; cin >> optiune;
  68. } while (optiune > 5);
  69. switch (optiune) {
  70. case 1: creare(); break;
  71. case 2: intrare(); break;
  72. case 3: iesire(); break;
  73. case 4: parcurgere(); break;
  74. case 5: cout << "Sfarsit program"; break;
  75. }
  76. } while (optiune != 5);
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement