From 78898ff455b0b82f2a06cb12f74873b3f3bb7972 Mon Sep 17 00:00:00 2001
From: "thibault.capt" <thibault.capt@hes-so.ch>
Date: Thu, 3 Nov 2022 23:14:02 +0100
Subject: [PATCH] last update

---
 src/Equation.java | 40 ++++++++++++++++++++++++++++++----------
 src/Main.java     | 14 +++++++++++++-
 2 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/src/Equation.java b/src/Equation.java
index 80bdff2..99f059f 100644
--- a/src/Equation.java
+++ b/src/Equation.java
@@ -4,6 +4,7 @@ import java.util.List;
 
 public class Equation {
     private String sens;
+    private int nbContraintes;
     private final List<Double> funcObj;
     private final List<Double> rightVec;
     private final Double[][] mat;
@@ -37,6 +38,19 @@ public class Equation {
         }
     }
 
+    public int getNbContraintes() {
+        if(this.nbContraintes != -1)
+            return this.nbContraintes;
+        else {
+            System.err.println("Une erreur a été détectée");
+            return -1;
+        }
+    }
+
+    public void setNbContraintes(int nbContraintes) {
+        this.nbContraintes = nbContraintes;
+    }
+
     public List<Double> getFuncObj() {
         return funcObj;
     }
@@ -61,11 +75,17 @@ public class Equation {
         this.mat[w][h] = n;
     }
 
-    public Equation(int width) {
+    /**
+     * Constructeur
+     * @param w hauteur de la matrice
+     * @param h largeur de la matrice
+     */
+    public Equation(int w, int h) {
         this.sens = null;
         this.funcObj = new ArrayList<>();
         this.rightVec = new ArrayList<>();
-        mat = new Double[width][5];
+        this.nbContraintes = h;
+        mat = new Double[w][h];
     }
 
     /**
@@ -94,12 +114,12 @@ public class Equation {
      * Créer le vecteurs éléments de droite et la matrice Amxn quand le sens est à min
      *
      * @param elements les elements
-     * @param line     la line en question
+     * @param line la line en question
      */
     public int createEq(String[] elements, int line) {
         try {
             // vecteur de droite
-            switch (elements[5]) {
+            switch (elements[getNbContraintes()]) {
                 case "=": // Si ax=b => ax <= b; ax >= b
                     double res = Double.parseDouble(elements[elements.length - 1]);
                     // Vecteur de droite
@@ -110,7 +130,7 @@ public class Equation {
                     else setRightVec(-res);
 
                     // Matrice
-                    for (int i = 0; i < 5; i++) {
+                    for (int i = 0; i < getNbContraintes(); i++) {
                         double tmp = Double.parseDouble(elements[i]);
                         setMat(tmp, line, i);
                         // Si 0.0, ne pas écrire -0.0
@@ -125,7 +145,7 @@ public class Equation {
                     setRightVec(-Double.parseDouble(elements[elements.length - 1]));
 
                     // Matrice
-                    for (int i = 0; i < 5; i++)
+                    for (int i = 0; i < getNbContraintes(); i++)
                         setMat(-Double.parseDouble(elements[i]), line, i);
                     break;
                 case "<=":
@@ -133,7 +153,7 @@ public class Equation {
                     setRightVec(Double.parseDouble(elements[elements.length - 1]));
 
                     // Matrice
-                    for (int i = 0; i < 5; i++)
+                    for (int i = 0; i < getNbContraintes(); i++)
                         setMat(Double.parseDouble(elements[i]), line, i);
                     break;
                 default:
@@ -149,7 +169,7 @@ public class Equation {
     /**
      * Print les vecteurs et la matrice
      */
-    public void printEq(int width) {
+    public void printEq(int w) {
         // Sens
         System.out.println("Sens: " + getSens());
 
@@ -161,8 +181,8 @@ public class Equation {
 
         System.out.println("Matrice Amxn:");
         // Matrice Amxn
-        for (int i = 0; i < width; i++) {
-            for (int j = 0; j < 5; j++) {
+        for (int i = 0; i < w; i++) {
+            for (int j = 0; j < getNbContraintes(); j++) {
                 if (getMatAtId(i, j) < 0.0)
                     System.out.print(getMatAtId(i, j) + " ");
                 else
diff --git a/src/Main.java b/src/Main.java
index a403120..287a07c 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -18,6 +18,16 @@ public class Main {
         }
         return count - 1; // A cause de la premiere ligne
     }
+    /**
+     * Compter le nombre d'inéquations S.C
+     * @param sc scanner
+     * @return le nombre d'inéquations S.C
+     */
+    private static Integer nbContraintes(Scanner sc) {
+        String firstLine = sc.nextLine();
+        String[] elements = firstLine.split(";");
+        return elements.length - 1; // Enlever le sens de la fonction
+    }
 
     /**
      * Fonction main
@@ -31,8 +41,10 @@ public class Main {
             String[] elements;
             int widthMat = nbLines(sc);
             sc = new Scanner(f); // remettre le scanner à la première ligne
-            Equation eq = new Equation(widthMat*2); // lignes supp au cas où, il y a des "="
+            int contraintes = nbContraintes(sc);
+            sc = new Scanner(f); // remettre le scanner à la première ligne
             int line = 0;
+            Equation eq = new Equation(widthMat*2, contraintes); // lignes supp au cas où, il y a des "="
 
             // Max / Min + function obj
             String firstLine = sc.nextLine();
-- 
GitLab