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

source

pub fn new() -> DumbCalcProcessor

source

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

source

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

source

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

source

pub fn eval(&mut self) -> Result<f64, DumbError>

like DumbCalcProcessor::evaluate, evaluate the pushed “calculation units” and return the result

source

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

source

pub fn get_last_operator(&self) -> Option<String>

return the last input “calculation unit”, only if it is an operator

source

pub fn count_opened_brackets(&self) -> u16

count and return the number of opened brackets

source

pub fn use_angle_mode(&mut self, angle_mode: &str)

use the “angle mode” for trigonometric functions

  • angle_mode: “deg” or “rad”
source

pub fn reset(&mut self)

reset for new input

source

pub fn backup(&self) -> CalcProcessorBackup

make a backup of the current state; can call DumbCalcProcessor::restore to restore the state

source

pub fn restore(&mut self, backup: CalcProcessorBackup)

restore the state from a backup, made with DumbCalcProcessor::backup

source§

impl DumbCalcProcessor

source

pub fn is_operator(unit: &str) -> bool

source

pub fn is_binary_operator(unit: &str) -> bool

source

pub fn is_unary_operator(unit: &str) -> bool

Trait Implementations§

source§

impl Debug for DumbCalcProcessor

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.