Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. class Stack
  9. {
  10.     private:
  11.         int size, size_now, top;
  12.         int *arr = new int[0];
  13.         int *arr_del_last();
  14.     public:
  15.         Stack (int _size);
  16.         ~Stack();
  17.         void add (int elem);
  18.         int del_last();
  19.         bool is_S_over_flow();
  20.         bool is_S_empty();
  21. };
  22.  
  23. Stack::Stack (int _size)
  24. {
  25.     arr = new int [_size];
  26.     size = _size;
  27.     size_now = 0;
  28.     top = -1;
  29. }
  30.  
  31. Stack::~Stack()
  32. {
  33.     delete[] arr;
  34. }
  35.  
  36. bool Stack::is_S_over_flow()
  37. {
  38.     return size_now == size;
  39. }
  40.  
  41. bool Stack::is_S_empty()
  42. {
  43.     return size == 0;
  44. }
  45.  
  46. void Stack::add (int elem)
  47. {
  48.     if (!is_S_over_flow())
  49.     {
  50.         top++;
  51.         arr[top] = elem;
  52.         size_now++;
  53.     }
  54. }
  55.  
  56. int *Stack::arr_del_last()
  57. {
  58.     int * new_arr = new int [size_now];
  59.     for (int i = 0; i < size_now - 1; i++)
  60.     {
  61.         new_arr[i] = arr[i];
  62.     }
  63.     return new_arr;
  64. }
  65.  
  66. int Stack::del_last()
  67. {
  68.     if (!is_S_empty())
  69.     {
  70.         int elem_last;
  71.         elem_last = arr[top];
  72.         arr = arr_del_last();
  73.         top--;
  74.         size_now--;
  75.         return elem_last;
  76.     }
  77. }
  78.  
  79. int main()
  80. {
  81.     int size1, size2, elem;
  82.     string name1, name2;
  83.     cin >> name1 >> size1;
  84.     Stack Stack1 (size1);
  85.     cin >> name2 >> size2;
  86.     Stack Stack2 (size2);
  87.     while (cin >> elem)
  88.     {
  89.         if (!Stack1.is_S_over_flow() and !Stack2.is_S_over_flow())
  90.         {
  91.             Stack1.add (elem);
  92.             Stack2.add (elem);
  93.         }
  94.         else
  95.             break;
  96.     }
  97.     cout << name1 << " " << size1 << endl;
  98.     cout << name2 << " " << size2 << endl;
  99.     cout << name1 << "   " << name2 << endl;
  100.     cout << setw(15) << left << name1 << setw(15) << left << name2 << endl;
  101.     while (!Stack1.is_S_empty() && !Stack2.is_S_empty()) {
  102.         cout << setw(15) << right << Stack1.del_last() << setw(15) << right << Stack2.del_last() << endl;  
  103.     }
  104.     return(0);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement