Struct rusty_dumb_tools::calculator::DumbCalculator
source · pub struct DumbCalculator { /* private fields */ }Implementations§
source§impl DumbCalculator
impl DumbCalculator
A simple calculator that accepts input keys acting like a real calculator;
it may task is the keep track of key presses and turn them into “calculation units”;
it uses a crate::calc::DumbCalcProcessor to handle the actual calculation processing
For example:
use rusty_dumb_tools::calculator::DumbCalculator;
let mut calculator = DumbCalculator::new();
calculator.push("1").unwrap();
calculator.push(".").unwrap();
calculator.push("0").unwrap();
calculator.push("2").unwrap();
assert_eq!(calculator.get_display(), "1.02");
calculator.push("*").unwrap();
calculator.push("3").unwrap();
assert_eq!(calculator.get_display(), "3");
calculator.push("=").unwrap();
assert_eq!(calculator.get_display(), "3.06");For a fuller sample code, please refer to the “calculator” sub-demo of crate::demo::run_demo
sourcepub fn new_min() -> Self
pub fn new_min() -> Self
create a new DumbCalculator instance with minimum feature (i.e. no undo etc)
sourcepub fn new() -> Self
pub fn new() -> Self
create a new DumbCalculator (with all the default features enabled)
sourcepub fn new_with_settings(settings: DumbCalculatorSettings) -> Self
pub fn new_with_settings(settings: DumbCalculatorSettings) -> Self
like DumbCalculator::new but with settings
sourcepub fn push(&mut self, key: &str) -> Result<(), DumbError>
pub fn push(&mut self, key: &str) -> Result<(), DumbError>
push a “key input”:
- a digit, including a “.”
- a bracket: “(”, “)”
- an operator accepted by
crate::calc::DumbCalcProcessor::pushlike:- binary operators; e.g. “+”, “-”, “*”, “/”, etc
- unary operators; e.g. “neg”, “sin”, “cos”, “tan”, etc
- a constant accepted by
crate::calc::DumbCalcProcessor::pushlike “PI”, etc - “=” there are some special “key input”:
- “ac”: clear the calculator
- “undo”: undo the last “key input” done by
DumbCalculator::push, if undo is enabled - “mc”, “mr”, “ms”, “m+”, “m-”: memory keys
sourcepub fn push_chars(&mut self, keys: &str) -> Result<(), DumbError>
pub fn push_chars(&mut self, keys: &str) -> Result<(), DumbError>
like DumbCalculator::push but each characters of the input will be pushed individually one by one
sourcepub fn undo(&mut self)
pub fn undo(&mut self)
undo the last “key input” done by DumbCalculator::push, if undo is enabled
sourcepub fn use_angle_mode(&mut self, angle_mode: &str)
pub fn use_angle_mode(&mut self, angle_mode: &str)
use the “angle mode” for trigonometric functions
- angle_mode: “deg” or “rad”
sourcepub fn get_memory(&self) -> Option<f64>
pub fn get_memory(&self) -> Option<f64>
get memory
sourcepub fn get_history(&self) -> Option<&[String]>
pub fn get_history(&self) -> Option<&[String]>
get history of the “key input”, if history is enabled
sourcepub fn get_history_string(&self, better_symbols: bool) -> Option<String>
pub fn get_history_string(&self, better_symbols: bool) -> Option<String>
like DumbCalculator::get_history but returns a string instead
sourcepub fn get_display(&self) -> String
pub fn get_display(&self) -> String
get what to show on the calculator’s display
sourcepub fn get_display_sized(&self, result_width: usize) -> String
pub fn get_display_sized(&self, result_width: usize) -> String
get what to show on the calculator’s display, but with a fixed display width