From 739df0f990fea4d1c9feb94f6cd788c3c1dd3f8d Mon Sep 17 00:00:00 2001 From: David Senk Date: Thu, 7 Sep 2023 15:24:44 -0400 Subject: [PATCH] better functionality --- src/main.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index c0cdd89..3e16291 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::io; use std::io::Write; +use std::num::ParseFloatError; use std::str::FromStr; fn main() { @@ -27,12 +28,14 @@ fn main() { "*" | "x" => Function::Multiply, "/" => Function::Divide, "%" => Function::Modulo, - _ => Function::Push(f64::from_str(input.trim()).expect("valid number")), + "clear" | "c" => Function::ClearStack, + _ => Function::Push(f64::from_str(input.trim())), }; match func { - Function::Push(x) => stack.push(x), + Function::Push(x) => stack_push(&mut stack, x), Function::Quit => break, + Function::ClearStack => stack.clear(), others => operate(&mut stack, others), } @@ -50,10 +53,18 @@ enum Function { Multiply, Divide, Modulo, - Push(f64), + Push(Result), + ClearStack, Quit, } +fn stack_push(stack: &mut Vec, num: Result) { + match num { + Ok(num) => stack.push(num), + Err(e) => println!("Cannot push: {e}."), + } +} + fn stack_too_small_err(func: &str) { println!("Stack must contain at least 2 numbers to {func}!"); }