Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #define DIM 20000
  4. using namespace std;
  5.  
  6. template <class T>
  7. class Pila {
  8. private:
  9. T VettorePila[DIM];
  10. T cima;
  11.  
  12. public:
  13. Pila(); //Costruttore della pila
  14. void Push(T elemento); //Inserisce un elemento nella pila
  15. T Pop(); //Estrae un elemento dalla pila
  16. void StampaPila();
  17. bool PilaVuota(); // Verifica se la pila è piena
  18. bool PilaPiena(); // Verifica se la pila è vuota
  19. };
  20.  
  21. template <class T>
  22. Pila<T>::Pila(void)
  23. : cima(-1){};
  24.  
  25. template <class T>
  26. bool Pila<T>::PilaPiena()
  27. {
  28. if (cima == (DIM - 1))
  29. return 1;
  30. else
  31. return 0;
  32. }
  33.  
  34. template <class T>
  35. bool Pila<T>::PilaVuota()
  36. {
  37. if (cima == -1)
  38. return 1;
  39. else
  40. return 0;
  41. }
  42.  
  43. template <class T>
  44. void Pila<T>::Push(T x)
  45. {
  46.  
  47. if (PilaPiena()) {
  48. cerr << "Stack Overflow" << '\n';
  49. exit(1);
  50. }
  51. else {
  52. cima++;
  53. VettorePila[cima] = x;
  54. }
  55. }
  56.  
  57. template <class T>
  58. T Pila<T>::Pop()
  59. {
  60. T x;
  61.  
  62. if (PilaVuota()) {
  63. cerr << "Stack Underflow" << '\n';
  64. exit(1);
  65. }
  66. else {
  67. x = VettorePila[cima];
  68. cima--;
  69. return x;
  70. }
  71. }
  72.  
  73. template <class T>
  74. void Pila<T>::StampaPila()
  75. {
  76.  
  77. if (PilaVuota()) {
  78. cout << "Pila vuota!" << endl;
  79. }
  80. else {
  81. for (int i = cima; i >= 0; i--) {
  82. cout << VettorePila[i] << " ";
  83. }
  84. }
  85. }
  86.  
  87.  
  88. int main()
  89. {
  90. freopen("input(1).txt", "r", stdin);
  91. // freopen("output.txt","w",stdout);
  92.  
  93. string line;
  94. int N;
  95. while (cin >> line >> N) {
  96.  
  97. /* questa funziona
  98. if ( line == "int") {
  99. int num;
  100. Pila <int>p;
  101. for (size_t i = 0; i < N; i++) {
  102. cin >> num;
  103. //cout << num << " ";
  104. p.Push(num);
  105. }
  106. p.StampaPila();
  107.  
  108. }else if ( line == "bool") {
  109. bool num;
  110. Pila <bool>p;
  111. for (size_t i = 0; i < N; i++) {
  112. cin >> num;
  113. p.Push(num);
  114. }
  115. p.StampaPila();
  116.  
  117. }else if ( line == "char") {
  118. char num;
  119. Pila <char>p;
  120. for (size_t i = 0; i < N; i++) {
  121. cin >> num;
  122. p.Push(num);
  123. }
  124. p.StampaPila();
  125.  
  126. }
  127. */
  128. if (line == "double") {
  129. double num;
  130. Pila<double> p;
  131. for (size_t i = 0; i < N; i++) {
  132. cin >> num;
  133. p.Push(num);
  134.  
  135. }
  136. p.StampaPila();
  137. }
  138.  
  139. cout << '\n';
  140. }
  141. return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement