In many cases solving even a simple looking problem analytic
Solution
ifndef QUEUE2_H_
#define QUEUE2_H_
#include <iostream>
#include \"Teller.h\"
#include \"Queue.h\"
#include \"Customer.h\"
#include \"QueueNode.h\"
template <typename T>
class Queue
{
private:
const static int Q_SIZE = 10;
QueueNode<T>*front; // pointer to front of Queue
QueueNode<T>*rear; // pointer to rear of Queue
int count; // current number of items in Queue
const int qsize; // maximum number of items in Queue
// preemptive definitions to prevent public copying
Queue(T C)
{
qsize = 0;
rear = NULL;
front = NULL;
count = 0;
}
Queue & operator=(const T & q) {return *this;};
public:
~Queue()
{
QueueNode<T> * temp; // create temporary address store
while(front != (void *) 0) // while the queue is not empty
{
temp = front;
front =front->next; // advance the front object to the next
delete temp; // delete the temporary data
}
}
bool isempty() const
{
return count == 0;
}
bool isfull() const
{
return count == qsize;
}
int queuecount() const
{
return count;
}
bool enqueue(const T &data) // add item to end
{
if(isfull()) // if queue is full halt queuing
return false;
QueueNode<T> *add = new QueueNode<T>; // create node
add->customer = data; // set node pointers
add->next = (void *) 0; // or nullptr;
count++;
if (front == (void *) 0) // if queue is empty,
front = add; // place item at front
else
rear->next = add; // else place at rear
rear = add; // have rear point to new node
return true;
}
bool dequeue(T &data) // remove item from front
{
if(front == (void *) 0) // front node is empty, queue is empty
return false;
data = front->customer; // set data to first item in queue
count--; // decrement item count
QueueNode<T>* temp = front; // save location of first item
front = front->next; // reset front to next item
delete temp; // delete former first item
if (count == 0) // if the queue is now empty set rear to point to nothing
rear = (void *)0;
return true;
}
};
#endif /* QUEUE_H_ */
#ifndef QUEUENODE_H_
#define QUEUENODE_H_
#include <iostream>
#include \"Queue.h\"
#include \"Customer.h\"
using namespace std;
class Customer;
template<class T>
class Queue;
template<class T>
class QueueNode
{
private:
T customer;
QueueNode *next;
public:
QueueNode(T C)
{
C = customer;
next = NULL;
}
friend class Queue<T>;
friend class Customer;
};
#endif /* QUEUENODE_
#ifndef CUSTOMER_H_
#define CUSTOMER_H_
#include <iostream>
#include \"QueueNode.h\"
#include \"Queue.h\"
#include \"Teller.h\"
#include <cstdlib>
using namespace std;
class Customer
{
private:
int serviceTime;
int arrivalTime;
public:
Customer()
{
serviceTime = 0;
arrivalTime = 0;
}
/*
void setServiceTime()
{
serviceTime = 1 + rand() % maxService;
}
*/
Customer(int arrival, int maxService)
{
arrivalTime = arrival;
serviceTime = 1 + rand() % maxService;
}
int getServiceTime()
{
return serviceTime;
}
bool isDone()
{
return (serviceTime-- <= 0);
}
int getArrivalTime()
{
return arrivalTime;
}
};
#ifndef TELLER_H_
#define TELLER_H_
#include <iostream>
#include \"Customer.h\"
#include \"QueueNode.h\"
#include \"Queue.h\"
using namespace std;
class Teller
{
private:
bool isOccupied;
Customer customer;
public:
Teller()
{
isOccupied = true;
customer = NULL;
}
bool isFree()
{
if(isOccupied)
{
return true;
}
else if(customer.isDone())
{
isOccupied = true;
}
return isOccupied;
}
void addCustomer(Customer C)
{
customer = C;
isOccupied = false;
}
};
#include <iostream>
#include <vector>
#include \"Queue.h\"
#include \"Customer.h\"
#include \"QueueNode.h\"
#include \"Teller.h\"
using namespace std;
int main(int argc, char *argv[])
{
srand(time(NULL)); //seeds the random time generator
if(argc != 5)
{
cout << \"You must enter at least 5 command line arguments \" << endl;
exit(1);
}
double p = atoi(argv[1]);
int numTellers = atoi(argv[2]); //number of the tellers (M)
int maxService = atoi(argv[3]);
int maxClock = atoi(argv[4]); //simulation time (N)
Queue<Customer> customerQueue;
vector<Teller> tellers(numTellers);
int totalWait = 0;
int numCustServed = 1;
cout<<\"Customer No. Arrival time Waiting time in queue\"<<endl;
cout<<\"------------ ------------ ---------------------\"<<endl;
//////////////////////////////////////////////////////////////////
for(int i = 1; i < maxClock; i++) //loop begins at 1 and counts up
{
if(1 + rand()%10 <= maxService)
{
Customer customer(i, maxService);
customerQueue.enqueue(customer);
cout << \" \" << numCustServed << \" \" << i << \" \" << totalWait/numCustServed <<endl;
}
for(int t = 0; t < numTellers; t++)
{
if(tellers[t].isFree() && (!customerQueue.isempty()))
{
Customer customer = customerQueue.front();
tellers[t].addCustomer(customer);
totalWait = totalWait + (i - customer.getArrivalTime());
customerQueue.remove(); //dont know about this
numCustServed++;
}
}
cout << \"Total waiting time: \" << totalWait << endl;
cout << \"Number of Customers served: \" << numCustServed << endl;
cout << \"Average waiting time: \" << totalWait/numCustServed<<endl;
cout << \"After service the number of customers left in queue: \" <<endl;
cout << customerQueue.queuecount() << endl;
cout << \"=====================================================\" <<endl;
cout << \"\ \ \ Customer No. Arrival time Waiting time in queue\" <<endl;
cout << \"------------------------------------------------------\" <<endl;
/*
while(customerQueue.queuecount() > 0)
{
if(1 + rand()%10 <= maxService)
{
int currentTime;
i = currentTime;
Customer customer(i);
cout << \" \" << numCustServed << \" \" << i << \" \" << totalWait/numCustServed << endl;
}
}
*/
while(customerQueue.queuecount() > 0)
{
for(int m = 0; m < maxService; m++)
{
if(tellers[m].isFree() && (!customerQueue.isempty()))
{
Customer customer = customerQueue.front();
customerQueue.remove();
cout << \" \" << numCustServed << \" \" << i << \" \" << totalWait/numCustServed <<endl;
}
}
}
cout << \"Total waiting time: \" << totalWait << endl;
cout << \"Number of Customers served: \" << numCustServed << endl;
cout << \"Average waiting time: \" << totalWait/numCustServed<<endl;
cout << \"After service the number of customers left in queue: \" <<endl;
cout << customerQueue.queuecount() << endl;
return 0;
}
}





