diff --git a/Cargo.toml b/Cargo.toml index 0733c56..32ad5c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +meval = "0.2.0" diff --git a/src/main.rs b/src/main.rs index fceb648..a717d60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::env; +use meval::tokenizer::tokenize; fn main() { let args: Vec = env::args().skip(1).take(3).collect(); @@ -18,13 +19,30 @@ fn main() { println!("Usage: $0 -1:1 3:-5 10:1.5") } } + + #[cfg(debug_assertions)] + default_points(&mut points); + + group_by_x(&mut points); let m1 = (points[0][1] - points[1][1]) / (points[0][0] - points[1][0]); - if m1 == (points[1][1] - points[2][1]) / (points[1][0] - points[2][0]) { - println!("{}", straight_string(m1, &points)); + let function = if m1 == (points[1][1] - points[2][1]) / (points[1][0] - points[2][0]) { + straight_string(m1, &points) } else { - println!("{}", square_string(&points)); + square_string(&points) + }; + + if function.contains("NaN") { + panic!("Contains NaN, this is probably the result of bad math"); + } + let expr2 = tokenize(function.as_str()).unwrap(); + println!("{:?}", expr2); +} + +fn default_points(points: &mut [[f64;2];3]) { + if points.iter().all(|row| row.iter().all(|&x| x == 0.0)) { + *points = [[-1.0,1.0], [2.0,4.0], [3.0,9.0]]; } } @@ -111,7 +129,7 @@ fn square_string(points: &[[f64;2];3]) -> String { if a == -1.0 { "-".to_string() } else if a != 1.0 { - a.to_string() + format!("{}{}", a, " * ") } else { "".to_string() }, @@ -124,7 +142,7 @@ fn square_string(points: &[[f64;2];3]) -> String { " + ".to_string() }, if b != 1.0 { - format!("{}x", b.abs()) + format!("{} * x", b.abs()) } else { "x".to_string() }