//ProjectEuler/C++/main.cpp //Matthew Ellison // Created: 07-04-19 //Modified: 07-08-19 //This is a driver function for all Project Euler problems //It prompts the user for an action (state the problem or solve the problem), then executes the appropriate command on the appropriate problem #include #include #include "Algorithms.hpp" #include "Headers/Problem.hpp" #include "Headers/Problem1.hpp" #include "Headers/Problem2.hpp" #include "Headers/Problem3.hpp" #include "Headers/Problem4.hpp" #include "Headers/Problem5.hpp" #include "Headers/Problem6.hpp" #include "Headers/Problem7.hpp" #include "Headers/Problem8.hpp" #include "Headers/Problem9.hpp" #include "Headers/Problem10.hpp" #include "Headers/Problem11.hpp" #include "Headers/Problem12.hpp" #include "Headers/Problem13.hpp" #include "Headers/Problem14.hpp" #include "Headers/Problem15.hpp" #include "Headers/Problem16.hpp" #include "Headers/Problem17.hpp" #include "Headers/Problem18.hpp" #include "Headers/Problem19.hpp" #include "Headers/Problem20.hpp" #include "Headers/Problem21.hpp" #include "Headers/Problem22.hpp" #include "Headers/Problem23.hpp" #include "Headers/Problem24.hpp" #include "Headers/Problem25.hpp" #include "Headers/Problem26.hpp" #include "Headers/Problem27.hpp" #include "Headers/Problem28.hpp" #include "Headers/Problem29.hpp" #include "Headers/Problem30.hpp" #include "Headers/Problem67.hpp" //Some helper functions to help with the menus void printMenu(); //Prints the menu in the terminal int getMenuSelection(); //Returns a valid menu selection from the user bool isValidMenu(int selection); //Returns true if the int passed into it is a valid menu selection void solveMenu(); //Prints the menu for solving a problem and handles how the problem is handed off Problem* getProblem(unsigned int problemNumber); //Takes a problem's number and returns a pointer to the appropriate class void solveProblem(Problem* problem); //Handles how the program solves the problem void descriptionMenu(); //Handles how the program reacts to printing the problem's description void printDescription(Problem* problem); //Prints out the description of the problem passed to it unsigned int getProblemNumber(); //A helper function to error check the problem number input for solving and describing problems void listProblems(); //Lists the problem numbers that you can choose //Setup the menu options enum MenuOptions {SOLVE = 1, DESCRIPTION, LIST, EXIT, SIZE}; //Setup the problem numbers std::vector PROBLEM_NUMBERS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 67}; int main(){ int selection = 0; //Holds the menu selection of the user do{ //Print the menu and prompt the user to select an action printMenu(); selection = getMenuSelection(); switch(selection){ case SOLVE : solveMenu(); break; case DESCRIPTION : descriptionMenu(); break; case LIST : listProblems(); break; case EXIT : break; } }while(selection != EXIT); return 0; } void printMenu(){ std::cout << "1. Solve a problem\n" << "2. Print a problem description\n" << "3. List valid problem numbers\n" << "4. Exit" << std::endl; } int getMenuSelection(){ int selection = 0; std::cin >> selection; while(std::cin.fail() || !isValidMenu(selection)){ std::cout << "That is an invalid option!\nPress Enter to continue" << std::endl; std::cin.clear(); std::cin.get(); printMenu(); std::cin >> selection; } return selection; } bool isValidMenu(int selection){ if((selection > 0) && (selection < MenuOptions::SIZE)){ return true; } else{ return false; } } void solveMenu(){ unsigned int problemNumber = getProblemNumber(); Problem* problem = nullptr; //This selection solves all problems in order if(problemNumber == 0){ //Solve to every valid problem number, skipping over 0 for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ //Generate the current problem problem = getProblem(PROBLEM_NUMBERS.at(problemLocation)); //Solve the problem std::cout << PROBLEM_NUMBERS.at(problemLocation) << ". "; solveProblem(problem); //Release the memory delete problem; //Safeguard problem = nullptr; } } //This is if a single problem number was chosen else{ //Generate the problem problem = getProblem(problemNumber); //Solve the problem solveProblem(problem); //Release the memory delete problem; //Just an overprotective safeguard problem = nullptr; } } //This function returns a pointer to a problem of type number Problem* getProblem(unsigned int problemNumber){ Problem* problem = nullptr; //Holds the problem we are about to create //Decide which problem was asked for and create it switch(problemNumber){ case 1 : problem = new Problem1; break; case 2 : problem = new Problem2; break; case 3 : problem = new Problem3; break; case 4 : problem = new Problem4; break; case 5 : problem = new Problem5; break; case 6 : problem = new Problem6; break; case 7 : problem = new Problem7; break; case 8 : problem = new Problem8; break; case 9 : problem = new Problem9; break; case 10 : problem = new Problem10; break; case 11 : problem = new Problem11; break; case 12 : problem = new Problem12; break; case 13 : problem = new Problem13; break; case 14 : problem = new Problem14; break; case 15 : problem = new Problem15; break; case 16 : problem = new Problem16; break; case 17 : problem = new Problem17; break; case 18 : problem = new Problem18; break; case 19 : problem = new Problem19; break; case 20 : problem = new Problem20; break; case 21 : problem = new Problem21; break; case 22 : problem = new Problem22; break; case 23 : problem = new Problem23; break; case 24 : problem = new Problem24; break; case 25 : problem = new Problem25; break; case 26 : problem = new Problem26; break; case 27 : problem = new Problem27; break; case 28 : problem = new Problem28; break; case 29 : problem = new Problem29; break; case 30 : problem = new Problem30; break; case 67 : problem = new Problem67; break; } //Return the newly created problem return problem; } void solveProblem(Problem* problem){ //Print the problem description printDescription(problem); //Solve the problem problem->solve(); //Print the results std::cout << problem->getString() << "\nIt took " << problem->getTime() << " to solve this problem.\n\n" << std::endl; } void descriptionMenu(){ Problem* problem = nullptr; //Holds the problem that will be generated std::cout << "\n\n"; //Give some extra space to print the description //Get the problem number unsigned int problemNumber = getProblemNumber(); //If the problem number is 0 print out all descriptions if(problemNumber == 0){ //Print description for every valid problem number, skipping over 0 for(unsigned int problemLocation = PROBLEM_NUMBERS[1];problemLocation < PROBLEM_NUMBERS.size();++problemLocation){ //Generate the problem problem = getProblem(PROBLEM_NUMBERS.at(problemLocation)); //Print the problem's description std::cout << PROBLEM_NUMBERS.at(problemLocation) << ". "; printDescription(problem); std::cout << '\n'; //Release the memory delete problem; //Safeguard problem = nullptr; } } //Otherwise print out a single problem's description else{ //Generate the problem problem = getProblem(problemNumber); //Print the problem's description printDescription(problem); //Release the memory delete problem; //Safeguard problem = nullptr; } } void printDescription(Problem* problem){ std::cout << problem->getDescription() << '\n'; } unsigned int getProblemNumber(){ unsigned int problemNumber = 0; std::cout << "Enter a problem number: "; std::cin >> problemNumber; while(!mee::isFound(PROBLEM_NUMBERS, problemNumber) || std::cin.fail()){ std::cout << "That is an invalid problem number!\nEnter a problem number: "; std::cin.clear(); std::cin >> problemNumber; } return problemNumber; } void listProblems(){ std::cout << PROBLEM_NUMBERS[1]; for(unsigned int problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){ std::cout << ", " << PROBLEM_NUMBERS[problemNumber]; } std::cout << std::endl; }