mattfong

CS2370 Prog 7

May 26th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. //Matthew Fong
  2. //Program 7: queues
  3. //description: simulate a DMV queue
  4.  
  5. #ifndef DYNINTQUEUE_H
  6. #define DYNINTQUEUE_H
  7. #include <string>
  8. #include <iostream>
  9. #include <ctime>
  10. using namespace std;
  11.  
  12. class DynIntQueue
  13. {
  14. private:
  15. // Structure for the queue nodes
  16. struct QueueNode
  17. {
  18. string name; //Customer's name
  19. int customerNumber; //Customer's number
  20. string timeStamp; //Time which the customer was processed
  21. QueueNode *next; // Pointer to the next node
  22. time_t start;
  23. };
  24.  
  25. QueueNode *front; // The front of the queue
  26. QueueNode *rear; // The rear of the queue
  27. int numItems; // Number of items in the queue
  28.  
  29. public:
  30. // Constructor
  31. DynIntQueue();
  32.  
  33. // Destructor
  34. ~DynIntQueue();
  35.  
  36. // Queue operations
  37. void enqueue(string, int, string, time_t);
  38. void dequeue(string &, int &, string &, time_t &);
  39. bool isEmpty() const;
  40. bool isFull() const;
  41. void clear();
  42. };
  43. #endif
  44.  
  45.  
  46. #include <iostream>
  47. #include "DynIntQueue.h"
  48. #include <cstdlib>
  49. #include <sstream>
  50. using namespace std;
  51.  
  52. //********************************************
  53. // The constructor creates an empty queue. *
  54. //********************************************
  55.  
  56. DynIntQueue::DynIntQueue()
  57. {
  58. front = NULL;
  59. rear = NULL;
  60. numItems = 0;
  61. }
  62.  
  63. //********************************************
  64. // Destructor *
  65. //********************************************
  66.  
  67. DynIntQueue::~DynIntQueue()
  68. {
  69. clear();
  70. }
  71.  
  72. //********************************************
  73. // Function enqueue inserts the value in num *
  74. // at the rear of the queue. *
  75. //********************************************
  76.  
  77. void DynIntQueue::enqueue(string n, int custNum, string t, time_t st)
  78. {
  79. QueueNode *newNode;
  80.  
  81. // Create a new node and store num there.
  82. newNode = new QueueNode;
  83. newNode->name = n;
  84. newNode->customerNumber = custNum;
  85. newNode->timeStamp = t;
  86. newNode->start = st;
  87. newNode->next = NULL;
  88.  
  89. // Adjust front and rear as necessary.
  90. if (isEmpty())
  91. {
  92. front = newNode;
  93. rear = newNode;
  94. }
  95. else
  96. {
  97. rear->next = newNode;
  98. rear = newNode;
  99. }
  100.  
  101. // Update numItems.
  102. numItems++;
  103. }
  104.  
  105. //**********************************************
  106. // Function dequeue removes the value at the *
  107. // front of the queue, and copies it into num. *
  108. //**********************************************
  109.  
  110. void DynIntQueue::dequeue(string &n, int &custNum, string &t, time_t &st)
  111. {
  112. QueueNode *temp;
  113.  
  114. if (isEmpty())
  115. {
  116. cout << "The queue is empty.\n";
  117. exit(EXIT_FAILURE);
  118. }
  119. else
  120. {
  121. // Save the front node value in num.
  122. n = front->name;
  123. custNum = front->customerNumber;
  124. t = front->timeStamp;
  125. st = front->start;
  126.  
  127. // Remove the front node and delete it.
  128. temp = front;
  129. front = front->next;
  130. delete temp;
  131.  
  132. // Update numItems.
  133. numItems--;
  134. }
  135. }
  136.  
  137. //*********************************************
  138. // Function isEmpty returns true if the queue *
  139. // is empty, and false otherwise. *
  140. //*********************************************
  141.  
  142. bool DynIntQueue::isEmpty() const
  143. {
  144. bool status;
  145.  
  146. if (numItems > 0)
  147. status = false;
  148. else
  149. status = true;
  150. return status;
  151. }
  152.  
  153. //********************************************
  154. // Function clear dequeues all the elements *
  155. // in the queue. *
  156. //********************************************
  157.  
  158. void DynIntQueue::clear()
  159. {
  160. string name;
  161. int customerNumber;
  162. string timeStamp;
  163. time_t st;
  164.  
  165. while(!isEmpty())
  166. dequeue(name, customerNumber, timeStamp, st);
  167. }
  168.  
  169.  
  170. #include "DynIntQueue.h"
  171. #include <iostream>
  172. #include <ctime>
  173. #include <cstdlib>
  174. #include <sstream>
  175. using namespace std;
  176.  
  177. void wait(int seconds);
  178. //double average(double t[]);
  179.  
  180. int main()
  181. {
  182. string customerName;
  183. int custNum = 1; //Customer's number starts at 1
  184. stringstream timestamp1; //string for the time when they begin processing
  185. stringstream timestamp2; //string for the time after they have been processed
  186. DynIntQueue DMV; //Queue
  187. double dif;
  188. time_t start;
  189. time_t end;
  190. unsigned seed = time(0);
  191. srand(seed);
  192. double total = 0;
  193. double avg;
  194.  
  195. for(int i = 0; i < 10; i++)
  196. {
  197. cout << "Enter your name: " << endl;
  198. getline(cin, customerName);
  199. cout << "Processing..." << endl;
  200. time(&start);
  201. time_t rawtime;
  202. struct tm *timeinfo;
  203. time(&rawtime);
  204. timeinfo = localtime(&rawtime);
  205. timestamp1 << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec; //Sets the string to the hour:min:sec
  206.  
  207. wait(rand()%6); //Waits for 0-5 seconds
  208.  
  209. DMV.enqueue(customerName, custNum, timestamp1.str(), start); //Enqueues the customer's name, number, and timestamp
  210. timestamp1.str(""); //Clears the string so the next timestamp doesn't add on to it
  211. cout << endl;
  212. custNum++;
  213. }
  214.  
  215. int i = 0;
  216.  
  217. while(!DMV.isEmpty())
  218. {
  219. string name;
  220. int num;
  221. string timestmp;
  222.  
  223. wait(rand()%11); //Waits for 0-10 seconds
  224.  
  225. DMV.dequeue(name, num, timestmp, start); //Dequeues
  226.  
  227. time(&end);
  228. time_t rawtime;
  229. struct tm *timeinfo;
  230. time(&rawtime);
  231. timeinfo = localtime(&rawtime);
  232. timestamp2 << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec;
  233. dif = difftime(end,start);
  234. total = total + dif;
  235. i++;
  236.  
  237. cout << "Customer name: " << name << endl;
  238. cout << "Customer number: " << num << endl;
  239. cout << "Time processing started: " << timestmp << endl;
  240. cout << "Time processing ended: " << timestamp2.str() << endl;
  241. cout << "Time taken to process: " << dif << " seconds." << endl;
  242. cout << endl;
  243. timestamp2.str("");
  244. }
  245.  
  246. cout << "Average time to process: " << (total / 10) << " seconds." << endl;
  247. return 0;
  248. }
  249.  
  250. void wait(int seconds)
  251. {
  252. clock_t endwait;
  253. endwait = clock() + seconds * CLOCKS_PER_SEC;
  254. while(clock() < endwait)
  255. {}
  256. }
Advertisement
Add Comment
Please, Sign In to add comment