begin to beeing able to use function in rust code
This commit is contained in:
parent
368f2aea7b
commit
19582a06ab
@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
meval = "0.2.0"
|
||||||
|
|||||||
28
src/main.rs
28
src/main.rs
@ -1,4 +1,5 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
use meval::tokenizer::tokenize;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().skip(1).take(3).collect();
|
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")
|
println!("Usage: $0 -1:1 3:-5 10:1.5")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
default_points(&mut points);
|
||||||
|
|
||||||
|
|
||||||
group_by_x(&mut points);
|
group_by_x(&mut points);
|
||||||
|
|
||||||
let m1 = (points[0][1] - points[1][1]) / (points[0][0] - points[1][0]);
|
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]) {
|
let function = if m1 == (points[1][1] - points[2][1]) / (points[1][0] - points[2][0]) {
|
||||||
println!("{}", straight_string(m1, &points));
|
straight_string(m1, &points)
|
||||||
} else {
|
} 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 {
|
if a == -1.0 {
|
||||||
"-".to_string()
|
"-".to_string()
|
||||||
} else if a != 1.0 {
|
} else if a != 1.0 {
|
||||||
a.to_string()
|
format!("{}{}", a, " * ")
|
||||||
} else {
|
} else {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
},
|
},
|
||||||
@ -124,7 +142,7 @@ fn square_string(points: &[[f64;2];3]) -> String {
|
|||||||
" + ".to_string()
|
" + ".to_string()
|
||||||
},
|
},
|
||||||
if b != 1.0 {
|
if b != 1.0 {
|
||||||
format!("{}x", b.abs())
|
format!("{} * x", b.abs())
|
||||||
} else {
|
} else {
|
||||||
"x".to_string()
|
"x".to_string()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user