Created
April 18, 2014 01:53
-
-
Save powerc9000/11020917 to your computer and use it in GitHub Desktop.
Lab 14 is CS1415 I have to tutor and it will be nice to have the code for people to look at
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Clay Murray | |
//CS 1415 | |
//A cool bank simulation with a queue | |
#include <iostream> | |
#include <queue> | |
#include <stdlib.h> | |
#include <time.h> | |
using namespace std; | |
int randomWait(); | |
//Main runs the simulation | |
int main(){ | |
int runtime; //Amount of time the business day lasts | |
int currentTime = 0; //Current Time in minutes | |
int nextArival; | |
int serviceEndTime = 0; | |
int longestWait = 0; | |
int maxLineLength = 0; | |
int waitTime; | |
int customers = 0; | |
bool dayOver = false; | |
bool customerBeingServiced = false; | |
bool firstCustomer = true; | |
srand(time(0)); | |
queue<int> line; | |
cout<<"This program simulates a queue at a bank\n\n"; | |
cout<<"How long to run simulation? (480 = 8 hours): "; | |
cin>>runtime; | |
//Time at which the first person arrives | |
//and when they will finish being serviced | |
nextArival = randomWait(); | |
//If the day isn't over or we still have someone being helped | |
while(!dayOver || customerBeingServiced){ | |
//If the next arival is due put them in line | |
//Unless the day is over. If the day is over dont allow new people in line | |
if(currentTime >= nextArival && !dayOver){ | |
line.push(currentTime); | |
customers++; | |
nextArival = randomWait() + currentTime; | |
//Output a differnt message for the first customer arriving | |
if(!firstCustomer){ | |
cout<<currentTime<<": A customer has arrived\n"; | |
}else{ | |
cout<<currentTime<<": The first customer has arrived\n"; | |
firstCustomer = false; | |
} | |
} | |
//If the service time for a customer is up and there is currently a customer at the window | |
//Shoo them away and say they are finished | |
//We do this first or else there will be a minute wait sometimes between when a customer is done | |
//and when we get a new one | |
//Causing the simulation to run a lot longer | |
if(currentTime >= serviceEndTime && customerBeingServiced){ | |
customerBeingServiced = false; | |
cout<<currentTime<<": A customer finshed being serviced\n"; | |
} | |
//If there is no customer and the window and the line isn't empty | |
//Get the next customer in line and see how long he/she has been waiting | |
if(!customerBeingServiced && !line.empty()){ | |
customerBeingServiced = true; | |
waitTime = currentTime - line.front(); | |
serviceEndTime = currentTime + randomWait(); | |
line.pop(); | |
if(waitTime > longestWait){ | |
longestWait = waitTime; | |
} | |
} | |
//Check to see the longest legnth of the line | |
if(line.size() > maxLineLength){ | |
maxLineLength = line.size(); | |
} | |
//if we are at closing time set day over to true so we can service the remaining people in line | |
//but not allow anymore in | |
if(currentTime >= runtime){ | |
dayOver = true; | |
} | |
currentTime++; | |
} | |
cout<<"longest wait: "<<longestWait<<"\n"; | |
cout<<"Max line length: "<<maxLineLength<<"\n"; | |
cout<<"Total customers: "<<customers<<"\n"; | |
} | |
//It's just helpful to not have to write out random % 4 everytime | |
int randomWait(){ | |
return (random() % 4) + 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment