From bde5b24c0dd469d5c629a3613494d627d8512e36 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 31 Mar 2021 17:24:45 +0200 Subject: [PATCH] first commit --- .gitignore | 3 ++ index.html | 20 ++++++++++ js/main.js | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 index.html create mode 100644 js/main.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d9828e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +hints.txt +logicalexpressions.txt +.vscode/ diff --git a/index.html b/index.html new file mode 100644 index 0000000..86bb245 --- /dev/null +++ b/index.html @@ -0,0 +1,20 @@ + + + + + + + default + + + + + + + + + + + + + diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..4a2132e --- /dev/null +++ b/js/main.js @@ -0,0 +1,113 @@ +window.addEventListener("load", () => main()); + +const main = () => { + + let def = { + a: true, + b: false, + c: 5 + }; + + let exemple = "a && b || c" + + console.log("with", def, "parsing logical expression of \"" + exemple + "\""); + console.log("result : " + parseLogicalExpression(def, exemple)); + + console.log("\n\n-----\n\n "); + console.log(parseLogicalExpression(def, "!a && b || c && a || c && b && a")); + console.log("vrai rep d'apres JS:", !true && false || 5 && true || 5 && false && true); + + console.log("\n\n-----\n\n "); + console.log(parseLogicalExpression(def, "a || b || a && b && c || c")); + console.log("vrai rep d'apres JS:", true || false || true && false && 5 || 5); + + console.log("\n\n-----\n\n "); + console.log(parseLogicalExpression(def, "a || a && !a && !b || !a")); + console.log("vrai rep d'apres JS:", true || true && !true && !false || !true); + + console.log("\n\n-----\n\n "); + console.log(parseLogicalExpression(def, "(a || b) || (a && b) && c")); + console.log("vrai rep d'apres JS:", true || false || true && false && 5 || 5); + +}; + +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) { + // 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]; + else if(gate == "||") array[posGate + 1] = array[posGate - 1] || array[posGate + 1]; + else return console.error("bad gate type"); + // console.log("res:", array[posGate + 1]); + array.splice(posGate - 1, 2); + // console.log("finit:", array); + } // else console.warn("la porte", gate, "n'a pas la priorité de calcule", array); + + // console.log("\n "); + return array; + }; + + const resolution = (problem = Array) => { + while(problem.length > 1) ["&&", "||"].forEach(element => problem = gate(element, problem)); + }; + + // expression propre + let arrayLogicalExpression = splitter(expression); + console.log(arrayLogicalExpression); + + // on remplace par les valeurs de subject + replaceWithRightValues(subject, arrayLogicalExpression); + + // on supprime les parenthèses + // -> on retire les string vide + arrayLogicalExpression = arrayLogicalExpression.filter(element => element != ""); + + // 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 + resolution(arrayLogicalExpression); + + console.log(arrayLogicalExpression); + return arrayLogicalExpression[0]; +};