Lab 2 formatting tweaks.
This commit is contained in:
parent
59fc1ad596
commit
698bfe413e
|
@ -13,7 +13,7 @@ fn read_row() -> Vec<i16> {
|
||||||
let raw_row: String = text_io::read!("{}\n");
|
let raw_row: String = text_io::read!("{}\n");
|
||||||
let trimmed_row: &str = raw_row.trim();
|
let trimmed_row: &str = raw_row.trim();
|
||||||
match trimmed_row.split(" ").all(|value| {
|
match trimmed_row.split(" ").all(|value| {
|
||||||
value.chars().all(|char| char.is_numeric())
|
value.chars().all(|char| char.is_numeric() || char == '-')
|
||||||
&& value.parse::<i32>().unwrap() <= i16::MAX as i32
|
&& value.parse::<i32>().unwrap() <= i16::MAX as i32
|
||||||
&& value.parse::<i32>().unwrap() >= i16::MIN as i32
|
&& value.parse::<i32>().unwrap() >= i16::MIN as i32
|
||||||
}) {
|
}) {
|
||||||
|
@ -37,10 +37,11 @@ fn generate_row(row_index: u16, length: u16) -> Vec<i16> {
|
||||||
fn read_matrix() -> Vec<Vec<i16>> {
|
fn read_matrix() -> Vec<Vec<i16>> {
|
||||||
let rows: u16 = protected_u16_read("the number of rows");
|
let rows: u16 = protected_u16_read("the number of rows");
|
||||||
loop {
|
loop {
|
||||||
print!("Generate matrix or input manually [g/i]: ");
|
print!("Generate [s]quare or [r]ectangle matrix or [i]nput manually [s/r/i]: ");
|
||||||
let option: String = text_io::read!();
|
let option: String = text_io::read!();
|
||||||
match option.to_lowercase().as_str() {
|
match option.to_lowercase().as_str() {
|
||||||
"g" => {
|
"s" => return (0..rows).map(|i| generate_row(i, rows)).collect(),
|
||||||
|
"r" => {
|
||||||
let columns: u16 = protected_u16_read("the number of columns");
|
let columns: u16 = protected_u16_read("the number of columns");
|
||||||
return (0..rows).map(|i| generate_row(i, columns)).collect();
|
return (0..rows).map(|i| generate_row(i, columns)).collect();
|
||||||
}
|
}
|
||||||
|
@ -81,22 +82,16 @@ fn average_of_row(row: &Vec<i16>) -> f32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_matrix(matrix: &Vec<Vec<i16>>) -> String {
|
fn format_matrix(matrix: &Vec<Vec<i16>>) -> String {
|
||||||
let max_element_width = matrix
|
let max_width = matrix
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| row.iter().max().unwrap())
|
.map(|row| {
|
||||||
|
row.iter()
|
||||||
|
.map(|element| element.to_string().len())
|
||||||
|
.max()
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
.max()
|
.max()
|
||||||
.unwrap()
|
.unwrap();
|
||||||
.to_string()
|
|
||||||
.len();
|
|
||||||
let min_element_width = matrix
|
|
||||||
.iter()
|
|
||||||
.map(|row| row.iter().min().unwrap())
|
|
||||||
.min()
|
|
||||||
.unwrap()
|
|
||||||
.to_string()
|
|
||||||
.len();
|
|
||||||
|
|
||||||
let max_width = max_element_width.max(min_element_width);
|
|
||||||
|
|
||||||
let mut formatted_matrix: String = matrix
|
let mut formatted_matrix: String = matrix
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -112,6 +107,20 @@ fn format_matrix(matrix: &Vec<Vec<i16>>) -> String {
|
||||||
return formatted_matrix;
|
return formatted_matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_column(column: &Vec<f32>) -> String {
|
||||||
|
let max_width = column
|
||||||
|
.iter()
|
||||||
|
.map(|element| element.to_string().len())
|
||||||
|
.max()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
return column
|
||||||
|
.iter()
|
||||||
|
.map(|element| format!("| {:max_width$} |", element))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let full_vec_test: Vec<Vec<i16>> = read_matrix();
|
let full_vec_test: Vec<Vec<i16>> = read_matrix();
|
||||||
println!("\nMatrix B:\n{}\n", format_matrix(&full_vec_test));
|
println!("\nMatrix B:\n{}\n", format_matrix(&full_vec_test));
|
||||||
|
@ -124,8 +133,11 @@ fn main() {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| average_of_row(row))
|
.map(|row| average_of_row(row))
|
||||||
.collect();
|
.collect();
|
||||||
println!("Averages for each row in a×B:");
|
println!(
|
||||||
row_averages
|
"Averages for each row in a×B:\n{}",
|
||||||
.iter()
|
format_column(&row_averages)
|
||||||
.for_each(|element| println!("{}", element))
|
);
|
||||||
|
/*row_averages
|
||||||
|
.iter()
|
||||||
|
.for_each(|element| println!("{}", element))*/
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue