diff --git a/README.md b/README.md index e69de29..efbc868 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,93 @@ +````markdown +# 🧼 Calculatrice Autonome en JavaScript (Vanilla JS & Tailwind CSS) + +Un composant de calculatrice moderne, modulaire et entiĂšrement autonome dĂ©veloppĂ© en **JavaScript pur** (ES6 Classes) et stylisĂ© avec **Tailwind CSS**. Conçu pour ĂȘtre facilement intĂ©grĂ© dans n'importe quelle page web via une simple balise ` + + + +
+ + + + + + + + +``` + +--- + +## 📌 Commandes Git Utiles + +Pour sauvegarder vos modifications et les envoyer sur votre dĂ©pĂŽt distant (sur la branche `main`) : + +```bash +# VĂ©rifier l'Ă©tat des fichiers +git status + +# Ajouter les modifications +git add . + +# Valider avec un message +git commit -m "Mise Ă  jour de la calculatrice autonome" + +# Envoyer sur la branche principale +git push origin main + +``` + +--- + +## 📄 Licence + +Ce projet est libre d'utilisation et de modification pour vos projets personnels ou professionnels. + +``` + +``` diff --git a/calculatrice.js b/calculatrice.js index 38ea3d4..094ca84 100644 --- a/calculatrice.js +++ b/calculatrice.js @@ -1,4 +1,4 @@ -class SimpleCalculator { +class ScientificCalculator { constructor(containerId) { this.container = document.getElementById(containerId); if (!this.container) return; @@ -10,36 +10,49 @@ class SimpleCalculator { init() { this.container.innerHTML = ` -
- +
+
-
0
+
0
- -
- - - - + +
+ + + + + + + + + + + + + - + + - + + - + - + + +
`; @@ -61,6 +74,8 @@ class SimpleCalculator { this.appendNumber(value); } else if (action === "operator") { this.chooseOperation(value); + } else if (action === "scientific") { + this.scientificOperation(value); } else if (action === "calculate") { this.compute(); } else if (action === "clear") { @@ -92,6 +107,41 @@ class SimpleCalculator { this.currentInput = ""; } + scientificOperation(func) { + const current = parseFloat(this.currentInput); + if (isNaN(current)) return; + + let result; + switch (func) { + case "sqrt": + result = current < 0 ? "Erreur" : Math.sqrt(current); + break; + case "sqr": + result = Math.pow(current, 2); + break; + case "inv": + result = current === 0 ? "Erreur" : 1 / current; + break; + case "sin": + // Conversion en radians pour le calcul trigonométrique standard + result = Math.sin(current * (Math.PI / 180)); + break; + case "cos": + result = Math.cos(current * (Math.PI / 180)); + break; + case "tan": + result = Math.tan(current * (Math.PI / 180)); + break; + default: + return; + } + + this.currentInput = + typeof result === "number" + ? parseFloat(result.toFixed(8)).toString() + : result; + } + compute() { let computation; const prev = parseFloat(this.previousInput); @@ -115,7 +165,7 @@ class SimpleCalculator { return; } - this.currentInput = computation.toString(); + this.currentInput = parseFloat(computation.toFixed(8)).toString(); this.operation = null; this.previousInput = ""; } @@ -127,7 +177,7 @@ class SimpleCalculator { } deleteDigit() { - if (this.currentInput.length === 1) { + if (this.currentInput.length === 1 || this.currentInput === "Erreur") { this.currentInput = "0"; } else { this.currentInput = this.currentInput.slice(0, -1); diff --git a/calculatrice_basic.js b/calculatrice_basic.js new file mode 100644 index 0000000..38ea3d4 --- /dev/null +++ b/calculatrice_basic.js @@ -0,0 +1,146 @@ +class SimpleCalculator { + constructor(containerId) { + this.container = document.getElementById(containerId); + if (!this.container) return; + this.currentInput = "0"; + this.previousInput = ""; + this.operation = null; + this.init(); + } + + init() { + this.container.innerHTML = ` +
+ +
+
+
0
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + +
+
+ `; + + this.prevScreen = this.container.querySelector("#calc-prev"); + this.currentScreen = this.container.querySelector("#calc-current"); + this.addListeners(); + } + + addListeners() { + this.container.addEventListener("click", (e) => { + const button = e.target.closest("button"); + if (!button) return; + + const action = button.getAttribute("data-action"); + const value = button.getAttribute("data-value"); + + if (action === "number") { + this.appendNumber(value); + } else if (action === "operator") { + this.chooseOperation(value); + } else if (action === "calculate") { + this.compute(); + } else if (action === "clear") { + this.clear(); + } else if (action === "delete") { + this.deleteDigit(); + } + + this.updateScreen(); + }); + } + + appendNumber(number) { + if (number === "." && this.currentInput.includes(".")) return; + if (this.currentInput === "0" && number !== ".") { + this.currentInput = number; + } else { + this.currentInput += number; + } + } + + chooseOperation(op) { + if (this.currentInput === "") return; + if (this.previousInput !== "") { + this.compute(); + } + this.operation = op; + this.previousInput = this.currentInput; + this.currentInput = ""; + } + + compute() { + let computation; + const prev = parseFloat(this.previousInput); + const current = parseFloat(this.currentInput); + if (isNaN(prev) || isNaN(current)) return; + + switch (this.operation) { + case "+": + computation = prev + current; + break; + case "-": + computation = prev - current; + break; + case "*": + computation = prev * current; + break; + case "/": + computation = current === 0 ? "Erreur" : prev / current; + break; + default: + return; + } + + this.currentInput = computation.toString(); + this.operation = null; + this.previousInput = ""; + } + + clear() { + this.currentInput = "0"; + this.previousInput = ""; + this.operation = null; + } + + deleteDigit() { + if (this.currentInput.length === 1) { + this.currentInput = "0"; + } else { + this.currentInput = this.currentInput.slice(0, -1); + } + } + + updateScreen() { + this.currentScreen.innerText = + this.currentInput === "" ? "0" : this.currentInput; + if (this.operation != null) { + this.prevScreen.innerText = `${this.previousInput} ${this.operation}`; + } else { + this.prevScreen.innerText = ""; + } + } +} diff --git a/index.html b/index.html index 813edaf..173c035 100644 --- a/index.html +++ b/index.html @@ -3,23 +3,18 @@ - Calculatrice Autonome - + Calculatrice Scientifique - -
+
- - - diff --git a/index_basic.html b/index_basic.html new file mode 100644 index 0000000..813edaf --- /dev/null +++ b/index_basic.html @@ -0,0 +1,26 @@ + + + + + + Calculatrice Autonome + + + + + +
+ + + + + + + +