This repository has been archived on 2021-11-01. You can view files and clone it, but cannot push or open issues or pull requests.
exo1-js/js/main.js

128 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-03-31 17:24:45 +02:00
window.addEventListener("load", () => main());
const main = () => {
let def = {
a: true,
b: false,
c: 5
};
let exemple = "a && b || c"
2021-03-31 18:25:35 +02:00
console.log(exemple, "\n=", parseLogicalExpression(def, exemple));
2021-03-31 18:17:09 +02:00
console.log("vrai rep d'apres JS:", def.a && def.b || def.c);
2021-03-31 17:24:45 +02:00
console.log("\n\n-----\n\n ");
2021-03-31 18:17:09 +02:00
exemple = "!a && b || c && a || c && b && a"
2021-03-31 18:25:35 +02:00
console.log(exemple, "\n=", parseLogicalExpression(def, exemple));
2021-03-31 18:17:09 +02:00
console.log("vrai rep d'apres JS:", !def.a && def.b || def.c && def.a || def.c && def.b && def.a);
2021-03-31 17:24:45 +02:00
console.log("\n\n-----\n\n ");
2021-03-31 18:17:09 +02:00
exemple = "a || b || a && b && c || c"
2021-03-31 18:25:35 +02:00
console.log(exemple, "\n=", parseLogicalExpression(def, exemple));
2021-03-31 18:17:09 +02:00
console.log("vrai rep d'apres JS:", def.a || def.b || def.a && def.b && def.c || def.c);
2021-03-31 17:24:45 +02:00
console.log("\n\n-----\n\n ");
2021-03-31 18:17:09 +02:00
exemple = "a || a && !a && !b || !a"
2021-03-31 18:25:35 +02:00
console.log(exemple, "\n=", parseLogicalExpression(def, exemple));
2021-03-31 18:17:09 +02:00
console.log("vrai rep d'apres JS:", def.a || def.a && !def.a && !def.b || !def.a);
2021-03-31 17:24:45 +02:00
console.log("\n\n-----\n\n ");
2021-03-31 18:17:09 +02:00
exemple = "(a || b) || (a && b) && c"
2021-03-31 18:25:35 +02:00
console.log(exemple, "\n=", parseLogicalExpression(def, exemple));
2021-03-31 18:32:53 +02:00
console.log("vrai rep d'apres JS:", (def.a || def.b) || (def.a && def.b) || def.c);
/* console.log("\n\n-----\n\n ");
exemple = "(a || b) || (a || (a && b) && c) && c"
console.log(exemple, "\n=", parseLogicalExpression(def, exemple));
console.log("vrai rep d'apres JS:", (def.a || def.b) || (def.a || (def.a && def.b) && def.c) && c); */
2021-03-31 17:24:45 +02:00
};
const parseLogicalExpression = (subject = Object, expression = String) => {
// on ajoute les portes non au subject
Object.keys(subject).forEach(key => {
subject["!" + key] = !subject[key];
});
/* ----------------------------------------------------------------------
Explication regex pour pas oublier
( ... ) -> groupe
| -> séparé les morceaux qu'on recherche
[^)] -> récupérer le contenu entre ce qu'on a spécifié, ici ( et )
+ -> au moins un élément
---------------------------------------------------------------------- */
const splitter = (expression = String) => expression.split(/(&&|\|\||\([^)]+\))/).map(str => str.trim());
const replaceWithRightValues = (subject = Object, tableau = Array) => {
let i = 0;
tableau.forEach(expression => {
Object.keys(subject).forEach(key => {
if(key == expression) tableau[i] = subject[key];
});
i++;
});
}
const gate = (gate = String, array = Array) => {
let posGate = array.findIndex(element => typeof element == "string")
if(array[posGate] == gate) {
2021-03-31 18:24:42 +02:00
// console.log("je vais travailler avec la porte", gate, "sur", array);
// console.log("je fais,", array[posGate - 1], gate, array[posGate + 1])
if(gate == "&&") array[posGate + 1] = array[posGate - 1] && array[posGate + 1];
2021-03-31 17:24:45 +02:00
else if(gate == "||") array[posGate + 1] = array[posGate - 1] || array[posGate + 1];
else return console.error("bad gate type");
2021-03-31 18:24:42 +02:00
// console.log("res:", array[posGate + 1]);
2021-03-31 17:24:45 +02:00
array.splice(posGate - 1, 2);
2021-03-31 18:24:42 +02:00
// console.log("finit:", array);
} // else console.warn("la porte", gate, "n'a pas la priorité de calcule", array);
2021-03-31 17:24:45 +02:00
2021-03-31 18:24:42 +02:00
// console.log("\n ");
2021-03-31 17:24:45 +02:00
return array;
};
const resolution = (problem = Array) => {
while(problem.length > 1) ["&&", "||"].forEach(element => problem = gate(element, problem));
};
2021-03-31 18:17:09 +02:00
const bracketCalculator = (problem = Array) => {
// on calcule les parenthèses
let calculatedBrackets = [] // inialisation du tableau qui va recevoir les réponses des parenthese
problem.forEach(element => {
if(typeof element == "string") { // ajout de tous les éléments sauf les parentheses
if(element.startsWith("(") == 0 || element.endsWith(")") == 0) calculatedBrackets.push(element);
} else calculatedBrackets.push(element)
if(typeof element == "string") if(element.startsWith("(") && element.endsWith(")")) {
element = splitter(element.replace("(", "").replace(")", ""));
replaceWithRightValues(subject, element);
resolution(element);
calculatedBrackets.push(element[0]) // ajout des réponses aux parenthèses
};
});
return calculatedBrackets;
}
2021-03-31 17:24:45 +02:00
// expression propre
let arrayLogicalExpression = splitter(expression);
2021-03-31 18:17:09 +02:00
// -> on retire les string vide
arrayLogicalExpression = arrayLogicalExpression.filter(element => element != "");
2021-03-31 18:24:42 +02:00
// console.log(arrayLogicalExpression);
2021-03-31 17:24:45 +02:00
// on remplace par les valeurs de subject
replaceWithRightValues(subject, arrayLogicalExpression);
// on supprime les parenthèses
2021-03-31 18:17:09 +02:00
arrayLogicalExpression = bracketCalculator(arrayLogicalExpression);
2021-03-31 17:24:45 +02:00
// on résoud les problemes
resolution(arrayLogicalExpression);
2021-03-31 18:24:42 +02:00
// onsole.log("resultat final:", arrayLogicalExpression);
2021-03-31 17:24:45 +02:00
return arrayLogicalExpression[0];
};