Advertisement
Mary_99

TASK 2B CORRET

Nov 3rd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <assert.h>
  3. #include <string.h>
  4. #include "stack2b.h"
  5. #define STACKSIZE 3
  6. #define INIT -1
  7.  
  8. using namespace std;
  9.  
  10. Stack::Stack()
  11. {
  12.     totalSize = STACKSIZE;
  13.     top = INIT;
  14.     array = (int*)malloc(sizeof(int)*totalSize);
  15.     if(array == NULL)
  16.     {
  17.         printf("Memory not allocated\n");
  18.         abort();
  19.     }
  20.  
  21. }
  22.  
  23.  
  24. Stack::Stack(const Stack &obj)
  25. {
  26.     //top = obj.top;
  27.    totalSize = obj.totalSize;
  28. //     array = (int*)malloc(sizeof(int) * totalSize);
  29. //     assert(array != NULL);
  30.     if(!(array = (int*)malloc(sizeof(int) * totalSize)))
  31.     {
  32.        printf("ERROR\n");
  33.        free(array);
  34.        abort();
  35.     }
  36.     this-> copy(obj);
  37. //     for(int i=0; i<obj.totalSize; i++)
  38. //     {
  39. //         array[i] = obj.array[i];
  40. //     }
  41. }
  42.  
  43. void Stack::copy(const Stack &obj)
  44. {
  45.     top = obj.top;
  46.     for(int i=0; i<obj.totalSize; i++)
  47.     {
  48.         array[i] = obj.array[i];
  49.     }
  50. }
  51.  
  52.  
  53. Stack& Stack::operator= (const Stack &obj)
  54. {
  55.    
  56.     if(obj.totalSize > totalSize)
  57.     {
  58.         if(!(array = (int*)realloc(array, sizeof(int) * totalSize)))
  59.         {
  60.             printf("ERROR\n");
  61.             free(array);
  62.             abort();
  63.         }
  64.         totalSize = obj.totalSize;
  65.     }
  66.     this-> copy(obj);
  67.      /*top = obj.top;
  68.      totalSize = obj.totalSize;
  69.      array = (int*)realloc(array, sizeof(int) * totalSize);
  70.      assert(array != NULL);
  71.      for(int i=0; i<obj.totalSize; i++)
  72.      {
  73.          array[i] = obj.array[i];
  74.     }*/
  75.     return *this;
  76. }
  77.  
  78.  
  79. void Stack::push(int element)
  80. {
  81.     if(top == totalSize - 1)
  82.     {
  83.         totalSize *= 2;
  84.         int *temp = (int*)realloc(array, sizeof(int)*totalSize);
  85.         if(temp == NULL)
  86.         {
  87.            cout<<"Realloc failed"<<endl;
  88.             abort();
  89.         } else {
  90.             array = temp;
  91.         }
  92.     }
  93.     array[++top] = element;
  94. }
  95.  
  96. int Stack::pop()
  97. {
  98.     if(isEmpty())
  99.     {
  100.         cout<<"Stack is empty"<<endl;
  101.         abort();
  102.     }
  103.     return array[top--];
  104. }
  105.  
  106. bool Stack::isEmpty()
  107. {
  108.     if(top == -1)
  109.     {
  110.         return true;
  111.     } else {
  112.         return false;
  113.     }
  114. }
  115.  
  116. void Stack::getTotatSize()
  117. {
  118.     printf("Size of stack is: %d\n", totalSize);
  119. }
  120.  
  121. Stack::~Stack()
  122. {
  123.    free(array);
  124.  
  125. }
  126. void Stack::display()
  127. {
  128.     int i;
  129.     for(i = 0; i <= top; i++)
  130.         cout << array[i]<<endl;
  131. }
  132.  
  133. int Stack::giveTop()
  134. {
  135.     return array[top];
  136. }
  137.  
  138. bool Stack::operator==(const Stack& s)
  139. {
  140.     if(this->top != s.top)
  141.     {
  142.         s.~Stack();
  143.         this-> ~Stack();
  144.         return false;
  145.     }
  146.     for(int i=0; i<=this-> top; i++)
  147.     {
  148.         if(this->array[i] != s.array[i])
  149.         {
  150.             return false;
  151.         }
  152.     }
  153.     return true;
  154. }
  155. #pragma once
  156.  
  157. using namespace std;
  158.  
  159. class Stack{
  160.    
  161.     int totalSize;
  162.     int top;
  163.     int * array;
  164.    
  165.     public:
  166.     void display();
  167.     void push(int elemennt);
  168.     int pop();
  169.     bool isEmpty();
  170.     int giveTop();
  171.     void getTotatSize();
  172.     void copy(const Stack&);
  173.     Stack(const Stack &obj);
  174.     Stack& operator= (const Stack&);
  175.     bool operator==(const Stack&);
  176.     Stack();
  177.     ~Stack();
  178.    
  179. };  
  180.  #include <iostream>
  181. #include <string>
  182. #include <string.h>
  183. #include "stack2b.h"
  184. #include "stack2b.cpp"
  185.    
  186. #define STACKSIZE 3
  187. #define DOUBLE_SIZE 2
  188. #define INITIAL_TOP -1    
  189.    
  190. using namespace std;
  191.  
  192.  
  193. int main(int argc, char const *argv[])
  194. {
  195.    
  196.     Stack s;
  197.     assert(s.isEmpty()==1);
  198.     s.push(1);
  199.     s.push(2);
  200.     s.push(2);
  201.     s.push(3);
  202.     s.display();
  203.     cout<<endl;
  204.     assert(s.pop()==3);
  205.     s.display();
  206.     cout<<endl;
  207.    
  208.    
  209.     Stack s2(s);
  210.     assert(s2 == s);
  211.  
  212.     s2.display();
  213.     cout<<endl;
  214.     s2.push(3);
  215.     s2.push(4);
  216.    
  217.     assert(s2.giveTop()==4);
  218.     assert(s2.isEmpty()==0);
  219.     assert(s2.pop()==4);
  220.     s2.display();
  221.     cout<<endl;
  222.    
  223.     s=s2;
  224.    
  225.     s2.display();
  226.     cout<<endl;
  227.     s.display();
  228.    
  229.  
  230.    
  231.    /*
  232.     Stack s1;
  233.     s1.push(1);
  234.     s1.push(2);
  235.     s1.push(3);
  236.    
  237.    
  238.     Stack s2(s1);
  239.  
  240.     Stack s3 = s1;
  241.  
  242.     Stack s4;
  243.     s4.push(10);
  244.     s4 = s1;
  245.    
  246.     s1.display();
  247.     printf("\n");
  248.     s2.display();
  249.     printf("\n");
  250.     s3.display();
  251.     printf("\n");
  252.     s4.display();
  253.    */
  254.    
  255.    
  256.    
  257.     return 0;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement