mirror of
https://github.com/mrfluffy-dev/lpr.git
synced 2026-01-17 02:10:33 +00:00
added table printting helper
This commit is contained in:
1
src/helpers/mod.rs
Normal file
1
src/helpers/mod.rs
Normal file
@@ -0,0 +1 @@
|
|||||||
|
pub mod table_printers;
|
||||||
49
src/helpers/table_printers.rs
Normal file
49
src/helpers/table_printers.rs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
use prettytable::{Table, Row, Cell};
|
||||||
|
use std::vec::Vec;
|
||||||
|
pub fn print_first_table(table: &Vec<Vec<f64>>){
|
||||||
|
let mut print_table = Table::new();
|
||||||
|
let mut titles: Vec<Cell> = 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<Cell> = 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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn print_table(table: &Vec<Vec<f64>>,pivot_col: &usize, pivot_row: &usize) {
|
||||||
|
let mut print_table = Table::new();
|
||||||
|
let mut titles: Vec<Cell> = 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<Cell> = 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();
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
mod simplex;
|
mod simplex;
|
||||||
|
mod helpers;
|
||||||
use simplex::simplex::simplex;
|
use simplex::simplex::simplex;
|
||||||
fn main() {
|
fn main() {
|
||||||
simplex();
|
simplex();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use prettytable::{Table, Row, Cell};
|
use crate::helpers::table_printers::{print_table, print_first_table};
|
||||||
struct Coll {
|
struct Coll {
|
||||||
restriction: Vec<f64>,
|
restriction: Vec<f64>,
|
||||||
sign: String,
|
sign: String,
|
||||||
@@ -78,53 +78,53 @@ fn check_optimal(table: &Vec<Vec<f64>>) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_first_table(table: &Vec<Vec<f64>>){
|
//fn print_first_table(table: &Vec<Vec<f64>>){
|
||||||
let mut print_table = Table::new();
|
// let mut print_table = Table::new();
|
||||||
let mut titles: Vec<Cell> = vec![];
|
// let mut titles: Vec<Cell> = vec![];
|
||||||
for title in 0..table[0].len(){
|
// for title in 0..table[0].len(){
|
||||||
if title == table[1].len() - 1 {
|
// if title == table[1].len() - 1 {
|
||||||
titles.push(Cell::new("rhs"));
|
// titles.push(Cell::new("rhs"));
|
||||||
} else if title < table[0].len() / 2 {
|
// } else if title < table[0].len() / 2 {
|
||||||
titles.push(Cell::new(&format!("x{}",title+1)));
|
// titles.push(Cell::new(&format!("x{}",title+1)));
|
||||||
} else {
|
// } else {
|
||||||
titles.push(Cell::new(&format!("s{}",title - table[0].len() / 2 + 1)));
|
// titles.push(Cell::new(&format!("s{}",title - table[0].len() / 2 + 1)));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
print_table.add_row(Row::new(titles));
|
// print_table.add_row(Row::new(titles));
|
||||||
for i in 0..table.len() {
|
// for i in 0..table.len() {
|
||||||
let mut cells: Vec<Cell> = vec![];
|
// let mut cells: Vec<Cell> = vec![];
|
||||||
for j in 0..table[i].len(){
|
// for j in 0..table[i].len(){
|
||||||
cells.push(Cell::new(&table[i][j].to_string()));
|
// cells.push(Cell::new(&table[i][j].to_string()));
|
||||||
};
|
// };
|
||||||
print_table.add_row(Row::new(cells));
|
// print_table.add_row(Row::new(cells));
|
||||||
};
|
// };
|
||||||
print_table.printstd();
|
// print_table.printstd();
|
||||||
|
//
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
|
//
|
||||||
fn print_table(table: &Vec<Vec<f64>>,pivot_col: &usize, pivot_row: &usize) {
|
//fn print_table(table: &Vec<Vec<f64>>,pivot_col: &usize, pivot_row: &usize) {
|
||||||
let mut print_table = Table::new();
|
// let mut print_table = Table::new();
|
||||||
let mut titles: Vec<Cell> = vec![];
|
// let mut titles: Vec<Cell> = vec![];
|
||||||
for title in 0..table[0].len(){
|
// for title in 0..table[0].len(){
|
||||||
if title == table[0].len() - 1 {
|
// if title == table[0].len() - 1 {
|
||||||
titles.push(Cell::new("rhs"));
|
// titles.push(Cell::new("rhs"));
|
||||||
} else if title < table[0].len() / 2 {
|
// } else if title < table[0].len() / 2 {
|
||||||
titles.push(Cell::new(&format!("x{}",title+1)));
|
// titles.push(Cell::new(&format!("x{}",title+1)));
|
||||||
} else {
|
// } else {
|
||||||
titles.push(Cell::new(&format!("s{}",title - table[0].len() / 2 + 1)));
|
// titles.push(Cell::new(&format!("s{}",title - table[0].len() / 2 + 1)));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
print_table.add_row(Row::new(titles));
|
// print_table.add_row(Row::new(titles));
|
||||||
for i in 0..table.len() {
|
// for i in 0..table.len() {
|
||||||
let mut cells: Vec<Cell> = vec![];
|
// let mut cells: Vec<Cell> = vec![];
|
||||||
for j in 0..table[i].len(){
|
// 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 {""}));
|
// 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.add_row(Row::new(cells));
|
||||||
};
|
// };
|
||||||
print_table.printstd();
|
// print_table.printstd();
|
||||||
}
|
//}
|
||||||
|
|
||||||
fn pivot_table(table: &mut Vec<Vec<f64>>, pivot_row: usize, pivot_col: usize) -> Vec<Vec<f64>> {
|
fn pivot_table(table: &mut Vec<Vec<f64>>, pivot_row: usize, pivot_col: usize) -> Vec<Vec<f64>> {
|
||||||
// firnd the pivot point
|
// firnd the pivot point
|
||||||
|
|||||||
Reference in New Issue
Block a user