This commit is contained in:
Mylloon 2021-03-31 18:17:09 +02:00
parent bde5b24c0d
commit fd209cd17d

View file

@ -9,25 +9,28 @@ const main = () => {
}; };
let exemple = "a && b || c" let exemple = "a && b || c"
console.error(exemple, "\n=", parseLogicalExpression(def, exemple));
console.log("with", def, "parsing logical expression of \"" + exemple + "\""); console.log("vrai rep d'apres JS:", def.a && def.b || def.c);
console.log("result : " + parseLogicalExpression(def, exemple));
console.log("\n\n-----\n\n "); console.log("\n\n-----\n\n ");
console.log(parseLogicalExpression(def, "!a && b || c && a || c && b && a")); exemple = "!a && b || c && a || c && b && a"
console.log("vrai rep d'apres JS:", !true && false || 5 && true || 5 && false && true); console.error(exemple, "\n=", parseLogicalExpression(def, exemple));
console.log("vrai rep d'apres JS:", !def.a && def.b || def.c && def.a || def.c && def.b && def.a);
console.log("\n\n-----\n\n "); console.log("\n\n-----\n\n ");
console.log(parseLogicalExpression(def, "a || b || a && b && c || c")); exemple = "a || b || a && b && c || c"
console.log("vrai rep d'apres JS:", true || false || true && false && 5 || 5); console.error(exemple, "\n=", parseLogicalExpression(def, exemple));
console.log("vrai rep d'apres JS:", def.a || def.b || def.a && def.b && def.c || def.c);
console.log("\n\n-----\n\n "); console.log("\n\n-----\n\n ");
console.log(parseLogicalExpression(def, "a || a && !a && !b || !a")); exemple = "a || a && !a && !b || !a"
console.log("vrai rep d'apres JS:", true || true && !true && !false || !true); console.error(exemple, "\n=", parseLogicalExpression(def, exemple));
console.log("vrai rep d'apres JS:", def.a || def.a && !def.a && !def.b || !def.a);
console.log("\n\n-----\n\n "); console.log("\n\n-----\n\n ");
console.log(parseLogicalExpression(def, "(a || b) || (a && b) && c")); exemple = "(a || b) || (a && b) && c"
console.log("vrai rep d'apres JS:", true || false || true && false && 5 || 5); console.error(exemple, "\n=", parseLogicalExpression(def, exemple));
console.log("vrai rep d'apres JS:", def.a || def.b || def.a && def.b && def.c || def.c);
}; };
@ -61,17 +64,17 @@ const parseLogicalExpression = (subject = Object, expression = String) => {
const gate = (gate = String, array = Array) => { const gate = (gate = String, array = Array) => {
let posGate = array.findIndex(element => typeof element == "string") let posGate = array.findIndex(element => typeof element == "string")
if(array[posGate] == gate) { if(array[posGate] == gate) {
// console.log("je vais travailler avec la porte", gate, "sur", array); console.log("je vais travailler avec la porte", gate, "sur", array);
// console.log("je fais,", array[posGate - 1], gate, array[posGate + 1]) console.log("je fais,", array[posGate - 1], gate, array[posGate + 1])
if(gate == "&&") array[posGate + 1] = array[posGate - 1] && array[posGate + 1]; if(gate == "&&") array[posGate + 1] = array[posGate - 1] && array[posGate + 1];
else if(gate == "||") array[posGate + 1] = array[posGate - 1] || array[posGate + 1]; else if(gate == "||") array[posGate + 1] = array[posGate - 1] || array[posGate + 1];
else return console.error("bad gate type"); else return console.error("bad gate type");
// console.log("res:", array[posGate + 1]); console.log("res:", array[posGate + 1]);
array.splice(posGate - 1, 2); array.splice(posGate - 1, 2);
// console.log("finit:", array); console.log("finit:", array);
} // else console.warn("la porte", gate, "n'a pas la priorité de calcule", array); } else console.warn("la porte", gate, "n'a pas la priorité de calcule", array);
// console.log("\n "); console.log("\n ");
return array; return array;
}; };
@ -79,35 +82,43 @@ const parseLogicalExpression = (subject = Object, expression = String) => {
while(problem.length > 1) ["&&", "||"].forEach(element => problem = gate(element, problem)); while(problem.length > 1) ["&&", "||"].forEach(element => problem = gate(element, problem));
}; };
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;
}
// expression propre // expression propre
let arrayLogicalExpression = splitter(expression); let arrayLogicalExpression = splitter(expression);
// -> on retire les string vide
arrayLogicalExpression = arrayLogicalExpression.filter(element => element != "");
console.log(arrayLogicalExpression); console.log(arrayLogicalExpression);
// on remplace par les valeurs de subject // on remplace par les valeurs de subject
replaceWithRightValues(subject, arrayLogicalExpression); replaceWithRightValues(subject, arrayLogicalExpression);
// on supprime les parenthèses // on supprime les parenthèses
// -> on retire les string vide console.log("avant bracket calculator:", arrayLogicalExpression);
arrayLogicalExpression = arrayLogicalExpression.filter(element => element != ""); arrayLogicalExpression = bracketCalculator(arrayLogicalExpression);
console.log("apres bracket calculator:", arrayLogicalExpression);
// on calcule les parenthèses
let calculatedBrackets = [] // inialisation du tableau qui va recevoir les réponses des parenthese
arrayLogicalExpression.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
};
});
arrayLogicalExpression = calculatedBrackets;
// on résoud les problemes // on résoud les problemes
resolution(arrayLogicalExpression); resolution(arrayLogicalExpression);
console.log(arrayLogicalExpression); console.log("resultat final:", arrayLogicalExpression);
return arrayLogicalExpression[0]; return arrayLogicalExpression[0];
}; };