- //Matthew Fong
- //Program 7: queues
- //description: simulate a DMV queue
- #ifndef DYNINTQUEUE_H
- #define DYNINTQUEUE_H
- #include <string>
- #include <iostream>
- #include <ctime>
- using namespace std;
- class DynIntQueue
- {
- private:
- // Structure for the queue nodes
- struct QueueNode
- {
- string name; //Customer's name
- int customerNumber; //Customer's number
- string timeStamp; //Time which the customer was processed
- QueueNode *next; // Pointer to the next node
- time_t start;
- };
- QueueNode *front; // The front of the queue
- QueueNode *rear; // The rear of the queue
- int numItems; // Number of items in the queue
- public:
- // Constructor
- DynIntQueue();
- // Destructor
- ~DynIntQueue();
- // Queue operations
- void enqueue(string, int, string, time_t);
- void dequeue(string &, int &, string &, time_t &);
- bool isEmpty() const;
- bool isFull() const;
- void clear();
- };
- #endif
- #include <iostream>
- #include "DynIntQueue.h"
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- //********************************************
- // The constructor creates an empty queue. *
- //********************************************
- DynIntQueue::DynIntQueue()
- {
- front = NULL;
- rear = NULL;
- numItems = 0;
- }
- //********************************************
- // Destructor *
- //********************************************
- DynIntQueue::~DynIntQueue()
- {
- clear();
- }
- //********************************************
- // Function enqueue inserts the value in num *
- // at the rear of the queue. *
- //********************************************
- void DynIntQueue::enqueue(string n, int custNum, string t, time_t st)
- {
- QueueNode *newNode;
- // Create a new node and store num there.
- newNode = new QueueNode;
- newNode->name = n;
- newNode->customerNumber = custNum;
- newNode->timeStamp = t;
- newNode->start = st;
- newNode->next = NULL;
- // Adjust front and rear as necessary.
- if (isEmpty())
- {
- front = newNode;
- rear = newNode;
- }
- else
- {
- rear->next = newNode;
- rear = newNode;
- }
- // Update numItems.
- numItems++;
- }
- //**********************************************
- // Function dequeue removes the value at the *
- // front of the queue, and copies it into num. *
- //**********************************************
- void DynIntQueue::dequeue(string &n, int &custNum, string &t, time_t &st)
- {
- QueueNode *temp;
- if (isEmpty())
- {
- cout << "The queue is empty.\n";
- exit(EXIT_FAILURE);
- }
- else
- {
- // Save the front node value in num.
- n = front->name;
- custNum = front->customerNumber;
- t = front->timeStamp;
- st = front->start;
- // Remove the front node and delete it.
- temp = front;
- front = front->next;
- delete temp;
- // Update numItems.
- numItems--;
- }
- }
- //*********************************************
- // Function isEmpty returns true if the queue *
- // is empty, and false otherwise. *
- //*********************************************
- bool DynIntQueue::isEmpty() const
- {
- bool status;
- if (numItems > 0)
- status = false;
- else
- status = true;
- return status;
- }
- //********************************************
- // Function clear dequeues all the elements *
- // in the queue. *
- //********************************************
- void DynIntQueue::clear()
- {
- string name;
- int customerNumber;
- string timeStamp;
- time_t st;
- while(!isEmpty())
- dequeue(name, customerNumber, timeStamp, st);
- }
- #include "DynIntQueue.h"
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- void wait(int seconds);
- //double average(double t[]);
- int main()
- {
- string customerName;
- int custNum = 1; //Customer's number starts at 1
- stringstream timestamp1; //string for the time when they begin processing
- stringstream timestamp2; //string for the time after they have been processed
- DynIntQueue DMV; //Queue
- double dif;
- time_t start;
- time_t end;
- unsigned seed = time(0);
- srand(seed);
- double total = 0;
- double avg;
- for(int i = 0; i < 10; i++)
- {
- cout << "Enter your name: " << endl;
- getline(cin, customerName);
- cout << "Processing..." << endl;
- time(&start);
- time_t rawtime;
- struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- timestamp1 << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec; //Sets the string to the hour:min:sec
- wait(rand()%6); //Waits for 0-5 seconds
- DMV.enqueue(customerName, custNum, timestamp1.str(), start); //Enqueues the customer's name, number, and timestamp
- timestamp1.str(""); //Clears the string so the next timestamp doesn't add on to it
- cout << endl;
- custNum++;
- }
- int i = 0;
- while(!DMV.isEmpty())
- {
- string name;
- int num;
- string timestmp;
- wait(rand()%11); //Waits for 0-10 seconds
- DMV.dequeue(name, num, timestmp, start); //Dequeues
- time(&end);
- time_t rawtime;
- struct tm *timeinfo;
- time(&rawtime);
- timeinfo = localtime(&rawtime);
- timestamp2 << timeinfo->tm_hour << ":" << timeinfo->tm_min << ":" << timeinfo->tm_sec;
- dif = difftime(end,start);
- total = total + dif;
- i++;
- cout << "Customer name: " << name << endl;
- cout << "Customer number: " << num << endl;
- cout << "Time processing started: " << timestmp << endl;
- cout << "Time processing ended: " << timestamp2.str() << endl;
- cout << "Time taken to process: " << dif << " seconds." << endl;
- cout << endl;
- timestamp2.str("");
- }
- cout << "Average time to process: " << (total / 10) << " seconds." << endl;
- return 0;
- }
- void wait(int seconds)
- {
- clock_t endwait;
- endwait = clock() + seconds * CLOCKS_PER_SEC;
- while(clock() < endwait)
- {}
- }