calc_lib
A simple library for passing in expressions in string from and getting back the result, using correct order of operations.
This works best when you have an equation in string form, usually from user input, and need to solve it quickly and efficiently.
Examples:
Integer equations:
use calc_lib::solve;
fn main() {
let expression = "(1 + 2) * 3";
let x = solve(expression);
if x.is_err() {
panic!("{}", x.unwrap_err());
}
println!("Result: {}", x.unwrap());
}
Decimal Equations:
use calc_lib::solve_decimals;
fn main() {
let expression = "1.3 + 2.5 * 3.1";
let x = solve_decimals("1.3 + 2.5 * 3.1");
if x.is_err() {
panic!("{}", x.unwrap_err());
}
println!("Result: {}", x.unwrap());
}
Solving with variables:
use calc_lib::{solve_with_definitions, Definitions};
fn main() {
let expression = "x + y";
let mut definitions = Definitions::new();
defs.insert("x".to_string(), 4);
defs.insert("y".to_string(), 5);
let solved = solve_with_definitions_f64(expression, &definitions);
if solved.is_err() {
panic!("{}", solved.unwrap_err());
}
println!("Result: {}", solved.unwrap());
}
Solving with variables and decimals:
use calc_lib::{solve_with_definitions_f64, Definitions};
fn main() {
let expression = "x + y";
let mut definitions = Definitions::new();
defs.insert("x".to_string(), 4.5);
defs.insert("y".to_string(), 5.5);
let solved = solve_with_definitions_f64(expression, &definitions);
if solved.is_err() {
panic!("{}", solved.unwrap_err());
}
println!("Result: {}", solved.unwrap());
}