1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <queue>
using namespace std;
struct Customer
{
int currentTime;
int newDepartureEvent;
int departureTime;
int arrivalTime;
int transactionTime;
int customerID;
};
struct Event
{
int newDepartureEvent;
int departureTime;
int customerID;
int transactionTime;
int arrivalTime;
int currentTime;
//Used to compare events and determine highest priority
bool operator < (const Event& right) const
{
return this->arrivalTime < right.arrivalTime;
}
};
void simulate();
void processArrival(int ¤tTime, Event &newEvent, priority_queue<Event> &eventListPQueue, queue<Customer> &bankQueue, bool tellerAvailable);
void processDeparture(Event &newEvent, priority_queue<Event> &eventListPQueue, queue<Customer> &bankQueue, bool tellerAvailable);
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 <Customer> 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 loop
while (!eventListPQueue.empty()) { // Event list is not empty
Event newEvent;
newEvent = eventListPQueue.top();
currentTime = newEvent.arrivalTime; // Get current time
if (newEvent.arrivalTime && newEvent.departureTime == 0) { // is an arrival event
processArrival(currentTime, newEvent, eventListPQueue, bankQueue, tellerAvailable);
}
else
processDeparture(newEvent, eventListPQueue, bankQueue, tellerAvailable);
}
/*
//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
}
void processArrival(int ¤tTime, Event &newEvent, priority_queue<Event> &eventListPQueue, queue<Customer> &bankQueue, bool tellerAvailable)
{
Event grab;
Event event;
Event eventCustomer;
Customer customerIn_line;
eventListPQueue.pop(); // Remove this event from the event list
event.customerID++; // customer = customer referenced in arrivalEvent
if (bankQueue.empty() && tellerAvailable) {
grab.departureTime = currentTime + newEvent.transactionTime; // departureTime = currentTime + transaction time in arrivalEvent
grab.newDepartureEvent = grab.departureTime; // newDepartureEvent = a new departure event with departureTime
eventListPQueue.push(grab); // eventListPQueue.add(newDepartureEvent)
tellerAvailable = false; // tellerAvailable = false
}
else {
customerIn_line.arrivalTime = event.arrivalTime;
customerIn_line.transactionTime = event.transactionTime;
bankQueue.push(customerIn_line); // bankQueue.enqueue(customer)
}
}
//Where problem occurs
void processDeparture(Event &newEvent, priority_queue<Event> &eventListPQueue, queue<Customer> &bankQueue, bool tellerAvailable)
{
eventListPQueue.pop();
Customer customer;
if(!bankQueue.empty())
{
customer = bankQueue.front();
bankQueue.pop();
customer.departureTime = customer.currentTime + customer.transactionTime;
customer.newDepartureEvent = customer.departureTime;
eventListPQueue.push(customer.newDepartureEvent);
}
else
{
tellerAvailable = true;
}
}
|