begin to beeing able to use function in rust code

This commit is contained in:
fabolous005 2024-01-15 18:50:01 +01:00
parent 368f2aea7b
commit 19582a06ab
2 changed files with 24 additions and 5 deletions

View File

@ -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"

View File

@ -1,4 +1,5 @@
use std::env;
use meval::tokenizer::tokenize;
fn main() {
let args: Vec<String> = 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()
}