Skip to content
Snippets Groups Projects
Commit 78898ff4 authored by thibault.capt's avatar thibault.capt
Browse files

last update

parent b4554495
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ import java.util.List; ...@@ -4,6 +4,7 @@ import java.util.List;
public class Equation { public class Equation {
private String sens; private String sens;
private int nbContraintes;
private final List<Double> funcObj; private final List<Double> funcObj;
private final List<Double> rightVec; private final List<Double> rightVec;
private final Double[][] mat; private final Double[][] mat;
...@@ -37,6 +38,19 @@ public class Equation { ...@@ -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() { public List<Double> getFuncObj() {
return funcObj; return funcObj;
} }
...@@ -61,11 +75,17 @@ public class Equation { ...@@ -61,11 +75,17 @@ public class Equation {
this.mat[w][h] = n; 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.sens = null;
this.funcObj = new ArrayList<>(); this.funcObj = new ArrayList<>();
this.rightVec = new ArrayList<>(); this.rightVec = new ArrayList<>();
mat = new Double[width][5]; this.nbContraintes = h;
mat = new Double[w][h];
} }
/** /**
...@@ -99,7 +119,7 @@ public class Equation { ...@@ -99,7 +119,7 @@ public class Equation {
public int createEq(String[] elements, int line) { public int createEq(String[] elements, int line) {
try { try {
// vecteur de droite // vecteur de droite
switch (elements[5]) { switch (elements[getNbContraintes()]) {
case "=": // Si ax=b => ax <= b; ax >= b case "=": // Si ax=b => ax <= b; ax >= b
double res = Double.parseDouble(elements[elements.length - 1]); double res = Double.parseDouble(elements[elements.length - 1]);
// Vecteur de droite // Vecteur de droite
...@@ -110,7 +130,7 @@ public class Equation { ...@@ -110,7 +130,7 @@ public class Equation {
else setRightVec(-res); else setRightVec(-res);
// Matrice // Matrice
for (int i = 0; i < 5; i++) { for (int i = 0; i < getNbContraintes(); i++) {
double tmp = Double.parseDouble(elements[i]); double tmp = Double.parseDouble(elements[i]);
setMat(tmp, line, i); setMat(tmp, line, i);
// Si 0.0, ne pas écrire -0.0 // Si 0.0, ne pas écrire -0.0
...@@ -125,7 +145,7 @@ public class Equation { ...@@ -125,7 +145,7 @@ public class Equation {
setRightVec(-Double.parseDouble(elements[elements.length - 1])); setRightVec(-Double.parseDouble(elements[elements.length - 1]));
// Matrice // Matrice
for (int i = 0; i < 5; i++) for (int i = 0; i < getNbContraintes(); i++)
setMat(-Double.parseDouble(elements[i]), line, i); setMat(-Double.parseDouble(elements[i]), line, i);
break; break;
case "<=": case "<=":
...@@ -133,7 +153,7 @@ public class Equation { ...@@ -133,7 +153,7 @@ public class Equation {
setRightVec(Double.parseDouble(elements[elements.length - 1])); setRightVec(Double.parseDouble(elements[elements.length - 1]));
// Matrice // Matrice
for (int i = 0; i < 5; i++) for (int i = 0; i < getNbContraintes(); i++)
setMat(Double.parseDouble(elements[i]), line, i); setMat(Double.parseDouble(elements[i]), line, i);
break; break;
default: default:
...@@ -149,7 +169,7 @@ public class Equation { ...@@ -149,7 +169,7 @@ public class Equation {
/** /**
* Print les vecteurs et la matrice * Print les vecteurs et la matrice
*/ */
public void printEq(int width) { public void printEq(int w) {
// Sens // Sens
System.out.println("Sens: " + getSens()); System.out.println("Sens: " + getSens());
...@@ -161,8 +181,8 @@ public class Equation { ...@@ -161,8 +181,8 @@ public class Equation {
System.out.println("Matrice Amxn:"); System.out.println("Matrice Amxn:");
// Matrice Amxn // Matrice Amxn
for (int i = 0; i < width; i++) { for (int i = 0; i < w; i++) {
for (int j = 0; j < 5; j++) { for (int j = 0; j < getNbContraintes(); j++) {
if (getMatAtId(i, j) < 0.0) if (getMatAtId(i, j) < 0.0)
System.out.print(getMatAtId(i, j) + " "); System.out.print(getMatAtId(i, j) + " ");
else else
......
...@@ -18,6 +18,16 @@ public class Main { ...@@ -18,6 +18,16 @@ public class Main {
} }
return count - 1; // A cause de la premiere ligne 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 * Fonction main
...@@ -31,8 +41,10 @@ public class Main { ...@@ -31,8 +41,10 @@ public class Main {
String[] elements; String[] elements;
int widthMat = nbLines(sc); int widthMat = nbLines(sc);
sc = new Scanner(f); // remettre le scanner à la première ligne 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; int line = 0;
Equation eq = new Equation(widthMat*2, contraintes); // lignes supp au cas où, il y a des "="
// Max / Min + function obj // Max / Min + function obj
String firstLine = sc.nextLine(); String firstLine = sc.nextLine();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment