Hi, I am working on a programming exercise using an STL Queue library and am having trouble with the departure and arrival process, and also printing out the my file that is read to read the output below.
I am implementing the < operator in my Event struct since This is how the priority_queue class will compare your Events to determine which ones have higher priority. Events with earlier times should have a greater priority.
Assignment Description:
Implement the event-driven simulation of a bank that this chapter described. A queue of arrival events will represent the line of customers in the bank. Maintain the arrival events and departure events in a priority queue,
sorted by the time of the event. Use an STL Priority Queue
#include <iostream>
#include <iomanip>
#include <fstream>
#include <queue>
usingnamespace std;
struct Customer
{
int customerID;
};
struct Event
{
int CustomerID;
int transactionTime;
int arrivalTime;
int currentTime;
//Used to compare events and determine highest priority
booloperator < (const Event& right) const
{
returnthis->arrivalTime < right.arrivalTime;
}
};
void simulate();
int main()
{
simulate();
return 0;
}
void simulate()
{
//Used to read in file
ifstream File;
//Declaring local variables
int data;
int arrivalEvent = 0;
int transactionTime = 0;
bool tellerAvailable = 0;
//Creating an empty queue called bankQueue to represent a bank line
//Creating an empty priority queue called eventListPQueue for an event list
queue <Event> bankQueue;
priority_queue <Event> eventListPQueue;
//Creating and adding arrival events to event list
File.open("in1.txt");
//Data file is not empty
while(File)
{
//Getting next arriavl time and transaction time from file
File >> arrivalEvent >> transactionTime;
//cout << "test data: " << data << endl;
cout << "test newArrivalEvent: " << arrivalEvent << endl;
cout << "test transactionTime: " << transactionTime << endl;
// a new arrival event containing a and t
Event event;
event.arrivalTime = arrivalEvent;
event.transactionTime = transactionTime;
eventListPQueue.push(event);
}
//Closing File
File.close();
//An Event loop
int currentTime = 0;
//Event list is not empty
while (eventListPQueue.size() > 0 )
{
Event newEvent;
newEvent = eventListPQueue.top();
//Get current time
currentTime = newEvent.arrivalTime;
// is an arrival event
if (newEvent.arrivalTime)
processArrival(newEvent, eventListPQueue, bankQueue);
else
processDeparture(newEvent, eventListPQueue, bankQueue);
//**************** pseudiocode to fill in **************************************
//// Processes an arrival event.
//processArrival(arrivalEvent: Event, eventListPQueue : PriorityQueue, bankQueue : Queue)
//// Remove this event from the event list
//eventListPQueue.remove()
//
//customer = customer referenced in arrivalEvent
//if (bankQueue.isEmpty() && tellerAvailable) {
// departureTime = currentTime + transaction time in arrivalEvent
// newDepartureEvent = a new departure event with departureTime
// eventListPQueue.add(newDepartureEvent)
// tellerAvailable = false
//}
//else
// bankQueue.enqueue(customer)
//
// // Processes a departure event .
// +processDeparture(departureEvent: Event, eventListPQueue: PriorityQueue, bankQueue: Queue)
// // Remove this event from the event list
// eventListPQueue.remove()
//
// if (!bankQueue.isEmpty()) {
//
// // Customer at front of line begins transaction
// customer = bankQueue.peek()
// bankQueue.dequeue()
// departureTime = currentTime + transaction time in customer
// newDepartureEvent = a new departure event with departureTime
// eventListPQueue.add(newDepartureEvent)
// }
// else
// tellerAvailable = true
}
}