Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
5.9 kB
8
Indexable
Never
Program:

#include <bits/stdc++.h>
#include <stdlib.h>
#include <ctime>
using namespace std;


// display the contents of the queues
void displayQueue(queue<string> que, queue<int> r) {
  int i = 0;
  while(!que.empty() && !r.empty()) {
    cout << "(" << i << ") " << que.front() << "  " << r.front() << endl;
    que.pop();
    r.pop();
    i++;
  }
  cout << endl;
}


/** function to search the name in queue
 * returns records associated with the name
 */
int searchName(queue<string> line, string name)
{
  int count = 0;
  while(!line.empty())
    {
      count++;
      if(line.front() == name)
  {
    return count;
  }
      line.pop();
    }
  return 0;
}


/** 
 * function to search and return the name of person having raffle number as num
 */
string searchRaffle(queue<int>raffle, int num, queue<string> line) {
  while(!raffle.empty()) {
    if(raffle.front() == num) {
      return line.front();
    }
    raffle.pop();
    line.pop();
  }


  // ***in case raffle number != num for any person, return empty string *** 
  return "";
}


int main() {
  int choice;
  // queue to store the names
  queue<string> line;
  // queue to store the raffle number
  queue<int>raffle;
  int last_raffle;


  srand(time(0));
  while(true) {
    cout << "Snow's BBQ Menu" << endl << endl;
    cout << "1. Add a Name" << endl;
    cout << "2. Make an Order" << endl;
    cout << "3. Count Names" << endl;
    cout << "4. Draw Raffle Winner" << endl;
    cout << "5. Display Line" << endl;
    cout << "6. Help your BFF" << endl;
    cout << "7. Exit Menu" << endl << endl;
    cout << "Choose your Option: ";
    cin >> choice;
    switch(choice) {


      case 1: {
        string name;
        cout << "Enter your name: ";
        cin >> name;
        line.push(name);
        last_raffle = 1 +(rand() % 500);
        raffle.push(last_raffle);
        cout << "Welcome! Your raffle number is " << last_raffle << endl;
        break;
      }
      case 2: {
        cout << line.front() << ", you can order now!" << endl;
        //as the order of first person in line processed remove that person from line
        line.pop();
        raffle.pop();
        break;
      }
      case 3: {
        cout << "Number of people in the line: " << line.size() << endl;
        break;
      }
      case 4: {
        int num =((rand() % line.size()) + 1) * 10;
        cout << "The winning raffle number is " << num << "." << endl;
        
        string winner = searchRaffle(raffle, num, line);
        
        cout << endl << winner << ", please choose what do you want:" << endl;
        cout << "> 1.  To move to the front of the line, or" << endl;
        cout << "> 2.  A free t-shirt" << endl << endl;
        cout << " Choose your option: ";
        int option;
        cin >> option;


        // if selected to move to first in line
        if(option == 1) {
          // find the persons in front of the winner
          int count = searchName(line, winner);
          if(count != 0) {
            // create two new temporary queues
            queue<string> names;
            queue<int>id;
            string friendName;
            int friendId;
            int size = line.size();
  
            // move the data from one queue to another
            for(int j = 0; j < size; j++) {
              if(j == count - 1) {
                friendName = line.front();
                friendId = raffle.front();
              } else {
                names.push(line.front());
                id.push(raffle.front());
              }
              line.pop();
              raffle.pop();
            }


            // add the winner as the first in old queues
            line.push(friendName);
            raffle.push(friendId);
            
            //add remaining persons in the both old queues
            for(int j = 0; j < size - 1; j++) {
              line.push(names.front());
              raffle.push(id.front());
              names.pop();
              id.pop();
            }
            cout << endl << winner << ", you are being moved to the front of line!" << endl << endl;
          }
        } else {      // else give free t-shirt
          cout << "You have won a free t-shirt" << endl << endl;
        }
        break;
      }
      case 5: {
        displayQueue(line, raffle);
        break;
      }
      case 6: {
        string bffName;
        cout << "Please input the name of your BFF: ";
        cin >> bffName;
        
        // the number of persons ahead of the bff including bff
        int count = searchName(line, bffName);
        
        if(count != 0) {
          queue<string> names;
          queue<int>id;
          string friendName;
          int friendId;
          int size = line.size();
          
          // move the data from old queues to new queues
          for(int j = 0; j < size; j++) {
            if(j == count - 1) {
              friendName = line.front();
              friendId = raffle.front();
            } else {
              names.push(line.front());
              id.push(raffle.front());
            }
            line.pop();
            raffle.pop();
          }


          // add the friend as first person in old queue
          line.push(friendName);
          raffle.push(friendId);
          //transfering the data from new queues to old queues
          for(int j = 0; j < size - 1; j++) {
            line.push(names.front());
            raffle.push(id.front());
            names.pop();
            id.pop();
          }
          cout << endl << bffName << ", you are being moved to the front of line!" << endl << endl;
        } else {
          cout << endl << "The person with name '" << bffName << "' is not in the line at this time." << endl << endl;
        }
        break;
      }
      case 7: {
        cout << "Bye!!" << endl;
        break;
      }
    }
  }
  return 0;
}