Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <sys\time.h>
- #include <math.h>
- #include <queue.h>
- typedef struct customer
- {
- unsigned int transactionTime;
- unsigned int startTimeInLine;
- unsigned int endTimeInLine;
- };
- void initTimeClock(void);
- struct timeval t1, t2;
- static double elapsedTime;
- static int time = 0;
- static int customerWait = 0;
- static int customerNumber = 0;
- int main(int argc, char *argv[]) {
- printf("Bank Opened");
- printf("\n");
- initTimeClock();
- while ((int) elapsedTime < 12000) {
- printf("Hours, %f \n", elapsedTime);
- updateTimeClock();
- generateCustomer();
- }
- printf("Customers Served Today: %f \n", customerLine.size());
- printf("Bank Closed");
- printf("\n");
- }
- void initTimeClock(void) {
- // start timer
- gettimeofday(&t1, NULL);
- //create queue
- queue<int> customerLine;
- }
- void updateTimeClock(void) {
- // stop timer
- gettimeofday(&t2, NULL);
- // compute and print the elapsed time in millisec
- elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
- elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
- }
- double rand_normal(double mean, double stddev) { //Box muller method
- static double n2 = 0.0;
- static int n2_cached = 0;
- if (!n2_cached) {
- double x, y, r;
- do {
- x = 2.0 * rand() / RAND_MAX - 1;
- y = 2.0 * rand() / RAND_MAX - 1;
- r = x * x + y * y;
- } while (r == 0.0 || r > 1.0);
- {
- double d = sqrt(-2.0 * log(r) / r);
- double n1 = x * d;
- n2 = y * d;
- double result = n1 * stddev + mean;
- n2_cached = 1;
- return result;
- }
- } else {
- n2_cached = 0;
- return n2 * stddev + mean;
- }
- }
- void generateCustomer(void) {
- customerNumber++;
- //customerWait = rand() % 400 + 100;
- customerWait = rand_normal(250, 50);
- delay(customerWait);
- printf("Customer Number: , %d "
- "\n", customerNumber);
- printf("Customer created, %d "
- "\n", customerWait);
- //customer newCustomer = {0, elapsedTime, 0};
- customerLine.push(elapsedTime);
- updateTimeClock();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement