Advertisement
fabis_sparks

Lab4

Jun 2nd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3.  
  4. using namespace std;
  5.  
  6. class MyDoubleStack
  7. {
  8. private:
  9.     double * _array = NULL;
  10.     int _arraySize = -1;
  11.     int _arrayPos = -1;
  12.  
  13. public:
  14.     MyDoubleStack(int size)
  15.     {
  16.         _arraySize = size;
  17.         _array = new double[_arraySize];
  18.         for (int i = 0; i < _arraySize; i++) _array[i] = NAN;
  19.         _arrayPos = -1;
  20.     }
  21.  
  22.     ~MyDoubleStack()
  23.     {
  24.         delete[] _array;
  25.         _array = NULL;
  26.         _arraySize = -1;
  27.         _arrayPos = -1;
  28.     }
  29.    
  30.     void Push(double value)
  31.     {
  32.         if (!TryPush(value))
  33.         {
  34.             cout << "Can't push element. Maximum elements count reached." << endl;
  35.             return;
  36.         }
  37.     }
  38.  
  39.     double Pop()
  40.     {
  41.         double res = NULL;
  42.         if (!TryPop(&res))
  43.         {
  44.             cout << "Can't pop element. Stack is empty." << endl;
  45.             return NAN;
  46.         }
  47.         return res;
  48.     }
  49.  
  50.     bool TryPush(double value)
  51.     {
  52.         if (_arrayPos >= _arraySize-1) return false;
  53.         _array[++_arrayPos] = value;
  54.         return true;
  55.     }
  56.  
  57.     bool TryPop(double * outValue)
  58.     {
  59.         if (_arrayPos <= -1) return false;
  60.         *outValue = _array[_arrayPos];
  61.         _array[_arrayPos] = NAN;
  62.         _arrayPos--;
  63.         return true;
  64.     }
  65.  
  66. };
  67.  
  68. // http://stackoverflow.com/questions/2704521/generate-random-double-numbers-in-c
  69. double fRand(double fMin, double fMax)
  70. {
  71.     double f = (double)rand() / RAND_MAX;
  72.     return fMin + f * (fMax - fMin);
  73. }
  74.  
  75.  
  76.  
  77. int main()
  78. {
  79.     srand(time(0));
  80.  
  81.     int arrSize = 10;
  82.  
  83.     double ** arr = new double*[arrSize];
  84.  
  85.     cout << "Array of ptr to doubles:" << endl;
  86.     for (int i = 0; i < arrSize; i++)
  87.     {
  88.         arr[i] = new double(fRand(-10, 10));
  89.         cout << *arr[i] << endl;
  90.     }
  91.     cout << endl;
  92.  
  93.     MyDoubleStack positives(arrSize), negatives(arrSize);
  94.  
  95.     for (int i = 0; i < arrSize; i++)
  96.     {
  97.         if (*arr[i] < 0) negatives.Push(*arr[i]);
  98.         else positives.Push(*arr[i]);
  99.     }
  100.  
  101.     double d;
  102.     cout << "Stack of positives was:" << endl;
  103.     while (positives.TryPop(&d)) cout << d << endl;
  104.     cout << endl;
  105.  
  106.     cout << "Stack of negatives was:" << endl;
  107.     while (negatives.TryPop(&d)) cout << d << endl;
  108.     cout << endl;
  109.  
  110.  
  111.     for (int i = 0; i < arrSize; i++)
  112.     {
  113.         delete arr[i];
  114.     }
  115.     delete[] arr;
  116.  
  117.     system("pause");
  118.  
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement