//This is a demonstration of a simple simulation. //Error handling has been omitted for clarity. // //It should be noted that due to the omission of code for clarity's sake, //the very first customer will be lost. In actuality, you would want to //implement a special case for the first customer. #include #include #include"generator.h" using namespace std; struct customer { int transactions; int entrytime; }; int main() { const int SimDuration=10; //Number of units of time to run simulation for. const int MeanCustomerArrivalRate=5; //mean arrival customers/unit of time. const int MeanTransactionRate=2; //mean # of transaction/customer const int TellerProcessingRate=8; //Number of transactions/unit time that a teller can perform. queue line; generator entrance(MeanCustomerArrivalRate); generator transactions(MeanTransactionRate); int CurrentTime=0; int NewCustomers, TotalCustomersServiced=-1; int AvailTellerTransactions, TotalTransactions=0; customer c, CurrentCustomer={0,0}; //start the simulation loop: while(CurrentTime0) { //generate an individual customer: c.entrytime=CurrentTime; c.transactions=transactions.GetNumberOfEvents(); cout << "Customer entering with " << c.transactions << " transactions" << endl; line.push(c); NewCustomers--; }; } //service transactions: AvailTellerTransactions=TellerProcessingRate; //amount of trans avail for this time interval. while(AvailTellerTransactions>0 && !line.empty()) { //perform the transactions that we can: if(CurrentCustomer.transactions>0) { if(CurrentCustomer.transactions > AvailTellerTransactions) { //this customer will exhaust all avail teller transactions: TotalTransactions+=AvailTellerTransactions; CurrentCustomer.transactions-=AvailTellerTransactions; AvailTellerTransactions=0; } else { //this customer can be handled in this time slot: TotalTransactions+=CurrentCustomer.transactions; AvailTellerTransactions-=CurrentCustomer.transactions; CurrentCustomer.transactions=0; } } //Remove the customer and check to see if we need to get a new customer: if(0==CurrentCustomer.transactions) { cout << "Customer leaving, time spent="<< CurrentTime-line.front().entrytime << endl; line.pop(); //actually, we loose the very first customer. TotalCustomersServiced++; if(!line.empty()) { //Get the next customer: CurrentCustomer.transactions=(line.front()).transactions; CurrentCustomer.entrytime=(line.front()).entrytime; } } } //update the time: CurrentTime++; } //Print a few rudimentary stats: cout << TotalCustomersServiced << " customers, " << TotalTransactions << " transactions, " << CurrentTime << " units of time." << endl; return(0); }