Struct math_parse::MathParse
source · pub struct MathParse { /* private fields */ }Expand description
Object generated when parsing a string of math. Can be later used for solving or formatting to other representations.
Implementations§
source§impl MathParse
impl MathParse
sourcepub fn parse(expression: &str) -> Result<Self, MathParseErrors>
pub fn parse(expression: &str) -> Result<Self, MathParseErrors>
Parse a math expression in infix notation.
math_parse::MathParse::parse("3 + 4").unwrap();sourcepub fn parse_rpn(expression: &str) -> Result<Self, MathParseErrors>
pub fn parse_rpn(expression: &str) -> Result<Self, MathParseErrors>
Parse a math expression in postfix notation (RPN).
math_parse::MathParse::parse_rpn("3 4 +").unwrap();source§impl MathParse
impl MathParse
sourcepub fn solve_auto(
&self,
map: Option<&HashMap<String, String>>
) -> Result<Result<i64, f64>, MathParseErrors>
pub fn solve_auto( &self, map: Option<&HashMap<String, String>> ) -> Result<Result<i64, f64>, MathParseErrors>
Does all the computation from a string with a line of math to the final
resulting number. If the result can be an int, return it as
Ok(Ok(int)). If it can only be a float, return it as Ok(Err(floar)).
If it can’t be solved, return Err(error).
use math_parse::MathParse;
use math_parse::MathParseErrors::*;
assert_eq!(
MathParse::parse("3 + 8.0").unwrap().solve_auto(None),
Ok(Ok(11)));
assert_eq!(
MathParse::parse("3 - 8.5").unwrap().solve_auto(None),
Ok(Err(-5.5)));
assert_eq!(
MathParse::parse("34 + bcd").unwrap().solve_auto(None),
Err(InvalidNumber("bcd".to_string())));A optional map of variable name can be taken as argument.
sourcepub fn solve_int(
&self,
variable_map: Option<&HashMap<String, String>>
) -> Result<i64, MathParseErrors>
pub fn solve_int( &self, variable_map: Option<&HashMap<String, String>> ) -> Result<i64, MathParseErrors>
Does all the computation from a string with a line of math to the final
resulting number. If the result can be an int, return it as
Ok(int). If it can’t be solved as an int, return Err(error).
use math_parse::MathParse;
use math_parse::MathParseErrors::*;
assert_eq!(
MathParse::parse("3 + 8.0").unwrap().solve_int(None),
Ok(11));
assert_eq!(
MathParse::parse("3 - 8.5").unwrap().solve_int(None),
Err(ReturnFloatExpectedInt(-5.5)));A optional map of variable name can be taken as argument:
use math_parse::MathParse;
let variables = std::collections::HashMap::from([
("a".to_string(), "1".to_string()),
("b".to_string(), "3*3".to_string()),
]);
let result = MathParse::parse("a+b").unwrap().solve_int(Some(&variables)).unwrap();
assert_eq!(result, 10);sourcepub fn solve_float(
&self,
variable_map: Option<&HashMap<String, String>>
) -> Result<f64, MathParseErrors>
pub fn solve_float( &self, variable_map: Option<&HashMap<String, String>> ) -> Result<f64, MathParseErrors>
Does all the computation from a string with a line of math to the final
resulting number. The result is returned as a Ok(f64).
If it can’t be solved, return Err(error).
use math_parse::MathParse;
use math_parse::MathParseErrors::*;
assert_eq!(
MathParse::parse("3 + 8").unwrap().solve_float(None),
Ok(11.0));
assert_eq!(
MathParse::parse("3 - 8.5").unwrap().solve_float(None),
Ok(-5.5));A optional map of variable name can be taken as argument.
source§impl MathParse
impl MathParse
sourcepub fn to_rpn(&self) -> Result<Vec<RPN>, MathParseErrors>
pub fn to_rpn(&self) -> Result<Vec<RPN>, MathParseErrors>
Parse a math expression into list of instructions in Reverse Polish notation (postfix notation).
Example:
use math_parse::RPN::*;
use math_parse::UnaryOp::*;
use math_parse::BinaryOp::*;
use math_parse::MathParse;
assert_eq!(
MathParse::parse("3-4+(-5)").unwrap().to_rpn(),
Ok(vec![Name("3".to_string()), Name("4".to_string()), Binary(Subtraction), Name("5".to_string()), Unary(Minus), Binary(Addition)]));source§impl MathParse
impl MathParse
sourcepub fn to_tree(&self) -> Result<Tree, MathParseErrors>
pub fn to_tree(&self) -> Result<Tree, MathParseErrors>
Parse a math expression into list of instructions as a Tree (infix notation).
Example:
use math_parse::Tree::*;
use math_parse::UnaryOp::*;
use math_parse::BinaryOp::*;
use math_parse::MathParse;
assert_eq!(
MathParse::parse("3*4+(-5)").unwrap().to_tree(),
Ok(Binary(Addition,
Box::new(Binary(Multiplication,
Box::new(Name("3".to_string())),
Box::new(Name("4".to_string())))),
Box::new(Unary(Minus,
Box::new(Name("5".to_string())))))));