math-tool/src/main.rs

152 lines
4.5 KiB
Rust

/*
Written by: James Musselman
On: 3/14/2024
Updated: Never
Description:
This tool was written as part of my 100DaysOfCode commitment for the 2024 year to help establish a
habit of programming daily and fuel my continuous drive to learn.
The tool below should allow the user to run basic, intermediate, and advanced math computations.
The user will select the equation or type of math they want to be done, enter the data, and
then receive the result of the computation.
Future work could include showing how the answer was calculated, showing graphs, and exporting to
a text file.
*/
use std::io;
fn main() {
// TODO: Greet and explain tool
println!("This math tool was made as a way to practice rust and is under development and does");
println!("not test or account for edge cases. (Go and try to divide by zero if you want to...0)\n");
println!("To use the tool enter a number from the menu below and enter values as requested.");
println!("If the number you enter does not exist in the menu it will be the return value.\n");
// Get user choice on what they want to use
loop {
//Print choices
println!("Here are the current math tools:\n");
println!("1. Addition");
println!("2. Subtraction");
println!("3. Multiplication");
println!("4. Division");
// Get user input
let mut user_input = String::new();
println!("Please select a function above or use 'q' to quit:");
io::stdin()
.read_line(&mut user_input)
.expect("Failed to read line");
if user_input.trim().eq_ignore_ascii_case("q") {
break;
}
let num: Result<i32, _> = user_input.trim().parse();
// Match statement to run choice
match num {
Ok(num) => {
let result = run_function(num);
println!("The result was: {}", result);
}
Err(_) => println!("Invalid input. Please try again."),
}
}
}
fn run_function(num: i32) -> i32 {
// run choice, then ask again or quit if the user selects that option
match num {
1 => addition(),
2 => subtraction(),
3 => multiplication(),
4 => division(),
_ => {
println!(
"{} is not a valid selection, please select one of the options below",
num
);
num
}
}
}
fn addition() -> i32 {
println!("Please enter the value of the first number:");
let mut x = String::new();
io::stdin()
.read_line(&mut x)
.expect("Failed to read x value");
let x: i32 = x.trim().parse().expect("Failed to parse x value");
println!("Please enter the value of the second number:");
let mut y = String::new();
io::stdin()
.read_line(&mut y)
.expect("Failed to read y value");
let y: i32 = y.trim().parse().expect("Failed to parse y value");
x + y
}
fn subtraction() -> i32 {
println!("Please enter the value of the first number:");
let mut x = String::new();
io::stdin()
.read_line(&mut x)
.expect("Failed to read x value");
let x: i32 = x.trim().parse().expect("Failed to parse x value");
println!("Please enter the value of the second number:");
let mut y = String::new();
io::stdin()
.read_line(&mut y)
.expect("Failed to read y value");
let y: i32 = y.trim().parse().expect("Failed to parse y value");
x - y
}
fn multiplication() -> i32 {
println!("Please enter the value of the first number:");
let mut x = String::new();
io::stdin()
.read_line(&mut x)
.expect("Failed to read x value");
let x: i32 = x.trim().parse().expect("Failed to parse x value");
println!("Please enter the value of the second number:");
let mut y = String::new();
io::stdin()
.read_line(&mut y)
.expect("Failed to read y value");
let y: i32 = y.trim().parse().expect("Failed to parse y value");
x * y
}
fn division() -> i32 {
println!("Please enter the value of the first number:");
let mut x = String::new();
io::stdin()
.read_line(&mut x)
.expect("Failed to read x value");
let x: i32 = x.trim().parse().expect("Failed to parse x value");
println!("Please enter the value of the second number:");
let mut y = String::new();
io::stdin()
.read_line(&mut y)
.expect("Failed to read y value");
let y: i32 = y.trim().parse().expect("Failed to parse y value");
x / y
}