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

manque reverse eq

parent e89b2266
No related branches found
No related tags found
No related merge requests found
2022/10/17 15:08:50 Micro started
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Equation { public class Equation {
private String _sens; private String sens;
private String _funcObj; private final List<Double> funcObj;
private final ArrayList<String> _sc; private final List<Double> rightVec;
private final Double[][] mat;
/**
* getter sens
* @return min | max | null
*/
public String getSens() { 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 List<Double> getFuncObj() {
return funcObj;
} }
public String getFuncObj() { public void setFuncObj(double d) {
return _funcObj; this.funcObj.add(d);
}
public List<Double> getRightVec() {
return rightVec;
} }
public void setFuncObj(String funcObj) { public void setRightVec(double d) {
this._funcObj = funcObj; this.rightVec.add(d);
} }
public ArrayList<String> getSc() { public Double[][] getMat() {
return _sc; return mat;
} }
public void setSc(String s) { public void setMat(Double n, int w, int h) {
this._sc.add(s); this.mat[w][h] = n;
}
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 Equation() { public void createEq(String[] elements, int line) {
setSens(null); addInRightVec(elements);
setFuncObj(null); addInMat(elements, line);
this._sc = new ArrayList<>(); }
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(){ public void printEq(){
// Sens
System.out.println("Sens: " + getSens()); System.out.println("Sens: " + getSens());
System.out.println("Fonction objective: " + getFuncObj());
System.out.println("S.C.:");
for (String s: getSc()) {
System.out.println(s);
}
// 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()));
} }
} }
...@@ -3,49 +3,43 @@ import java.io.FileNotFoundException; ...@@ -3,49 +3,43 @@ import java.io.FileNotFoundException;
import java.util.Scanner; import java.util.Scanner;
public class Main { public class Main {
private static Integer nbLines(Scanner sc) {
private static void getFirstLine(Equation eq, String[] elements) { int count = 0;
// Première ligne différente while (sc.hasNextLine()) {
eq.setSens(elements[0]); count++;
StringBuilder funcObj = new StringBuilder(); sc.nextLine();
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(", ");
} }
eq.setSc(String.valueOf(str)); return count - 1; // A cause de la premiere ligne
} }
public static void main(String[] args) { public static void main(String[] args) {
try { try {
File f = new File("src/input.txt"); File f = new File("src/input.txt");
Scanner sc = new Scanner(f); Scanner sc = new Scanner(f);
String[] elements; 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(); String firstLine = sc.nextLine();
elements = firstLine.split(";"); elements = firstLine.split(";");
getFirstLine(eq, elements); eq.setFirstLine(elements);
// Le reste if(eq.getSens().equals("min")) {
// S.C.
while (sc.hasNextLine()) { while (sc.hasNextLine()) {
String tmp = sc.nextLine(); String tmp = sc.nextLine();
elements = tmp.split(";"); elements = tmp.split(";");
newSC(eq, elements); eq.createEq(elements, line);
line++;
} }
} else {
// TODO reverse eq
System.out.println("Reverse eq");
}
// Print
eq.printEq(); eq.printEq();
sc.close(); sc.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment