Struct rusty_dumb_tools::calc::DumbCalcProcessor
source · pub struct DumbCalcProcessor { /* private fields */ }
Expand description
a simple infix calculation processor that accepts a stream of “calculation units” and evaluate the result;
please refer to DumbCalcProcessor::push
for the acceptable “calculation units”
example:
use rusty_dumb_tools::calc::DumbCalcProcessor;
let mut calc = DumbCalcProcessor::new();
calc.push("1.5"); // push a single "calculation unit", like a number or operator
calc.eval().unwrap(); // evaluate the pushed "calculation units" and get the result
assert_eq!(1.5, calc.get_result().unwrap());
calc.parse_and_push("+ 2.5 * 3 - 4"); // based on last calculation result, parse and push additional "calculation units"
calc.eval().unwrap(); // evaluate the pushed "calculation units" and get the result
assert_eq!(5.0, calc.get_result().unwrap());
you may want to refer to crate::demo::run_demo
for a demo program that uses DumbCalcProcessor
;
additionally, you may want to consider crate::calculator::DumbCalculator
, which should make coding a calculator UI easier
Implementations§
source§impl DumbCalcProcessor
impl DumbCalcProcessor
pub fn new() -> DumbCalcProcessor
sourcepub fn push(&mut self, unit: &str) -> Result<(), DumbError>
pub fn push(&mut self, unit: &str) -> Result<(), DumbError>
push a “calculation unit”:
- a bracket: “(”, “)”
- a number: e.g. “0”, “1”, “2.3”, “-4”, “-5.67”
- a binary operator: “+”, “-”, “*”, “/”, “^”
note that these binary operators have the usual precedence - an unary operator: “neg”, “sin”, “cos”, “tan”, “asin”, “acos”, “atan”, “log”, “ln”, “sqrt”, “square”, “pow10”, “inv”, “exp”, “abs”, “%”
notes:- an unary operator should come after the operand that it operates on;
- these unary operators have the same highest precedence (basically operation will be performed, with the operand that comes before it, immediately)
- a constant: “PI”, “E”
- a “=”, which will evaluate the pushed “calculation units”
please use DumbCalcProcessor::parse_and_push
if you want to push multiple “calculation units” in a string, like a string of a complete infix expression
sourcepub fn parse_and_push<T: AsRef<str>>(
&mut self,
units: T
) -> Result<(), DumbError>
pub fn parse_and_push<T: AsRef<str>>( &mut self, units: T ) -> Result<(), DumbError>
parse and push multiple “calculation units” in a string, like a string of a complete infix expression;
each parsed “calculation unit” will be pushed one by one with DumbCalcProcessor::push
note: please consider unary operators as not parsable
sourcepub fn evaluate(&mut self)
pub fn evaluate(&mut self)
evaluate the pushed “calculation units”;
the result will also be assigned to the internal result
, which can be used as the “initial” value of the next sequence of “calculation units”;
note that you can call DumbCalcProcessor::get_result
to get the result
sourcepub fn eval(&mut self) -> Result<f64, DumbError>
pub fn eval(&mut self) -> Result<f64, DumbError>
like DumbCalcProcessor::evaluate
, evaluate the pushed “calculation units” and return the result
sourcepub fn get_result(&self) -> CalcResult
pub fn get_result(&self) -> CalcResult
return the calculation result so far; call DumbCalcProcessor::evaluate
to evaluate the pushed “calculation units”, and assign the result to it (as final result)
note that the result is a CalcResult
enum, that can be one of three kinds – final, intermediate, or error
sourcepub fn get_last_operator(&self) -> Option<String>
pub fn get_last_operator(&self) -> Option<String>
return the last input “calculation unit”, only if it is an operator
sourcepub fn count_opened_brackets(&self) -> u16
pub fn count_opened_brackets(&self) -> u16
count and return the number of opened brackets
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 backup(&self) -> CalcProcessorBackup
pub fn backup(&self) -> CalcProcessorBackup
make a backup of the current state; can call DumbCalcProcessor::restore
to restore the state
sourcepub fn restore(&mut self, backup: CalcProcessorBackup)
pub fn restore(&mut self, backup: CalcProcessorBackup)
restore the state from a backup, made with DumbCalcProcessor::backup