Guest User

Untitled

a guest
Oct 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////
  2. main.cpp
  3. ///////////////////////////////////////////////////////////////
  4.  
  5.  
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include "stackClass_integer.h"
  9.  
  10. using namespace std;
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     stackClass_integer newStack(5);
  15.     newStack.pushInt(3);
  16.     newStack.pushInt(4);
  17.     cout << newStack.popInt() << endl;  
  18.     newStack.pushInt(7);
  19.     newStack.pushInt(5);
  20.     cout << newStack.popInt() << endl;
  21.     cout << newStack.popInt() << endl;
  22.     cout << newStack.popInt() << endl;
  23.     cout << newStack.popInt() << endl;  
  24.    
  25.     system("PAUSE");
  26.     return EXIT_SUCCESS;
  27. }
  28.  
  29.  
  30.  
  31.  
  32. ///////////////////////////////////////////////////////////////
  33. stackClass_Integer.h
  34. ///////////////////////////////////////////////////////////////
  35.  
  36. #pragma once
  37. #include <iostream>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40.  
  41. class stackClass_integer
  42. {
  43.       private:
  44.           int numInStack, sizeOfStack, ptrPosition;
  45.           int * stackPtr;
  46.       public:
  47.           stackClass_integer(int numInts);
  48.           void pushInt(int inputValue);
  49.           int popInt();
  50.           ~stackClass_integer(void);  
  51. };
  52.  
  53.  
  54. ///////////////////////////////////////////////////////////////
  55. stackClass_Integer.cpp
  56. ///////////////////////////////////////////////////////////////
  57.  
  58. #include "stackClass_integer.h"
  59.  
  60. stackClass_integer::stackClass_integer(int numInts)
  61. {
  62.     numInStack = numInts;
  63.     sizeOfStack = sizeof(int) * numInStack;
  64.     stackPtr = (int*) calloc(numInStack,sizeOfStack);
  65.     ptrPosition = 0;
  66. };
  67.  
  68. void stackClass_integer::pushInt(int inputValue)
  69. {
  70.     if(ptrPosition < numInStack)
  71.     {
  72.         std::cout << "PUSH: ptrPosition #" << ++ptrPosition << " now = "<<inputValue<<std::endl;
  73.         * ++stackPtr = inputValue;
  74.     }
  75. };
  76.  
  77. int stackClass_integer::popInt()
  78. {    
  79.     if(ptrPosition > 0)
  80.     {                      
  81.         int result = * stackPtr--;
  82.         std::cout << "POP: ptrPosition #" << ptrPosition-- << ": ";
  83.         return result;      
  84.     }
  85.     return -1;
  86. };
  87.  
  88. stackClass_integer::~stackClass_integer(void)
  89. {
  90. };
Add Comment
Please, Sign In to add comment