diff --git a/src/helpers/input.rs b/src/helpers/input.rs new file mode 100644 index 0000000..0747c0b --- /dev/null +++ b/src/helpers/input.rs @@ -0,0 +1,38 @@ +use std::io::{self, Write}; +pub fn string_input(prompt: &str) -> String { + print!("{}", prompt); + let mut input = String::new(); + let _ = io::stdout().flush(); + io::stdin() + .read_line(&mut input) + .expect("Error reading from STDIN"); + input.trim().to_string() +} + +pub fn int_input(prompt: &str) -> usize { + print!("{}", prompt); + let mut input = String::new(); + let _ = io::stdout().flush(); + io::stdin() + .read_line(&mut input) + .expect("Error reading from STDIN"); + //try to parse the input as usize else return max usize + match input.trim().parse::() { + Ok(i) => i, + Err(_) => { + usize::max_value() + } + } +} + +pub fn float_input(prompt: &str) -> f64 { + print!("{}", prompt); + let mut input = String::new(); + let _ = io::stdout().flush(); + io::stdin() + .read_line(&mut input) + .expect("Error reading from STDIN"); + //try to parse the input as f64 else return max f64 + let output = input.trim().parse::().unwrap(); + output +} diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 7f39a04..481a947 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1 +1,2 @@ pub mod table_printers; +pub mod input; diff --git a/src/simplex/simplex.rs b/src/simplex/simplex.rs index 2ff19f7..532cb79 100644 --- a/src/simplex/simplex.rs +++ b/src/simplex/simplex.rs @@ -1,6 +1,10 @@ -use std::io::{self, Write}; use std::vec::Vec; use crate::helpers::table_printers::{print_table, print_first_table}; +use crate::helpers::input::{ + string_input, + int_input, + float_input, +}; struct Coll { restriction: Vec, sign: String, @@ -78,53 +82,6 @@ fn check_optimal(table: &Vec>) -> bool { } } -//fn print_first_table(table: &Vec>){ -// let mut print_table = Table::new(); -// let mut titles: Vec = vec![]; -// for title in 0..table[0].len(){ -// if title == table[1].len() - 1 { -// titles.push(Cell::new("rhs")); -// } else if title < table[0].len() / 2 { -// titles.push(Cell::new(&format!("x{}",title+1))); -// } else { -// titles.push(Cell::new(&format!("s{}",title - table[0].len() / 2 + 1))); -// } -// } -// print_table.add_row(Row::new(titles)); -// for i in 0..table.len() { -// let mut cells: Vec = vec![]; -// for j in 0..table[i].len(){ -// cells.push(Cell::new(&table[i][j].to_string())); -// }; -// print_table.add_row(Row::new(cells)); -// }; -// print_table.printstd(); -// -//} -// -// -//fn print_table(table: &Vec>,pivot_col: &usize, pivot_row: &usize) { -// let mut print_table = Table::new(); -// let mut titles: Vec = vec![]; -// for title in 0..table[0].len(){ -// if title == table[0].len() - 1 { -// titles.push(Cell::new("rhs")); -// } else if title < table[0].len() / 2 { -// titles.push(Cell::new(&format!("x{}",title+1))); -// } else { -// titles.push(Cell::new(&format!("s{}",title - table[0].len() / 2 + 1))); -// } -// } -// print_table.add_row(Row::new(titles)); -// for i in 0..table.len() { -// let mut cells: Vec = vec![]; -// for j in 0..table[i].len(){ -// cells.push(Cell::new(&table[i][j].to_string()).style_spec(if j == *pivot_col || i == *pivot_row+1{ "Fgb"} else {""})); -// }; -// print_table.add_row(Row::new(cells)); -// }; -// print_table.printstd(); -//} fn pivot_table(table: &mut Vec>, pivot_row: usize, pivot_col: usize) -> Vec> { // firnd the pivot point @@ -216,43 +173,3 @@ fn fill_coll(amount_restrictions: usize, amount_vars: usize) -> Vec { } colls } - - - -pub fn string_input(prompt: &str) -> String { - print!("{}", prompt); - let mut input = String::new(); - let _ = io::stdout().flush(); - io::stdin() - .read_line(&mut input) - .expect("Error reading from STDIN"); - input.trim().to_string() -} - -pub fn int_input(prompt: &str) -> usize { - print!("{}", prompt); - let mut input = String::new(); - let _ = io::stdout().flush(); - io::stdin() - .read_line(&mut input) - .expect("Error reading from STDIN"); - //try to parse the input as usize else return max usize - match input.trim().parse::() { - Ok(i) => i, - Err(_) => { - usize::max_value() - } - } -} - -pub fn float_input(prompt: &str) -> f64 { - print!("{}", prompt); - let mut input = String::new(); - let _ = io::stdout().flush(); - io::stdin() - .read_line(&mut input) - .expect("Error reading from STDIN"); - //try to parse the input as f64 else return max f64 - let output = input.trim().parse::().unwrap(); - output -}