Files
ProjectEulerCS/ProjectEulerCS/ProblemSelection.cs
2020-08-23 14:39:45 -04:00

92 lines
3.2 KiB
C#

//ProjectEuler/ProjectEulerCS/src/ProblemSelection.cs
//Matthew Ellison
// Created: 08-21-20
//Modified: 08-21-20
//This class holds all of the functions needed to handle a problem
/*
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 <https://www.gnu.org/licenses/>.
*/
using System.Collections.Generic;
using ProjectEulerCS.Problems;
namespace ProjectEulerCS{
public class ProblemSelection{
//Holds the valid problem numbers
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
{0, 1, 2, 3, 4, 5, 6};
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
get { return _PROBLEM_NUMBERS; }
}
//Returns the problem corresponding to the given problem number
public static Problem getProblem(int problemNumber){
Problem problem = null;
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;
}
return problem;
}
//Print the description of a problem
public static void printDescription(int problemNumber){
//Get the problem
Problem problem = getProblem(problemNumber);
//Print the problem's description
System.Console.WriteLine(problem.description);
}
//Solve a problem
public static void solveProblem(int problemNumber){
//Get the problem
Problem problem = getProblem(problemNumber);
//Print the problem description
System.Console.WriteLine(problem.description);
//Solve the problem
problem.solve();
//Print the results
System.Console.WriteLine(problem.result + "\nIt took " + problem.time + " to solve this problem.\n\n");
}
//Get a valid problem number from a user
public static int getProblemNumber(){
int problemNumber = 0;
System.Console.Write("Enter a problem number: ");
string problemNumberString = System.Console.ReadLine();
problemNumber = System.Convert.ToInt32(problemNumberString);
while(_PROBLEM_NUMBERS.IndexOf(problemNumber) == -1){
System.Console.Write("That is an invalid problem number!\nEnter a problem number: ");
problemNumberString = System.Console.ReadLine();
problemNumber = System.Convert.ToInt32(problemNumberString);
}
return problemNumber;
}
//List all valid problem numbers
public static void listProblems(){
System.Console.Write(_PROBLEM_NUMBERS[1]);
for(int problemNumber = 2; problemNumber < _PROBLEM_NUMBERS.Count; ++problemNumber){
System.Console.Write(", " + _PROBLEM_NUMBERS[problemNumber]);
}
System.Console.WriteLine();
}
}
}