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

manque reverse eq

parent e89b2266
Branches
No related tags found
No related merge requests found
2022/10/17 15:08:50 Micro started
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 List<Double> getFuncObj() {
return funcObj;
}
public String getFuncObj() {
return _funcObj;
public void setFuncObj(double d) {
this.funcObj.add(d);
}
public List<Double> getRightVec() {
return rightVec;
}
public void setFuncObj(String funcObj) {
this._funcObj = funcObj;
public void setRightVec(double d) {
this.rightVec.add(d);
}
public ArrayList<String> getSc() {
return _sc;
public Double[][] getMat() {
return mat;
}
public void setSc(String s) {
this._sc.add(s);
public void setMat(Double n, int w, int h) {
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() {
setSens(null);
setFuncObj(null);
this._sc = new ArrayList<>();
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());
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;
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
if(eq.getSens().equals("min")) {
// S.C.
while (sc.hasNextLine()) {
String tmp = sc.nextLine();
elements = tmp.split(";");
newSC(eq, elements);
eq.createEq(elements, line);
line++;
}
} else {
// TODO reverse eq
System.out.println("Reverse eq");
}
// Print
eq.printEq();
sc.close();
} catch (FileNotFoundException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment