mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Initial commit with a few problems
This commit is contained in:
171
src/main.rs
Normal file
171
src/main.rs
Normal file
@@ -0,0 +1,171 @@
|
||||
#![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; 3] = [0, 1, 2];
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
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!();
|
||||
}
|
||||
Reference in New Issue
Block a user