Application 01
Énoncé
-
Vous pouvez visualiser l'énoncé de l'exercice
Solution
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> body { font-family: Arial, sans-serif; background-color: #f1f1f1; } fieldset { width: 360px; border: 3px solid #556B2F; border-radius: 10px; padding: 10px 20px; text-align: left; margin: 0 auto; background-color: #fff; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2); } legend { color: #59663F; } label { display: block; margin-top: 4px; color: #59663F; } input[type="text"] { width: 70%; padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 5px; } input[type="radio"] { margin-right: 5px; -ms-transform: scale(1.5); /* IE 9 */ -webkit-transform: scale(1.5); /* Chrome, Safari, Opera */ transform: scale(1.5); } input[type=radio]#Yellow { accent-color: pink; } input[type=radio]#Green { accent-color: rgb(0, 255, 0); } input[type=radio]#auto { accent-color: auto; } input[type=radio]#Red { accent-color: #FF0000; } input[type=radio]#blue { accent-color: #4400ff; } #resultat { width: 70%; padding: 10px; font-size: 16px; border: 1px solid #ccc; border-radius: 5px; margin-top: 10px; } button { padding: 10px 20px; font-size: 16px; border: none; border-radius: 5px; background-color: #556B2F; color: #fff; cursor: pointer; } button:hover { background-color: #8FBC8F; } </style> <script> function calculer() { var x = Number(document.frmCalculatrice.var1.value); var y = Number(document.frmCalculatrice.var2.value); var resultat = document.getElementById('resultat'); if (document.frmCalculatrice.choix[0].checked) { if (y !== 0) { resultat.value = (x / y); } else { alert("Opération invalide !"); resultat.value = ""; } } else if (document.frmCalculatrice.choix[1].checked) { resultat.value = (x + y); } else if (document.frmCalculatrice.choix[2].checked) { resultat.value = (x - y); } else if (document.frmCalculatrice.choix[3].checked) { resultat.value = (x * y); } else if (document.frmCalculatrice.choix[4].checked) { resultat.value = (x % y); } } </script> </head> <body> <center> <fieldset> <legend>Petite calculatrice</legend> <center> <form name="frmCalculatrice"> <label for="var1"><b>Variable 1:</b> <input type="text" name="var1" id="var1"></label> <label for="var2"><b>Variable 2:</b> <input type="text" name="var2" id="var2"></label> <div style="padding-left: 25%;"><br> <label><b>Opérateurs :</b></label> <label><input type="radio" name="choix" id="Yellow">Division</label> <label><input type="radio" name="choix" id="Green">Addition</label> <label><input type="radio" name="choix" id="auto">Soustraction</label> <label><input type="radio" name="choix" id="Red">Multiplication</label> <label><input type="radio" name="choix" id="blue">Reste de la division</label> </div><br> <button type="button" onclick="calculer()">Calculer</button> <button type="reset">Effacer</button><br><br> <label for="resultat"><b>Résultat de l'opération :</b></label> <input type="text" id="resultat"> </form> </center> </fieldset> </center> </body> </html>