#include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iomanip> #include <cmath> #include <ctype.h> using namespace std; const int MAX = 50; void get_names(string[], int&); void print_hat(string[], int); void secret_santa(int*, bool*, int); void gift_to(string[], int, int*); void print_ascii(); int main() { string hat[MAX]; string temp; int count = 0; string x; get_names(hat, count); print_ascii(); if(count > 1) { print_hat(hat, count); cout << "Hit enter to begin sort!" << endl; getline(cin, x); int * givesTo = new int [count]; // recipient of gift for(int i = 0; i < count; i++) givesTo[i] = -1; bool * assigned = new bool [count]; // assigned: T or F for(int i = 0; i < count; i++) // init to false assigned[i] = false; print_ascii(); secret_santa(givesTo, assigned, count); gift_to(hat, count, givesTo); } else cout << "There are not enough participants" << endl; return 0; } void get_names(string hat[], int &count) { bool moreNames = true; string temp; // add names to hat while(moreNames) { print_ascii(); print_ascii(); // prompt for input cout << "Enter your name to be put in the hat!" << endl; cout << "Or hit enter when you're done" << endl << endl; cout << "New Name: "; getline(cin, temp); // add to list and increment count if(!temp.empty()) { hat[count] = temp; count++; } // finish adding to list else { moreNames = false; cout << endl; } } } void print_hat(string hat[], int count) { // print numbers and corresponding participants cout << "Secret Santa Participants" << endl; for(int i = 0; i < count; i++) cout << i << ". " << hat[i] << endl; } void secret_santa(int * givesTo, bool * assigned, int count) { int cnt = 0; print_ascii(); // initialize srand to time (randomize) srand(time(NULL)); // assign secret santas for(int i = 0; i < count; i++) { // do until assigned while(givesTo[i] == -1) { // redo if last participant has no suitor if((i == count-1) && assigned[i] == false) { //reinitialize arrays for(int j = 0; j < count; j++) { givesTo[j] = -1; assigned[j] = false; } // i will become 0 at the end i = -1; } // find someone to pair else { // find random number in range int temp = rand() % (count); // if number not itself and not already assigned if(temp != i && assigned[temp] == false) { // assign number givesTo[i] = temp; // mark number to not be used again assigned[temp] = true; } } } } } void gift_to(string hat[], int count, int * givesTo) { string temp; int num = -1; int assign; string again; bool exit = false; while(!exit) { print_ascii(); print_ascii(); // prompt and print list cout << "Enter only your number to see who you got." << endl << endl; cout << "type 'exit' to complete. (Warning: results are lost after exit)" << endl; print_hat(hat, count); cout << endl << "Number: "; getline(cin, temp); // if enter with no input, do nothing if(!temp.empty()) { // verify all digits if(isdigit(temp[0])) num = stoi(temp); // exit program if(temp == "exit") exit = true; // if num is out of range else if(num < 0 || num >= count) { print_ascii(); cout << "Invalid number. Press Enter to try again." << endl; getline(cin, again); } // if num is valid else { print_ascii(); // print requested secret-santa result cout << hat[num] << "," << endl << endl; assign = givesTo[num]; cout << "You are " << hat[assign] << "'s Secret Santa!" << endl; cout << endl; // hit enter to make new request cout << "Press enter to select a new name." << endl; getline(cin, again); } // reinitialize num = -1; } } } void print_ascii() { cout << "\033[2J\033[1;1H"; cout << "*****************************************" << endl; cout << "* *" << endl; cout << "* $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ *" << endl; cout << "* $ $ $ $ $ $ $ *" << endl; cout << "* $$$$$ $$$$$ $ $$$$$ $$$$$ $ *" << endl; cout << "* $ $ $ $ $ $ $ *" << endl; cout << "* $$$$$ $$$$$ $$$$$ $ $ $$$$$ $ *" << endl; cout << "* $$$$$ $$$$$ $ $ $$$$$ $$$$$ *" << endl; cout << "* $ $ $ $$ $ $ $ $ *" << endl; cout << "* $$$$$ $$$$$ $ $ $ $ $$$$$ *" << endl; cout << "* $ $ $ $ $$ $ $ $ *" << endl; cout << "* $$$$$ $ $ $ $ $ $ $ *" << endl; cout << "* *" << endl; cout << "*****************************************" << endl; cout << endl; }
Thursday, November 20, 2014
'Tis the Season: A highly unoptimized Secret Santa C++ Program!
Labels:
programming
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment