diff --git a/src/lib.rs b/src/lib.rs index 76daeab..4cbfe35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,11 +20,8 @@ pub fn pretty(v: &Vec) { pub fn parse(query: &str) -> Result> { let tokens = tokenize(query)?; - println!("{:?}", tokens); let sanitized_tokens = sanitize_tokens(tokens)?; - println!("{:?}", sanitized_tokens); let ast = build_ast(sanitized_tokens); - println!("{:?}", ast); Ok(ast) } diff --git a/src/main.rs b/src/main.rs index 7bd7a72..fa02d3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,6 @@ fn main() { println!("Error during parsing: {e}"); process::exit(1); }); - println!("{:?}", equation); let is_equation = computorv1::maths::evaluator::is_equation(&equation); let evaluated = computorv1::maths::evaluator::evaluate(equation).unwrap_or_else(|e| { println!("Error during evaluation: {e}"); diff --git a/src/maths.rs b/src/maths.rs index 4a5a3c7..d8ade07 100644 --- a/src/maths.rs +++ b/src/maths.rs @@ -268,9 +268,10 @@ impl ops::Div for GaussianRational { } } +#[cfg(test)] mod tests { use super::*; - + #[test] fn gcd_test() { assert_eq!(gcd(7, 3), 1); diff --git a/src/maths/evaluator.rs b/src/maths/evaluator.rs index 69fc7e2..d8c7f24 100644 --- a/src/maths/evaluator.rs +++ b/src/maths/evaluator.rs @@ -43,7 +43,6 @@ fn sub(lhs: Vec, rhs: Vec) -> Vec, rhs: &Vec) -> Vec { - println!("\nIci ca multiplie {:?} et {:?}", lhs, rhs); let len = lhs.len() + rhs.len() - 1; let mut res = Vec::new(); for _ in 0..len { diff --git a/src/maths/solver.rs b/src/maths/solver.rs index 96e06b8..3dda2c5 100644 --- a/src/maths/solver.rs +++ b/src/maths/solver.rs @@ -37,14 +37,11 @@ fn simplify_sqrt(mut n: i128) -> (i128, i128) { } let mut prime_factors = vec![]; let mut prime = 3; - println!("stuck {n}"); while n != 1 && prime * prime < n { if n % (prime - 1) == 0 { - println!("stuck {n} {}", prime - 1); prime_factors.push(prime - 1); n /= prime - 1; } else if n % (prime + 1) == 0 { - println!("stuck {n} {}", prime + 1); prime_factors.push(prime + 1); n /= prime + 1; } else { @@ -77,22 +74,16 @@ fn simplify_sqrt(mut n: i128) -> (i128, i128) { } fn comp_sqrt(c: GaussianRational) -> MyCompSqrt { - println!("c in comp_sqrt: {:?}", c); let real = c.real(); let imag = c.imaginary(); let n = real * real + imag * imag; let numerator_irrational = n.numerator * n.denominator; - println!("numerator_irrational {:?}", numerator_irrational); let (mut numerator_natural, numerator_irrational) = simplify_sqrt(numerator_irrational); - println!("numerator_irrational {:?} numerator_natural {:?}", numerator_irrational, numerator_natural); let mut denominator = n.denominator; let gcd_var = gcd(numerator_natural, denominator); denominator /= gcd_var; numerator_natural /= gcd_var; - println!("{numerator_natural} * sqrt({numerator_irrational})"); - println!("----------------------"); - println!("{denominator}"); let mut sqrt = MySqrt { numerator_natural, numerator_irrational, @@ -170,24 +161,17 @@ fn get_strings(left_part: Rational, sqrt_delta: MySqrt) -> (String, String, Stri let len = std::cmp::max(string_a.len(), format!("{d}").len()) + 2; let len_b = std::cmp::max(string_b.len(), format!("{d}").len()) + 2; - println!("len {len}, b {len_b}"); - let len1 = len - string_a.len(); let len2 = len - d.to_string().len(); let len1b = len_b - string_b.len(); let len2b = len_b - d.to_string().len(); - - println!("len1 {len1}, 2 {len2}, b {len1b} 2b {len2b}"); - - println!("{a} {b} {c} {d}"); - - let mut string1 = String::from(""); - let mut string2 = String::from(""); - let mut string3 = String::from(""); - let mut string4 = String::from(""); - let mut string5 = String::from(""); - let mut string6 = String::from(""); + let string1; + let string2; + let string3; + let string4; + let string5; + let string6; if d != 1 { string1 = format!("{}{string_a}{}", " ".repeat(len1 / 2), " ".repeat(len1 / 2 + len1 % 2)); @@ -220,26 +204,10 @@ fn degree_two_complex(a: GaussianRational, b: GaussianRational, c: GaussianRatio let b = b / a; let c = c / a; let delta = b * b - 4 * c; - println!("this is complex {:?}", delta); let sqrt_delta = comp_sqrt(delta); - println!("{:?}", sqrt_delta); - println!("b {:?}", b); let b = b / GaussianRational::new(Rational::new(-2, 1), Rational::new(0, 1)); - println!("BEWARE IM GOING TO PRINT THE SOLUTIONS MDR"); - - -/* - - 2362 / 473 + sqrt(7896511271) \ 49 / -473 + sqrt(7896511271) \ - ---- + sqrt | ------------------------ | + -- i sign sqrt | ------------------------- | i - 32 \ 45150 / 13 \ 45150 / - -*/ - - - let mut sign = '+'; if sqrt_delta.sign < 0 { sign = '-'; @@ -290,12 +258,6 @@ fn degree_two_complex(a: GaussianRational, b: GaussianRational, c: GaussianRatio string2 = format!("{string2}{sign} sqrt | {s5} | i"); string3 = format!("{string3} \\{s6}/ "); -/* - let string1 = format!("{}{}{} / {s1} \\ {}{}{} / {s4} \\ ", " ".repeat(space1 / 2), b.real.numerator, " ".repeat(space1 / 2 + space1 % 2), " ".repeat(space2 / 2), b.imaginary.numerator, " ".repeat(space2 / 2 + space2 % 2)); - let string2 = format!("{} + sqrt | {s2} | + {} i {sign} sqrt | {s5} | i", "-".repeat(b_real_len), "-".repeat(b_imag_len)); - let string3 = format!("{}{}{} \\ {s3} / {}{}{} \\ {s6} / ", " ".repeat(space3 / 2), b.real.denominator, " ".repeat(space3 / 2 + space3 % 2), " ".repeat(space4 / 2), b.imaginary.denominator, " ".repeat(space4 / 2 + space4 % 2)); - */ - println!("{string1}"); println!("{string2}"); println!("{string3}"); @@ -365,15 +327,14 @@ fn print_degree_two_real(left_part: Rational, sqrt_delta: MySqrt, imaginary: boo let len = std::cmp::max(string.chars().count(), format!("{d}").len()) + 2; - println!("{a} {b} {c} {d}"); - - println!("len {len} s_len {}", string.len()); - println!("{string}"); - if b != 0 || c != 0 { - println!("The two solutions are:"); + if imaginary == true { + println!("Discriminant is less than zero. The two complex solutions are:"); + } else { + println!("Discriminant is more than zero. The two solutions are:"); + } } else { - println!("The solution is:"); + println!("Discriminant is zero. The solution is:"); } if d != 1 { println!("{}{string}", " ".repeat(4 + (len - string.chars().count()) / 2)); @@ -387,23 +348,15 @@ fn print_degree_two_real(left_part: Rational, sqrt_delta: MySqrt, imaginary: boo } fn degree_two_real(a: Rational, b: Rational, c: Rational) { - println!("a {:?} b {:?} c {:?}", a, b ,c); let mut delta = b * b - 4 * a * c; - println!("delta {:?}", delta); - println!("anumera {:?}", a.numerator()); let mut imaginary = false; if delta.numerator < 0 { imaginary = true; - println!("imag {:?}", delta); delta = -1 * delta; - println!("imag {:?}", delta); } let mut sqrt_delta = sqrt(delta, a); let mut left_part = (-1 * b) / (2 * a); - println!("sqrt delta {:?}", sqrt_delta); - println!("left part {:?}", left_part); - let tmp = left_part.denominator; left_part.denominator *= sqrt_delta.denominator; @@ -451,36 +404,4 @@ pub fn solve(mut equation: Vec) -> Vec { _ => println!("Polynomial of degree greater than 2 detected, I can't solve that !"), } vec![] -} - - -/* - - 2362 / 473 + sqrt(7896511271) \ 49 / -473 + sqrt(7896511271) \ - ---- + sqrt | ------------------------ | + -- i sign sqrt | ------------------------- | i - 32 \ 45150 / 13 \ 45150 / - -*/ - -/* - / -sqrt | - \ - - - / 473 + sqrt(7896511271) \ / -473 + sqrt(7896511271) \ -2362 + 37 * sqrt | ------------------------ | + 46 * sqrt | ------------------------- | - \ 45150 / \ 45150 / - ---------------------------------------------------------------------------------------------- + - - 21615671563 - - / -473 + sqrt(7896511271) \ / 473 + sqrt(7896511271) \ -2362 + 37 * sqrt | ------------------------ | - 46 * sqrt | ------------------------- | - \ 45150 / \ 45150 / - ---------------------------------------------------------------------------------------------- i - - 21615671563 - */ +} \ No newline at end of file