Get basic math working
This commit is contained in:
parent
c7df2bd546
commit
1bf7e1a884
1 changed files with 83 additions and 5 deletions
86
src/main.rs
86
src/main.rs
|
@ -22,9 +22,17 @@ fn main() {
|
||||||
|
|
||||||
// Get user choice on what they want to use
|
// Get user choice on what they want to use
|
||||||
loop {
|
loop {
|
||||||
|
//Print choices
|
||||||
|
println!("Here are the current math tools:\n");
|
||||||
|
println!("1. Additon");
|
||||||
|
println!("2. Subtraction");
|
||||||
|
println!("3. Multiplication");
|
||||||
|
println!("4. Division");
|
||||||
|
|
||||||
|
// Get user input
|
||||||
let mut user_input = String::new();
|
let mut user_input = String::new();
|
||||||
|
|
||||||
println!("Please select a function below or use 'q' to quit:");
|
println!("Please select a function above or use 'q' to quit:");
|
||||||
|
|
||||||
io::stdin().read_line(&mut user_input)
|
io::stdin().read_line(&mut user_input)
|
||||||
.expect("Failed to read line");
|
.expect("Failed to read line");
|
||||||
|
@ -38,14 +46,84 @@ fn main() {
|
||||||
// Match statement to run choice
|
// Match statement to run choice
|
||||||
match num {
|
match num {
|
||||||
Ok(num) => {
|
Ok(num) => {
|
||||||
run_function(num);
|
let result = run_function(num);
|
||||||
|
println!("The result was: {}", result);
|
||||||
}
|
}
|
||||||
Err(_) => println!("Invalid input. Please try again."),
|
Err(_) => println!("Invalid input. Please try again."),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_function(num: i32) {
|
fn run_function(num: i32) -> i32 {
|
||||||
// run choice, then ask again or quit if the user selects that option
|
// run choice, then ask again or quit if the user selects that option
|
||||||
println!("{}",num)
|
match num {
|
||||||
|
1 => addition(),
|
||||||
|
2 => subtraction(),
|
||||||
|
3 => multiplacation(),
|
||||||
|
4 => division(),
|
||||||
|
_ => {
|
||||||
|
println!("{} is not a valid selection, please select one of the options below", num);
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 multiplacation() -> 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
|
||||||
}
|
}
|
Loading…
Reference in a new issue