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

finish part 1

parent 15d74d76
No related branches found
No related tags found
No related merge requests found
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class Equation { public class Equation {
...@@ -54,6 +53,10 @@ public class Equation { ...@@ -54,6 +53,10 @@ public class Equation {
return mat; return mat;
} }
public Double getMatAtId(int w, int h) {
return mat[w][h];
}
public void setMat(Double n, int w, int h) { public void setMat(Double n, int w, int h) {
this.mat[w][h] = n; this.mat[w][h] = n;
} }
...@@ -81,48 +84,85 @@ public class Equation { ...@@ -81,48 +84,85 @@ public class Equation {
} }
/** /**
* Créer le vecteurs éléments de droite et la matrice Amxn * Créer le vecteurs éléments de droite et la matrice Amxn quand le sens est à min
* @param elements * @param elements
* @param line * @param line
*/ */
public void createEq(String[] elements, int line) { public void createMinEq(String[] elements, int line) {
// Vecteur de droite try {
addInRightVec(elements); // vecteur de droite
setRightVec(Double.parseDouble(elements[elements.length - 1]));
// Matrice // Matrice
addInMat(elements, line); for (int i = 0; i < 5; i++) {
setMat(Double.parseDouble(elements[i]), line, i);
} }
/**
* Ajouter dans le vecteur de droite
* @param elements
*/
public void addInRightVec(String[] elements){
try {
setRightVec(Double.parseDouble(elements[elements.length - 1]));
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
/** /**
* Ajouter dans la matrice * Créer le vecteurs éléments de droite et la matrice Amxn quand le sens est à max
* @param elements * @param elements
* @param line * @param line
*/ */
public void addInMat(String[] elements, int line) { public int createMaxEq(String[] elements, int line) {
try { try {
// vecteur de droite
switch(elements[5]) {
case "=": // Si ax=b => ax <= b; ax >= b
// Vecteur de droite
setRightVec(Double.parseDouble(elements[elements.length - 1]));
setRightVec(Double.parseDouble(elements[elements.length - 1]));
// Matrice
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
setMat(Double.parseDouble(elements[i]), line, i); setMat(Double.parseDouble(elements[i]), line, i);
setMat(Double.parseDouble(elements[i]), line+1, i);
} }
// On rajoute une ligne
line += 1;
break;
case ">=":
// Vecteur de droite
setRightVec(-Double.parseDouble(elements[elements.length - 1]));
// Matrice
for (int i = 0; i < 5; i++) {
setMat(-Double.parseDouble(elements[i]), line, i);
}
break;
case "<=":
// Vecteur de droite
setRightVec(Double.parseDouble(elements[elements.length - 1]));
// Matrice
for (int i = 0; i < 5; i++) {
setMat(Double.parseDouble(elements[i]), line, i);
}
break;
}
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
return line;
}
/**
* Ajouter dans le vecteur de droite
* @param elements
*/
public void addInRightVec(String[] elements){
} }
/** /**
* Print les vecteurs et la matrice * Print les vecteurs et la matrice
*/ */
public void printEq(){ public void printEq(int width){
// Sens // Sens
System.out.println("Sens: " + getSens()); System.out.println("Sens: " + getSens());
...@@ -132,7 +172,16 @@ public class Equation { ...@@ -132,7 +172,16 @@ public class Equation {
// Vecteur membre de droite // Vecteur membre de droite
System.out.println("Vecteur membre de droite: " + getRightVec()); System.out.println("Vecteur membre de droite: " + getRightVec());
System.out.println("Matrice Amxn:");
// Matrice Amxn // Matrice Amxn
System.out.println("Matrice Amxn: " + Arrays.deepToString(getMat())); for(int i = 0; i < width; i ++) {
for(int j = 0; j < 5; j ++) {
if(getMatAtId(i, j) < 0)
System.out.print(getMatAtId(i, j) + " ");
else
System.out.print(" " + getMatAtId(i, j) + " ");
}
System.out.println();
}
} }
} }
...@@ -18,7 +18,7 @@ public class Main { ...@@ -18,7 +18,7 @@ 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); Equation eq = new Equation(widthMat*2); // ajout de ligne supp si sens: max et = donc + 1 ligne
int line = 0; int line = 0;
// Max / Min + function obj // Max / Min + function obj
...@@ -26,21 +26,22 @@ public class Main { ...@@ -26,21 +26,22 @@ public class Main {
elements = firstLine.split(";"); elements = firstLine.split(";");
eq.setFirstLine(elements); eq.setFirstLine(elements);
if(eq.getSens().equals("min")) {
// S.C. // S.C.
while (sc.hasNextLine()) { while (sc.hasNextLine()) {
String tmp = sc.nextLine(); String tmp = sc.nextLine();
elements = tmp.split(";"); elements = tmp.split(";");
eq.createEq(elements, line);
line++; if(eq.getSens().equals("min")) {
} eq.createMinEq(elements, line);
} else { } else {
// TODO reverse eq line = eq.createMaxEq(elements, line);
System.out.println("Reverse eq"); }
line++;
} }
// Print // Print
eq.printEq(); eq.printEq(line);
sc.close(); sc.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment