Compare commits
3 Commits
725ca99712
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e690e88bed | |||
| 074b1edbb6 | |||
| 384544c5ae |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -5,7 +5,7 @@ version = 4
|
||||
[[package]]
|
||||
name = "fractions"
|
||||
version = "0.1.0"
|
||||
source = "git+https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git?branch=main#885bbfeebed047a62ef86eae5bcc44137e2ae127"
|
||||
source = "git+https://laentropia-homelab.tail7368da.ts.net/laentropia/Rusty-Fractions.git?branch=main#2fea33ca7080b4e05b6e4df0cad90d7e7c97f24f"
|
||||
|
||||
[[package]]
|
||||
name = "matrix"
|
||||
|
||||
35
src/lib.rs
35
src/lib.rs
@@ -1,4 +1,4 @@
|
||||
use fractions::{Fraction, FractionError};
|
||||
pub use fractions::{Fraction, FractionError};
|
||||
use std::cmp::min;
|
||||
use std::error::Error;
|
||||
use std::fmt::{self, Debug, Display};
|
||||
@@ -580,19 +580,30 @@ impl Mul for Matrix {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Matrix {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut display = String::new();
|
||||
let mut index = 0;
|
||||
for _i in 0..self.columns {
|
||||
display += "{";
|
||||
for _k in 0..self.rows {
|
||||
display += &format!(" {},", self.data[index]);
|
||||
index += 1;
|
||||
impl fmt::Display for Matrix {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let strings: Vec<String> = self.data.iter().map(|x| x.to_string()).collect();
|
||||
|
||||
let max_width = strings.iter().map(|s| s.len()).max().unwrap_or(0);
|
||||
|
||||
for r in 0..self.rows {
|
||||
write!(f, "{{ ")?;
|
||||
|
||||
for c in 0..self.columns {
|
||||
let idx = r * self.columns + c;
|
||||
let val = &strings[idx];
|
||||
|
||||
write!(f, "{:>width$}", val, width = max_width)?;
|
||||
|
||||
if c != self.columns - 1 {
|
||||
write!(f, ", ")?;
|
||||
}
|
||||
}
|
||||
display += " }\n";
|
||||
|
||||
writeln!(f, " }}")?;
|
||||
}
|
||||
write!(f, "{}", display)
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user