//ProjectEuler/ProjectEulerCPP/Headers/Problem.hpp //Matthew Ellison // Created: 07-04-19 //Modified: 07-09-20 //This is an abstract base class to allow polymorphism for the individual problems /* Copyright (C) 2020 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #ifndef PROBLEM_HPP #define PROBLEM_HPP #include #include #include "Stopwatch.hpp" class Problem{ protected: const std::string description; //Holds the description of the problem mee::Stopwatch timer; //Used to determine your algorithm's run time bool solved; //Holds true after the problem has been solved class Unsolved{}; //An exception class thrown if you try to access something before it has been solved std::stringstream result; //Get the result of the problem public: //Constructors Problem() : solved(false){ } Problem(std::string description) : description(description), solved(false){ } //Destructor virtual ~Problem(){ } //Gets virtual std::string getDescription() const{ return description; } //Get the time it took to run the algorithm as a string virtual std::string getTime(){ //If the problem hasn't been solved throw an exception if(!solved){ throw Unsolved(); } return timer.getStr(); } //Get a copy of the timer that was used to time the algorithm virtual mee::Stopwatch getTimer(){ //If the problem hasn't been solved throw an exception if(!solved){ throw Unsolved(); } return timer; } //Reset the problem so it can be run again virtual void reset(){ result.str(std::string()); timer.reset(); solved = false; } //Return a string with the solution to the problem virtual std::string getResults(){ //If the problem hasn't been solved throw an exception if(!solved){ throw Unsolved(); } return result.str(); } //Pure virtual functions virtual void solve() = 0; //Solve the problem }; #endif //PROBLEM_HPP