mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
302 lines
8.9 KiB
Rust
302 lines
8.9 KiB
Rust
//ProjectEulerRust/src/main.rs
|
|
//Matthew Ellison
|
|
// Created: 06-11-20
|
|
//Modified: 06-15-20
|
|
//This is a driver function for the Project Euler solutions in Rust
|
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses
|
|
/*
|
|
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/>.
|
|
*/
|
|
|
|
|
|
#![allow(non_snake_case)]
|
|
#![allow(unused_parens)]
|
|
#![allow(non_upper_case_globals)]
|
|
|
|
|
|
mod Problems;
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE}
|
|
|
|
static problemNumbers: [u32; 18] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
|
10, 11, 12, 13, 14, 15, 16, 17];
|
|
|
|
fn main(){
|
|
let mut selection = Selections::EMPTY;
|
|
while(selection != Selections::EXIT){
|
|
printMenu();
|
|
selection = getMenuSelection();
|
|
|
|
if(selection == Selections::SOLVE){
|
|
solveMenu();
|
|
}
|
|
else if(selection == Selections::DESCRIPTION){
|
|
descriptionMenu();
|
|
}
|
|
else if(selection == Selections::LIST){
|
|
listProblems();
|
|
}
|
|
else if(selection == Selections::EXIT){
|
|
|
|
}
|
|
else{
|
|
printErrorMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn printMenu(){
|
|
println!("{}. Solve a problem", Selections::SOLVE as i32);
|
|
println!("{}. Print a problem description", Selections::DESCRIPTION as i32);
|
|
println!("{}. List valid problem numbers", Selections::LIST as i32);
|
|
println!("{}. Exit\n", Selections::EXIT as i32);
|
|
}
|
|
|
|
fn getMenuSelection() -> Selections{
|
|
//Setup reading from the console
|
|
let mut selectionStr = String::new();
|
|
|
|
std::io::stdin().read_line(&mut selectionStr).ok();
|
|
//Convert the read to an int
|
|
let mut selection = selectionStr.trim().parse::<i32>().unwrap();
|
|
while(!isValidMenu(selection)){
|
|
println!("That is an invalid option!");
|
|
printMenu();
|
|
std::io::stdin().read_line(&mut selectionStr).ok();
|
|
selection = selectionStr.trim().parse::<i32>().unwrap();
|
|
}
|
|
return getSelection(selection);
|
|
}
|
|
fn isValidMenu(selection: i32) -> bool{
|
|
if((selection > 0) && (selection < Selections::SIZE as i32)){
|
|
return true;
|
|
}
|
|
else{
|
|
return false;
|
|
}
|
|
}
|
|
fn getSelection(selection: i32) -> Selections{
|
|
if(selection == Selections::SOLVE as i32){
|
|
return Selections::SOLVE;
|
|
}
|
|
else if(selection == Selections::DESCRIPTION as i32){
|
|
return Selections::DESCRIPTION;
|
|
}
|
|
else if(selection == Selections::LIST as i32){
|
|
return Selections::LIST;
|
|
}
|
|
else if(selection == Selections::EXIT as i32){
|
|
return Selections::EXIT;
|
|
}
|
|
else{
|
|
return Selections::EMPTY;
|
|
}
|
|
}
|
|
fn printErrorMessage(){
|
|
println!("That is an invalid selection!");
|
|
}
|
|
fn solveMenu(){
|
|
let problemNumber = getProblemNumber();
|
|
println!("\n\n");
|
|
//This selection solves all problems in order
|
|
if(problemNumber == 0){
|
|
//Solve to every valid problem number
|
|
for problemLocation in 1..problemNumbers.len() as u32{
|
|
//Solve the problems
|
|
print!("{}. ", problemNumbers[problemLocation as usize]);
|
|
solveProblem(problemNumbers[problemLocation as usize]);
|
|
println!("\n\n");
|
|
}
|
|
}
|
|
//This is if a single problem number was chosen
|
|
else{
|
|
//Solve the problem
|
|
solveProblem(problemNumber);
|
|
}
|
|
println!("\n\n");
|
|
}
|
|
fn solveProblem(problemNumber: u32){
|
|
if(problemNumber == 1){
|
|
println!("{}", Problems::Problem1::getDescription());
|
|
println!("{}", Problems::Problem1::solve());
|
|
}
|
|
else if(problemNumber == 2){
|
|
println!("{}", Problems::Problem2::getDescription());
|
|
println!("{}", Problems::Problem2::solve());
|
|
}
|
|
else if(problemNumber == 3){
|
|
println!("{}", Problems::Problem3::getDescription());
|
|
println!("{}", Problems::Problem3::solve());
|
|
}
|
|
else if(problemNumber == 4){
|
|
println!("{}", Problems::Problem4::getDescription());
|
|
println!("{}", Problems::Problem4::solve());
|
|
}
|
|
else if(problemNumber == 5){
|
|
println!("{}", Problems::Problem5::getDescription());
|
|
println!("{}", Problems::Problem5::solve());
|
|
}
|
|
else if(problemNumber == 6){
|
|
println!("{}", Problems::Problem6::getDescription());
|
|
println!("{}", Problems::Problem6::solve());
|
|
}
|
|
else if(problemNumber == 7){
|
|
println!("{}", Problems::Problem7::getDescription());
|
|
println!("{}", Problems::Problem7::solve());
|
|
}
|
|
else if(problemNumber == 8){
|
|
println!("{}", Problems::Problem8::getDescription());
|
|
println!("{}", Problems::Problem8::solve());
|
|
}
|
|
else if(problemNumber == 9){
|
|
println!("{}", Problems::Problem9::getDescription());
|
|
println!("{}", Problems::Problem9::solve());
|
|
}
|
|
else if(problemNumber == 10){
|
|
println!("{}", Problems::Problem10::getDescription());
|
|
println!("{}", Problems::Problem10::solve());
|
|
}
|
|
else if(problemNumber == 11){
|
|
println!("{}", Problems::Problem11::getDescription());
|
|
println!("{}", Problems::Problem11::solve());
|
|
}
|
|
else if(problemNumber == 12){
|
|
println!("{}", Problems::Problem12::getDescription());
|
|
println!("{}", Problems::Problem12::solve());
|
|
}
|
|
else if(problemNumber == 13){
|
|
println!("{}", Problems::Problem13::getDescription());
|
|
println!("{}", Problems::Problem13::solve());
|
|
}
|
|
else if(problemNumber == 14){
|
|
println!("{}", Problems::Problem14::getDescription());
|
|
println!("{}", Problems::Problem14::solve());
|
|
}
|
|
else if(problemNumber == 15){
|
|
println!("{}", Problems::Problem15::getDescription());
|
|
println!("{}", Problems::Problem15::solve());
|
|
}
|
|
else if(problemNumber == 16){
|
|
println!("{}", Problems::Problem16::getDescription());
|
|
println!("{}", Problems::Problem16::solve());
|
|
}
|
|
else if(problemNumber == 17){
|
|
println!("{}", Problems::Problem17::getDescription());
|
|
println!("{}", Problems::Problem17::solve());
|
|
}
|
|
}
|
|
fn descriptionMenu(){
|
|
//Give some extra space to print the description
|
|
println!("\n\n");
|
|
|
|
//Get the problem number
|
|
let problemNumber = getProblemNumber();
|
|
|
|
//If the problem number is 0 then print out all the descriptions
|
|
if(problemNumber == 0){
|
|
//Print decription for every valid problem number
|
|
for problemLocation in 1..problemNumbers.len(){
|
|
//Print the problem's description
|
|
print!("{}. ", problemNumbers[problemLocation]);
|
|
printDescription(problemNumbers[problemLocation]);
|
|
println!();
|
|
}
|
|
}
|
|
//Otherwise print out a single problem's description
|
|
else{
|
|
printDescription(problemNumber);
|
|
}
|
|
println!("\n\n");
|
|
}
|
|
fn printDescription(problemNumber: u32){
|
|
if(problemNumber == 1){
|
|
println!("{}", Problems::Problem1::getDescription());
|
|
}
|
|
else if(problemNumber == 2){
|
|
println!("{}", Problems::Problem2::getDescription());
|
|
}
|
|
else if(problemNumber == 3){
|
|
println!("{}", Problems::Problem3::getDescription());
|
|
}
|
|
else if(problemNumber == 4){
|
|
println!("{}", Problems::Problem4::getDescription());
|
|
}
|
|
else if(problemNumber == 5){
|
|
println!("{}", Problems::Problem5::getDescription());
|
|
}
|
|
else if(problemNumber == 6){
|
|
println!("{}", Problems::Problem6::getDescription());
|
|
}
|
|
else if(problemNumber == 7){
|
|
println!("{}", Problems::Problem7::getDescription());
|
|
}
|
|
else if(problemNumber == 8){
|
|
println!("{}", Problems::Problem8::getDescription());
|
|
}
|
|
else if(problemNumber == 9){
|
|
println!("{}", Problems::Problem9::getDescription());
|
|
}
|
|
else if(problemNumber == 10){
|
|
println!("{}", Problems::Problem10::getDescription());
|
|
}
|
|
else if(problemNumber == 11){
|
|
println!("{}", Problems::Problem11::getDescription());
|
|
}
|
|
else if(problemNumber == 12){
|
|
println!("{}", Problems::Problem12::getDescription());
|
|
}
|
|
else if(problemNumber == 13){
|
|
println!("{}", Problems::Problem13::getDescription());
|
|
}
|
|
else if(problemNumber == 14){
|
|
println!("{}", Problems::Problem14::getDescription());
|
|
}
|
|
else if(problemNumber == 15){
|
|
println!("{}", Problems::Problem15::getDescription());
|
|
}
|
|
else if(problemNumber == 16){
|
|
println!("{}", Problems::Problem16::getDescription());
|
|
}
|
|
else if(problemNumber == 17){
|
|
println!("{}", Problems::Problem17::getDescription());
|
|
}
|
|
}
|
|
fn getProblemNumber() -> u32{
|
|
println!("Enter a problem number: ");
|
|
//Setup reading from the console
|
|
let mut problemNumberStr = String::new();
|
|
std::io::stdin().read_line(&mut problemNumberStr).ok();
|
|
//Convert the read to an int
|
|
let mut problemNumber = problemNumberStr.trim().parse::<u32>().unwrap();
|
|
|
|
//Loop through all valid problem numbers and see if the one entered matches it
|
|
while(!problemNumbers.contains(&problemNumber)){
|
|
println!("That is an invalid problem number!\nEnter a problem number: ");
|
|
std::io::stdin().read_line(&mut problemNumberStr).ok();
|
|
problemNumber = problemNumberStr.trim().parse::<u32>().unwrap();
|
|
}
|
|
return problemNumber;
|
|
}
|
|
fn listProblems(){
|
|
print!("Problems: {}", problemNumbers[1]);
|
|
for problemNumber in 2..problemNumbers.len(){
|
|
println!(", {}", problemNumbers[problemNumber])
|
|
}
|
|
println!();
|
|
}
|