feat: various fixes

clippy: rm unnecessary borrows
structure: move struct definitions around
tests: add unit test for matrix operations
errors: add custom error types
This commit is contained in:
Zhongheng Liu 2025-01-22 20:29:02 +02:00
commit fcf4d05b8a
Signed by: steven
GPG key ID: 805A28B071DAD84B
6 changed files with 134 additions and 43 deletions

View file

@ -1,31 +1,25 @@
use std::io::stdin;
use std::{error::Error, io::stdin, str::FromStr};
use types::matrix::Matrix;
#[cfg(test)]
mod tests;
use types::{matrix::Matrix, matrix_err::ParseMatrixError};
mod types;
fn handle_input() -> Matrix {
fn handle_input() -> Result<Matrix, ParseMatrixError> {
let input = stdin();
let c = true;
let mut construct_string = String::from("");
while c {
loop {
let mut s = "".to_string();
let _ = input.read_line(&mut s);
if s == "exit\n" { break; }
if s == "exit\n" {
break;
}
construct_string += &s;
}
// println!("Constructed \"{}\"", construct_string.trim_end());
Matrix::from_str(construct_string.trim_end().to_string())
Matrix::from_str(construct_string.trim_end())
}
fn main() {
// println!("Hello, world!");
let m1 = Matrix::from_str("1,2,3\n4,5,6\n7,8,9".to_string());
let m2 = Matrix::from_str("1,1,1\n1,1,1".to_string());
let m4d = Matrix::from_str("1,2,3,4\n5,6,7,8\n9,18,11,12\n13,14,15,15".to_string());
println!("Matrix:\n{}Has determinant:{}",&m1, &m1.determinant());
println!("det(m4d): {}", &m4d.determinant());
let mi = handle_input();
println!("m from input:\n{}", mi);
// println!("row: {}, col: {}", mi.nrows, mi.ncols);
// println!("row: {}, col: {}", m2.nrows, m2.ncols);
println!("{}", &mi+&m2);
fn main() -> Result<(), Box<dyn Error>> {
let m = handle_input()?;
Ok(println!("The matrix is:\n{}", m))
}