diff --git a/log.txt b/log.txt new file mode 100644 index 0000000000000000000000000000000000000000..8956e375593c28ef6c7049b792bc8ab1bff657b8 --- /dev/null +++ b/log.txt @@ -0,0 +1 @@ +2022/10/17 15:08:50 Micro started diff --git a/src/Equation.java b/src/Equation.java index 577e486005b481a705752c0c3b3223058302ef60..9de8852e81b47f737ddb8a07f5bfdf871239db4c 100644 --- a/src/Equation.java +++ b/src/Equation.java @@ -1,47 +1,111 @@ import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class Equation { - private String _sens; - private String _funcObj; - private final ArrayList<String> _sc; + private String sens; + private final List<Double> funcObj; + private final List<Double> rightVec; + private final Double[][] mat; + /** + * getter sens + * @return min | max | null + */ public String getSens() { - return _sens; + return sens; } - public void setSens(String _sens) { - this._sens = _sens; + /** + * Setter sens + * @param sens max | min + */ + public void setSens(String sens) { + switch (sens) { + case "min", "max" -> this.sens = sens; + default -> { + this.sens = null; + System.err.println("Le sens ne peut que être min ou max"); + System.exit(1); + } + } } - public String getFuncObj() { - return _funcObj; + public List<Double> getFuncObj() { + return funcObj; } - public void setFuncObj(String funcObj) { - this._funcObj = funcObj; + public void setFuncObj(double d) { + this.funcObj.add(d); + } + public List<Double> getRightVec() { + return rightVec; } - public ArrayList<String> getSc() { - return _sc; + public void setRightVec(double d) { + this.rightVec.add(d); } - public void setSc(String s) { - this._sc.add(s); + public Double[][] getMat() { + return mat; } - public Equation() { - setSens(null); - setFuncObj(null); - this._sc = new ArrayList<>(); + public void setMat(Double n, int w, int h) { + this.mat[w][h] = n; } - public void printEq() { - System.out.println("Sens: " + getSens()); - System.out.println("Fonction objective: " + getFuncObj()); - System.out.println("S.C.:"); - for (String s: getSc()) { - System.out.println(s); + public Equation(int width) { + this.sens = null; + this.funcObj = new ArrayList<>(); + this.rightVec = new ArrayList<>(); + mat = new Double[width][5]; + } + + public void setFirstLine(String[] elements) { + try { + setSens(elements[0]); + for (int i = 1; i < elements.length; i++){ + setFuncObj(Double.parseDouble(elements[i])); + } + } catch (NumberFormatException ex) { + ex.printStackTrace(); + } + } + + public void createEq(String[] elements, int line) { + addInRightVec(elements); + addInMat(elements, line); + } + + public void addInRightVec(String[] elements){ + try { + setRightVec(Double.parseDouble(elements[elements.length - 1])); + } catch (NumberFormatException ex) { + ex.printStackTrace(); } + } + + public void addInMat(String[] elements, int line) { + try { + for (int i = 0; i < 5; i++) { + setMat(Double.parseDouble(elements[i]), line, i); + } + } catch(NumberFormatException ex) { + ex.printStackTrace(); + } + } + + public void printEq(){ + // Sens + System.out.println("Sens: " + getSens()); + + // Fonction obj + System.out.println("Fonction obj: " + getFuncObj()); + + // Vecteur membre de droite + System.out.println("Vecteur membre de droite: " + getRightVec()); + // Matrice Amxn + System.out.println("Matrice Amxn: " + Arrays.deepToString(getMat())); } } diff --git a/src/Main.java b/src/Main.java index 9d37a7759fe0f47bd3beed8f6849b6bacde033d5..d320e2f0506359db25962b67900a566cb458fb82 100644 --- a/src/Main.java +++ b/src/Main.java @@ -3,49 +3,43 @@ import java.io.FileNotFoundException; import java.util.Scanner; public class Main { - - private static void getFirstLine(Equation eq, String[] elements) { - // Première ligne différente - eq.setSens(elements[0]); - StringBuilder funcObj = new StringBuilder(); - for (int i = 1; i < elements.length; i++) - if(i == elements.length - 1) - funcObj.append(elements[i]); - else - funcObj.append(elements[i]).append(", "); - - eq.setFuncObj(String.valueOf(funcObj)); - } - - private static void newSC(Equation eq, String[] elements) { - StringBuilder str = new StringBuilder(); - for (int i = 0; i < elements.length; i++ ) { - if(i == elements.length - 1) - str.append(elements[i]); - else - str.append(elements[i]).append(", "); + private static Integer nbLines(Scanner sc) { + int count = 0; + while (sc.hasNextLine()) { + count++; + sc.nextLine(); } - eq.setSc(String.valueOf(str)); + return count - 1; // A cause de la premiere ligne } - public static void main(String[] args) { try { File f = new File("src/input.txt"); Scanner sc = new Scanner(f); String[] elements; - Equation eq = new Equation(); + int widthMat = nbLines(sc); + sc = new Scanner(f); // remettre le scanner à la première ligne + Equation eq = new Equation(widthMat); + int line = 0; - // Première ligne différente + // Max / Min + function obj String firstLine = sc.nextLine(); elements = firstLine.split(";"); - getFirstLine(eq, elements); + eq.setFirstLine(elements); - // Le reste - while (sc.hasNextLine()) { - String tmp = sc.nextLine(); - elements = tmp.split(";"); - newSC(eq, elements); + if(eq.getSens().equals("min")) { + // S.C. + while (sc.hasNextLine()) { + String tmp = sc.nextLine(); + elements = tmp.split(";"); + eq.createEq(elements, line); + line++; + } + } else { + // TODO reverse eq + System.out.println("Reverse eq"); } + + // Print eq.printEq(); sc.close(); } catch (FileNotFoundException e) {