Advertisement
Guest User

main.cpp

a guest
Mar 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.48 KB | None | 0 0
  1. #include "Header.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. Stack::Stack()
  7. {
  8.     prev = NULL;
  9. }
  10.  
  11. Stack::Stack(int newNum, Stack *newLink)
  12. {
  13.     prev = newLink;
  14.     val = newNum;
  15. }
  16.  
  17. StackClass::StackClass()
  18. {
  19.     top = NULL;
  20. }
  21.  
  22. void StackClass::push(int newNum)
  23. {
  24.     Stack *temp = top;
  25.  
  26.     //checks if the stack is null
  27.     if (top == NULL)
  28.     {
  29.         top = new Stack(newNum);
  30.         top->prev = NULL;
  31.     }
  32.     else //add new top with link to previous top (temp)
  33.     {
  34.         top = new Stack(newNum, temp);
  35.     }
  36. }
  37.  
  38. void StackClass::pop()
  39. {
  40.     //moves top to the next node down in the stack then deletes old top
  41.     Stack *temp = top;
  42.     top = top->prev;
  43.     delete temp;
  44. }
  45.  
  46. void StackClass::travPrt(void(*print)(Stack*))
  47. {
  48.     //traverses the linked list and prints values
  49.     Stack *newNode = top;
  50.     while (newNode != NULL)
  51.     {
  52.         (*print)(newNode);
  53.         newNode = newNode->prev;
  54.     }
  55. }
  56.  
  57. bool StackClass::isNull()
  58. {
  59.     //checks if stack is empty for underflow condition
  60.     if (top == NULL)
  61.     {
  62.         return true;
  63.     }
  64.     else
  65.     {
  66.         return false;
  67.     }
  68. }
  69.  
  70. void print(Stack *p)
  71. {
  72.     cout << p->val << endl;
  73. }
  74.  
  75. Queue::Queue()
  76. {
  77.     next = NULL;
  78. }
  79.  
  80. Queue::Queue(int newNum, Queue *nextLink)
  81. {
  82.     val = newNum;
  83.     next = nextLink;
  84. }
  85.  
  86. QueueClass::QueueClass()
  87. {
  88.     front = NULL;
  89.     rear = NULL;
  90. }
  91.  
  92. void QueueClass::append(int newNum)
  93. {
  94.     Queue *temp = rear;
  95.  
  96.     //adds node to empty queue
  97.     if (front == NULL)
  98.     {
  99.         front = new Queue(newNum);
  100.     }
  101.     //adds second node and connects front to rear
  102.     else if (front->next == NULL)
  103.     {
  104.         rear = new Queue(newNum);
  105.         front->next = rear;
  106.     }
  107.     //adds node and moves rear
  108.     else
  109.     {
  110.         rear = new Queue(newNum);
  111.         temp->next = rear;
  112.     }
  113. }
  114.  
  115. void QueueClass::serve()
  116. {
  117.     //moves front one toward rear and deletes old front
  118.     Queue *temp = front;
  119.     front = front->next;
  120.     delete temp;
  121. }
  122.  
  123. bool QueueClass::frontNull()
  124. {
  125.     //checks if queue is empty for underflow
  126.     if (front == NULL)
  127.     {
  128.         return true;
  129.     }
  130.     else
  131.     {
  132.         return false;
  133.     }
  134. }
  135.  
  136. void QueueClass::travPrint(void(*print)(Queue*))
  137. {
  138.     //traverses queue and prints values
  139.     Queue* newNode = front;
  140.     while (newNode != NULL)
  141.     {
  142.         (*print)(newNode);
  143.         newNode = newNode->next;
  144.     }
  145. }
  146.  
  147. void print(Queue *p)
  148. {
  149.     cout << p->val << endl;
  150. }
  151.  
  152. using namespace std;
  153.  
  154. int main()
  155. {
  156.     StackClass stack1;
  157.     QueueClass queue1;
  158.     ifstream infile;
  159.     string filename;
  160.     string command;
  161.     int newNum;
  162.     const int SPACES_1 = 10;
  163.     const int SPACES_2 = 100;
  164.     const int SPACES_3 = 1000;
  165.  
  166.     cout << "Nate Bothwell" << endl << "CS 2420, Section 601" << endl << "Program 3: Stacks and Queues" << endl << endl;
  167.  
  168.     //takes file name and exits program if file is not found
  169.     cout << "Enter valid filename: ";
  170.     getline(cin, filename);
  171.     infile.open(filename.c_str());
  172.     if (infile.fail())
  173.     {
  174.         cout << "File error" << endl;
  175.         system("pause");
  176.         return 1;
  177.     }
  178.  
  179.     //beginning of output format
  180.     cout << endl << "Operation Stack value  Result" << endl;
  181.  
  182.     while (!infile.eof())
  183.     {
  184.         //reads first command in file
  185.         infile >> command;
  186.  
  187.         if (command == "push")
  188.         {
  189.             //takes value associated with command
  190.             infile >> newNum;
  191.  
  192.             //output format continued
  193.             cout << command << "      " << stack1.typeName << " " << newNum;
  194.            
  195.             //padding for number of decimals to make output look better
  196.             if (newNum < SPACES_1)
  197.             {
  198.                 cout << "      ";
  199.             }
  200.             else if (newNum >= SPACES_1 && newNum < SPACES_2)
  201.             {
  202.                 cout << "     ";
  203.             }
  204.             else if (newNum >= SPACES_2 && newNum < SPACES_3)
  205.             {
  206.                 cout << "    ";
  207.             }
  208.             else
  209.             {
  210.                 cout << "   ";
  211.             }
  212.            
  213.             //last of output format
  214.             cout << "success" << endl;
  215.  
  216.             //perform command
  217.             stack1.push(newNum);
  218.         }
  219.         else if (command == "pop")
  220.         {
  221.             //output format continued
  222.             cout << command << "       " << stack1.typeName << " ---    ";
  223.  
  224.             //handles if queue is empty prior to pop command
  225.             if (stack1.isNull())
  226.             {
  227.                 cout << "underflow" << endl;
  228.             }
  229.             else
  230.             {
  231.                 cout << "success" << endl;
  232.                 //if stack is not empty, performs pop
  233.                 stack1.pop();
  234.             }
  235.         }
  236.         else if (command == "append")
  237.         {
  238.             //takes value associated with command
  239.             infile >> newNum;
  240.  
  241.             //output format continued
  242.             cout << command << "    " << queue1.typeName << " " << newNum;
  243.  
  244.             //padding for number of decimals to make output look better
  245.             if (newNum < SPACES_1)
  246.             {
  247.                 cout << "      ";
  248.             }
  249.             else if (newNum >= SPACES_1 && newNum < SPACES_2)
  250.             {
  251.                 cout << "     ";
  252.             }
  253.             else if (newNum >= SPACES_2 && newNum < SPACES_3)
  254.             {
  255.                 cout << "    ";
  256.             }
  257.             else
  258.             {
  259.                 cout << "   ";
  260.             }
  261.  
  262.             //last of output format
  263.             cout << "success" << endl;
  264.  
  265.             //perform command
  266.             queue1.append(newNum);
  267.         }
  268.         else if (command == "serve")
  269.         {
  270.             //output format continued
  271.             cout << command << "     " << queue1.typeName << " ---    ";
  272.  
  273.             //handles if queue is empty prior to serve command
  274.             if (queue1.frontNull())
  275.             {
  276.                 cout << "underflow" << endl;
  277.             }
  278.             else
  279.             {
  280.                 cout << "success" << endl;
  281.                 //if stack is not empty, performs serve
  282.                 queue1.serve();
  283.             }
  284.         }
  285.         //if command is mispelled or garbage, program will output error message and exit
  286.         else
  287.         {
  288.             cout << endl << "command \"" << command <<"\" not found" << endl << endl;
  289.             system("pause");
  290.             return 1;
  291.         }
  292.     }
  293.  
  294.     //print contents of stack
  295.     cout << endl << "Stack" << endl;
  296.     stack1.travPrt(print);
  297.  
  298.     //print contents of queue
  299.     cout << endl << "Queue" << endl;
  300.     queue1.travPrint(print);
  301.     cout << endl;
  302.  
  303.     infile.close();
  304.  
  305.     system("pause");
  306.     return 0;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement