better functionality
This commit is contained in:
17
src/main.rs
17
src/main.rs
@@ -1,5 +1,6 @@
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
use std::num::ParseFloatError;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -27,12 +28,14 @@ fn main() {
|
|||||||
"*" | "x" => Function::Multiply,
|
"*" | "x" => Function::Multiply,
|
||||||
"/" => Function::Divide,
|
"/" => Function::Divide,
|
||||||
"%" => Function::Modulo,
|
"%" => 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 {
|
match func {
|
||||||
Function::Push(x) => stack.push(x),
|
Function::Push(x) => stack_push(&mut stack, x),
|
||||||
Function::Quit => break,
|
Function::Quit => break,
|
||||||
|
Function::ClearStack => stack.clear(),
|
||||||
others => operate(&mut stack, others),
|
others => operate(&mut stack, others),
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,10 +53,18 @@ enum Function {
|
|||||||
Multiply,
|
Multiply,
|
||||||
Divide,
|
Divide,
|
||||||
Modulo,
|
Modulo,
|
||||||
Push(f64),
|
Push(Result<f64, ParseFloatError>),
|
||||||
|
ClearStack,
|
||||||
Quit,
|
Quit,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn stack_push(stack: &mut Vec<f64>, num: Result<f64, ParseFloatError>) {
|
||||||
|
match num {
|
||||||
|
Ok(num) => stack.push(num),
|
||||||
|
Err(e) => println!("Cannot push: {e}."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn stack_too_small_err(func: &str) {
|
fn stack_too_small_err(func: &str) {
|
||||||
println!("Stack must contain at least 2 numbers to {func}!");
|
println!("Stack must contain at least 2 numbers to {func}!");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user