SHARE
TWEET

cis-277 stack arrays

a guest Feb 28th, 2020 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Course ID: CIS-277
  2. //Name: Arlon Arvesu
  3. //Prof. Aljamal
  4.  
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <string>
  8. #include <cmath>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <fstream>
  12. using namespace std;
  13.  
  14. const int SIZE = 10;
  15. //****************************************************
  16. //              FUNCTION PROTOTYPES                  *
  17. //****************************************************
  18. void createStack();
  19. bool isEmpty(int &TOP);
  20. void purgeStack (int* S, int &TOP);
  21. void pushStack(int* S, const int SIZE, int &TOP);
  22. void popStack(int* S, const int SIZE, int &TOP);
  23. void topStack(int* S, const int SIZE, int &TOP);
  24.  
  25. int main()
  26. {
  27.     //************************************************
  28.     //         VARIABLE INITIALIZATION               *
  29.     //************************************************
  30.     int choice;
  31.     char repeatChoice = 'Y';
  32.    
  33.     cout << "Please choose on of the following options: " << endl;
  34.     cout << "*******************************************" << endl;
  35.     cout << "* 1) Create stack                         *" << endl;
  36.     cout << "* 2) Check if stack is empty              *" << endl;
  37.     cout << "* 3) Purge stack                          *" << endl;
  38.     cout << "* 4) Push onto stack                      *" << endl;
  39.     cout << "* 5) Pop stack                            *" << endl;
  40.     cout << "* 6) Check top of stack                   *" << endl;
  41.     cout << "*******************************************" << endl;
  42.     //************************************************
  43.     //                INPUT SECTION                  *
  44.     //************************************************
  45.     cin >> choice;
  46.     cout << endl;
  47.     //************************************************
  48.     //              PROCESSING SECTION               *
  49.     //************************************************
  50.     while (repeatChoice == 'Y' || repeatChoice == 'y')
  51.     {  
  52.         switch(choice)
  53.         {
  54.             case 1: createStack();
  55.                     repeatChoice = 'n'; // Change it to 'n' here as you chose to finish in the createStack function but not here
  56.                     break;              // Otherwise you'll keep looping in THIS while loop
  57.             default:
  58.                     cout << "Error, cannot perform as there is no stack created yet!" << endl << endl;
  59.                     cout << "Would you like to try again?" << endl;
  60.                     cin >> repeatChoice;
  61.                     cout << endl;
  62.                     if (repeatChoice == 'Y' || repeatChoice == 'y')
  63.                     {
  64.                         cout << "Please enter (1) to create a stack " << endl;
  65.                         cin >> choice;
  66.                         cout << endl;
  67.                     }
  68.                     break;
  69.         }
  70.     }
  71.     //************************************************
  72.     //              OUTPUT SECTION                   *
  73.     //************************************************
  74.  system("pause");  
  75.  return 0;  
  76. }
  77.  
  78. //****************************************************
  79. //              FUNCTION DECLARATION                 *
  80. //****************************************************
  81. void createStack()
  82. {
  83.     int stack[SIZE],
  84.         TOP = -1,
  85.         opChoice;
  86.     char repeatChoice = 'Y';
  87.    
  88.     cout << "Stack has been successfully made!" << endl;
  89.     cout << "Please choose on of the following options: " << endl;
  90.     cout << "*******************************************" << endl;
  91.     cout << "* 1) Create stack *Stack already made*    *" << endl;
  92.     cout << "* 2) Check if stack is empty              *" << endl;
  93.     cout << "* 3) Purge stack                          *" << endl;
  94.     cout << "* 4) Push onto stack                      *" << endl;
  95.     cout << "* 5) Pop stack                            *" << endl;
  96.     cout << "* 6) Check top of stack                   *" << endl;
  97.     cout << "*******************************************" << endl;
  98.     //************************************************
  99.     //                INPUT SECTION                  *
  100.     //************************************************
  101.     cin >> opChoice;
  102.     cout << endl;
  103.     //************************************************
  104.     //              PROCESSING SECTION               *
  105.     //************************************************
  106.     while (repeatChoice == 'Y' || repeatChoice == 'y')
  107.     {  
  108.         switch(opChoice)
  109.         {
  110.             case 1: cout << "Stack already made! Please choose another option." << endl;
  111.                     break;
  112.                    
  113.             case 2: isEmpty(TOP);
  114.                     break;
  115.                    
  116.             case 3: purgeStack(stack, TOP);
  117.                     break;
  118.                    
  119.             case 4: pushStack(stack, SIZE, TOP);
  120.                     break;
  121.                    
  122.             case 5: popStack(stack, SIZE, TOP);
  123.                     break;
  124.                    
  125.             case 6: topStack(stack, SIZE, TOP);
  126.                     break;
  127.                    
  128.             default:
  129.                     cout << "Error, invalid input!" << endl << endl;
  130.                     break;
  131.         }
  132.         cout << "Would you like to do another operation?" << endl;
  133.         cin >> repeatChoice;
  134.         cout << endl;
  135.         if (repeatChoice == 'Y' || repeatChoice == 'y')
  136.         {
  137.             cout << "Please choose one of the options shown above: " << endl;
  138.             cin >> opChoice;
  139.             cout << endl;
  140.         }
  141.     }
  142. }
  143. bool isEmpty(int &TOP)
  144. {
  145.     if (TOP == -1)
  146.     {
  147.         cout << "Stack status: EMPTY " << endl << endl;
  148.         return true;
  149.     }
  150.     else
  151.     {
  152.         cout << "Stack status: Not empty. Currently there are " << (TOP + 1) << " value(s) in stack " << endl;
  153.         return false;
  154.     }
  155. }
  156. void purgeStack (int* S, int &TOP)
  157. {
  158.     delete[] S;
  159.     TOP = -1;
  160. }
  161. void pushStack(int* S, const int SIZE, int &TOP)
  162. {
  163.     int item;
  164.     if (TOP > (SIZE-1))
  165.         cout << "Error, stack is full! " << endl;
  166.        
  167.     cout << "Please enter the integer you would like to push onto the stack: " << endl;
  168.     cin >> item;  
  169.     cout << endl;
  170.     S[++TOP ] = item;
  171. }
  172. void popStack(int* S, const int SIZE, int &TOP)
  173. {
  174.     if(TOP == -1)
  175.         cout << "Stack is empty, unable to pop!" << endl;
  176.     else if (TOP < -1)
  177.         cout << "Stack is empty unable to pop!" << endl;
  178.     else
  179.         TOP--;
  180. }
  181. void topStack(int* S, const int SIZE, int &TOP)
  182. {
  183.     cout << "Current top value of the stack is: " << S[TOP] << endl;
  184. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top