// William Orem // 6/15/2012 // Assignment #3 UCFmart // formula 40 + 6(9) = 94 seconds #include <stdio.h> #include <stdlib.h> #include <time.h>//data types struct node { char name[30]; int time; int end; int numItems; struct node* next; }; struct queue { int size; int wait; struct node* front; struct node* back; }; struct bigQ { struct queue* A; struct queue* B; struct queue* C; }; #define DEBUG 0 //prototypes void getTime(int seconds); void initQueue(struct queue* newqueue); void enqueue(struct queue* myQ, struct node* newnode); void pre_proccess(struct bigQ* mybigQ, int time); void print_queue(struct node* myNode); struct node* dequeue(struct queue* myQ); int isEmptyQ(struct queue* myQ); int isFullQ(struct queue* myQ); int main() { FILE* ifp = fopen("customer.txt", "r"); int i,numDays; fscanf(ifp,"%d",&numDays); for(i=0;i time, newnode->name, &newnode->numItems); newnode->next=NULL; //check to see if anyone needs to be dequeued pre_proccess(&mybigQ,newnode->time); //check to see if we have full queues if(isFullQ(mybigQ.A) && isFullQ(mybigQ.B) && isFullQ(mybigQ.C)) { //error all lines full and need to enqueue printf("%d is an INVALID DAY! all queues full!\n\a",i); } //pick a queue and stick someone in it char Q; if(!isFullQ(mybigQ.A) && newnode->numItems<=10) { enqueue(mybigQ.A, newnode); Q='A'; } else if(isFullQ(mybigQ.B) && isFullQ(mybigQ.C)) { printf("%d is an INVALID DAY! queues B&C full!\n\a",i); } else if((!isFullQ(mybigQ.B) && nextQ=='B')||(isFullQ(mybigQ.C) && nextQ=='C')) { enqueue(mybigQ.B, newnode); Q='B'; nextQ='C'; } else //if(!isFullQ(mybigQ.C) && nextQ=='C') { enqueue(mybigQ.C, newnode); Q='C'; nextQ='B'; } getTime(newnode->time); printf("Customer %s checking in line %c.\n",newnode->name,Q); } pre_proccess(&mybigQ,39600); //end of day if(!isEmptyQ(mybigQ.B) || !isEmptyQ(mybigQ.C)) printf("Invalid day!!! people in line past midnight!\n\a"); printf("\n\n"); } return 0; } //precondition: recieves a bigQ struct and a int seconds. removes in order any nodes before that time //postcondition: prints info and dequeues nodes frees node. void pre_proccess(struct bigQ* myQ, int time) { int done=0; while(!done){ done=1; char Q; struct queue* next = NULL; //big ol' if block to check if a queue has stuff in it and if its next out if(!isEmptyQ(myQ->A) && (myQ->A->front->end + myQ->A->front->time) < time) { next = myQ->A; Q='A'; } if(next){ if(!isEmptyQ(myQ->B) && (myQ->B->front->end) < next->front->end) { next = myQ->B; Q='B'; } } else { if(!isEmptyQ(myQ->B) && (myQ->B->front->end + myQ->B->front->time) < time) { next = myQ->B; Q='B'; } } if(next){ if(!isEmptyQ(myQ->C) && (myQ->C->front->end) < next->front->end) { next = myQ->C; Q='C'; } } else { if(!isEmptyQ(myQ->C) && (myQ->C->front->end+ myQ->C->front->time) < time) { next = myQ->C; Q='C'; } } if(next){ //print node and remove from queue struct node* tmp = dequeue(next); if(tmp->end+tmp->time>=39600) { printf("Invalid out time!!\n"); } else { getTime(tmp->end+tmp->time); printf("Customer %s checking out of line %c waited %d seconds.\n",tmp->name,Q,tmp->end); } //free mem free(tmp); done=0; } } } //precondition: recieves a queue struct * and removes the first node. //postcondition: returns a pointer to dequed node. struct node* dequeue(struct queue* myQ) { struct node* tmp; // Check the empty case if (myQ->front == NULL) return myQ->front; // Set up a temporary pointer to use to free the memory for this node tmp = myQ->front; // Make front point to the next node in the queue myQ->front = myQ->front->next; // If deleting this node makes the queue empty, we have to change the back if (myQ->front == NULL) myQ->back = NULL; //calculate wait time for q myQ->wait -= (40 + 6 * tmp->numItems); //decrement q myQ->size--; // Return the value that just got dequeued. return tmp; } //precondition: recieves a queue* and a node*. adds node to front of queue void enqueue(struct queue *myQ, struct node* newnode) { // If the queue is NOT empty, we must set the old "last" node to point // to this newly created node. if (myQ->back != NULL) myQ->back->next = newnode; // Now, we must reset the back of the queue to our newly created node. myQ->back = newnode; // If the queue was previously empty we must ALSO set the front of the // queue. if (myQ->front == NULL) myQ->front = newnode; if(isEmptyQ(myQ)) { myQ->wait = newnode->time+(40 + 6 * newnode->numItems); newnode->end = myQ->wait - newnode->time; } else { myQ->wait += (40 + 6 * newnode->numItems); newnode->end = myQ->wait - newnode->time; } myQ->size++; } //precondition: print a queue void print_queue(struct node* myNode) { while(myNode!=NULL) { printf("%s\n",myNode->name); myNode=myNode->next; } } int isEmptyQ(struct queue* myQ) { if(myQ->size==0) return 1; return 0; } int isFullQ(struct queue* myQ) { if(myQ->size==8) return 1; return 0; } //precondition: initialize a queue void initQueue(struct queue* newqueue) { newqueue->size=0; newqueue->wait=0; newqueue->front=NULL; newqueue->back=NULL; } //precondition: prints out the time from 1pm given a number of int seconds void getTime(int seconds) { int hour,min,sec; sec = seconds%60; min = (seconds/60)%60; hour = 1+(seconds/3600)%12; printf("%d:%02d:%02dpm: ",hour,min,sec); }
UCF Mart assignment #3 (COP3502)
Subscribe to:
Posts (Atom)
No comments:
Post a Comment