Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with the buzzer functions that are bolded in c++. #ifndef _SERVICE_QUEUE_H #define _SERVICE_QUEUE_H #include #include #include #include class ServiceQueue { private: /**

I need help with the buzzer functions that are bolded in c++. #ifndef _SERVICE_QUEUE_H #define _SERVICE_QUEUE_H 
#include #include #include #include 
 class ServiceQueue { private: /** Your private data members here! * (you should have NO PUBLIC data members! * * Nested private types also go here. * For example, if your design needs some kind of * structure, you would specify it here. */ std::vector the_queue; std::vector buzzer_bucket; public: /** * Constructor * Description: intializes an empty service queue. * * RUNTIME REQUIREMENT: O(1) * * TODO */ ServiceQueue() { } /** * Destructor * Description: deallocates all memory assciated * with service queue * * RUNTIME REQUIREMENT: O(N_b) where N_b is the number of buzzer * IDs that have been used during the lifetime of the * service queue; in general, at any particular instant * the actual queue length may be less than N_b. * * [See discussion of "re-using buzzers" below] * * TODO */ ~ServiceQueue() { } /** * Function: snapshot() * param: buzzers is an integer vector passed by ref * Description: populates buzzers vector with a "snapshot" * of the queue as a sequence of buzzer IDs * * * RUNTIME REQUIREMENT: O(N) (where N is the current queue * length). */ void snapshot(std::vector &buzzers) { buzzers.clear(); // you don't know the history of the // buzzers vector, so we had better // clear it first. buzzers = the_queue; } /** * Function: length() * Description: returns the current number of * entries in the queue. * * RUNTIME REQUIREMENT: O(1) */ int length() { return the_queue.size(); // placeholder } /** * Function: give_buzzer() * Return: buzzer-ID (integer) assigned to the new customer. * Description: This is the "enqueue" operation. For us * a "buzzer" is represented by an integer (starting * from zero). The function selects an available buzzer * and places a new entry at the end of the service queue * with the selected buzer-ID. * This buzzer ID is returned. * The assigned buzzer-ID is a non-negative integer * with the following properties: * * (1) the buzzer (really it's ID) is not currently * taken -- i.e., not in the queue. (It * may have been in the queue at some previous * time -- i.e., buzzer can be re-used). * This makes sense: you can't give the same * buzzer to two people! * * (2) Reusable Buzzers: A re-usable buzzer is * a buzzer that _was_ in the queue at some previous * time, but currently is not. * * REQUIREMENT: If there is one or more reusable * buzzer, you MUST return one of them; furthermore, * it must be the buzzer that became reusable most * MOST RECENTLY. * * (3) if there are no previously-used / reusable buzzers, * the SMALLEST possible buzzer-ID is used (retrieved from * inventory). * Properties in this situation (where N is the current * queue length): * * - The largest buzzer-ID used so far is N-1 * * - All buzzer-IDs in {0..N-1} are in the queue * (in some order). * * - The next buzzer-ID (from the basement) is N. * * In other words, you can always get more buzzers (from * the basement or something), but you don't fetch an * additional buzzer unless you have to (i.e., no reusable buzzers). * * Comments/Reminders: * * Rule (3) implies that when we start from an empty queue, * the first buzzer-ID will be 0 (zero). * * Rule (2) does NOT require that the _minimum_ reuseable * buzzer-ID be used. If there are multiple reuseable buzzers, * any one of them will do. * * Note the following property: if there are no re-useable * buzzers, the queue contains all buzzers in {0..N-1} where * N is the current queue length (of course, the buzzer IDs * may be in any order.) * * RUNTIME REQUIREMENT: O(1) ON AVERAGE or "AMORTIZED" * In other words, if there have been M calls to * give_buzzer, the total time taken for those * M calls is O(M). * * An individual call may therefore not be O(1) so long * as when taken as a whole they average constant time. * */ int give_buzzer() { return 0; // placeholder } #ifdef _SLOW #include "ServiceQueueSlow.h" std::string SourceFile = "ServiceQueueSlow.h"; #define __IMPL_INCL #endif #ifdef _SLOW2 #ifndef __IMPL_INCL #include "ServiceQueueSlow2.h" std::string SourceFile = "ServiceQueueSlow2.h"; #define __IMPL_INCL #endif #endif #ifndef __IMPL_INCL #include "ServiceQueue.h" std::string SourceFile = "ServiceQueue.h"; #define __IMPL_INCL #endif void display(ServiceQueue &q) { std::vector snap; q.snapshot(snap); std::cout << " [ "; for(int b : snap) { std::cout << b << " "; } std::cout << "] " << std::endl; } /** * takes a full line of user input and * parses it. * * Executes specified command and prints appropriate * message if syntactially correct. * otherwise prints an error message. * * Returns 1 only to indicate the the quite command 'q' was * entered. * Otherwise, 0 is returned. */ int execute_cmd(ServiceQueue &q, char *buf) { int tok; char cmd; int buzzer; char junk[128]; // tok stores number of tokens parsed // Note that all commands have either one or two 'tokens': // example: b 8 // 'b' is the first token and '8' is the 2nd // junk array is used to detect if an extraneous 3rd argument // has been entered (parse error). tok = sscanf(buf, " %c %i %s", &cmd, &buzzer, junk); if(tok==0) return 0; if(tok > 2){ printf(" bad command. try again "); return 0; } switch (cmd) { case 'q': if(tok != 1){ printf(" bad command. try again "); return 0; } else{ printf(" goodbye... "); return 1; } case 'd' : if(tok != 1) printf(" bad command. try again "); else display(q); return 0; case 'l': if(tok != 1) printf(" bad command. try again "); else printf(" len: %i ", q.length()); return 0; case 'g': if(tok != 1) printf(" bad command. try again "); else printf(" buzzer: %i ", q.give_buzzer()); return 0; case 's': if(tok != 1) printf(" bad command. try again "); else{ buzzer = q.seat(); if(buzzer != -1) printf(" seating buzzer: %i ", buzzer); else printf(" sorry, queue is empty "); } return 0; case 'k': if(tok != 2) printf(" bad command. try again "); else { if(q.kick_out(buzzer)) printf(" %i is outta here! ", buzzer); else printf(" could not remove tkt %i! ", buzzer); } return 0; case 'b': if(tok != 2) printf(" bad command. try again "); else { if(q.take_bribe( buzzer)) printf(" VIP coming through! "); else printf(" Get in line, then bribe me! "); } return 0; default: printf(" bad command. try again "); return 0; } } int main() { ServiceQueue q; char buf[128]; int done = 0; printf(" Welcome to the simple service-queue interactive program "); printf(" (SOURCE FILE USED FOR ServiceQueue CLASS: %s) ", SourceFile.c_str()); printf(" An empty service queue has been created for you "); printf(" Commands: "); printf(" d : display queue "); printf(" l : report length of queue "); printf(" g : give out a buzzer "); printf(" s : serve the first buzzer in line "); printf(" k : kick specified buzzer out! "); printf(" b : take a bribe to move specified buzzer to front! "); printf(" q : quit "); printf("----------------------------------- "); while(!done) { printf("cmd > "); if(fgets(buf, 127, stdin) != NULL) { if(execute_cmd(q, buf)) done = 1; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2016 Riva Del Garda Italy September 19 23 2016 Proceedings Part 3 Lnai 9853

Authors: Bettina Berendt ,Bjorn Bringmann ,Elisa Fromont ,Gemma Garriga ,Pauli Miettinen ,Nikolaj Tatti ,Volker Tresp

1st Edition

3319461303, 978-3319461304

More Books

Students also viewed these Databases questions

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago