|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="de"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Math.js Gleichungsrechner</title> |
| 6 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.11.0/math.min.js"></script> |
| 7 | + <!-- Carefully take https://mathjs.org/examples/algebra.js.html as a documentation.--> |
| 8 | + <style> |
| 9 | + body { |
| 10 | + font-family: sans-serif; |
| 11 | + padding: 20px; |
| 12 | + } |
| 13 | + input, button { |
| 14 | + padding: 8px; |
| 15 | + font-size: 16px; |
| 16 | + } |
| 17 | + #output { |
| 18 | + margin-top: 20px; |
| 19 | + padding: 10px; |
| 20 | + background: #f4f4f4; |
| 21 | + border-radius: 6px; |
| 22 | + white-space: pre-wrap; |
| 23 | + } |
| 24 | + .highlight { |
| 25 | + border-radius: 25px; |
| 26 | + color: #AD7700; |
| 27 | + border: 2px solid #AD7700; |
| 28 | + width: 200px; |
| 29 | + padding-left: 8px; |
| 30 | + padding-right: 8px; |
| 31 | + height: 150px; |
| 32 | + } |
| 33 | + </style> |
| 34 | +</head> |
| 35 | +<body> |
| 36 | + <h2>Math.js Gleichungsrechner</h2> |
| 37 | + <p>Beispiel: <code>2x + 7 = 3x + 8</code></p> |
| 38 | + |
| 39 | + <input type="text" id="equation" placeholder="Gib eine Gleichung ein, z.B. 2x + 7 = 3x + 8" size="40"> |
| 40 | + <button onclick="calculate()">Berechnen</button> |
| 41 | + |
| 42 | + <div id="output"></div> |
| 43 | + |
| 44 | + <script> |
| 45 | + function calculate() { |
| 46 | + const input = document.getElementById("equation").value; |
| 47 | + const output = document.getElementById("output"); |
| 48 | + try { |
| 49 | + if (input.includes('=')) { |
| 50 | + // math.parseEquationString() braucht LHS und RHS |
| 51 | + const [lhs, rhs] = input.split('='); |
| 52 | + //const solution = math.solveEquation(lhs + " = " + rhs, "x"); |
| 53 | + const simplified = math.simplify(lhs).toString() + " = " + math.simplify(rhs).toString(); |
| 54 | + //output.innerText = "Vereinfachung:\n x = " + solution.toString(); |
| 55 | + output.innerHTML = "<h2>Vereinfachung:</h2>" + simplified.toString(); |
| 56 | + const nextStep = simplified.toString().replace(" = ", " - (") + ")"; |
| 57 | + //const solved = math.simplify( |
| 58 | + output.innerHTML += "<br>" + nextStep.toString(); |
| 59 | + const finalStep = math.simplify( nextStep.toString()).toString(); |
| 60 | + const a0 = math.evaluate(finalStep, {x: 0}).toString(); |
| 61 | + let result = ""; |
| 62 | + let a1 = ""; |
| 63 | + if(finalStep.includes("x")){ |
| 64 | + a1 = math.simplify(`(${finalStep}- ${a0})/x`).toString(); |
| 65 | + result += "x = " + math.simplify(`(-1)*(${a0}/${a1})`).toString(); |
| 66 | + } |
| 67 | + else if(finalStep === "0"){result = "allgemein lösbar";} |
| 68 | + else{result = "nicht lösbar";} |
| 69 | + output.innerHTML += "<br>" + finalStep.toString(); |
| 70 | + output.innerHTML += "<br><br><span class='highlight'>Beta</span> "; |
| 71 | + output.innerHTML += result.toString(); |
| 72 | + } else { |
| 73 | + const simplified = math.simplify(input).toString(); |
| 74 | + const evaluated = math.evaluate(input); |
| 75 | + output.innerText = "Vereinfacht: " + simplified + "\nErgebnis: " + evaluated; |
| 76 | + } |
| 77 | + } catch (err) { |
| 78 | + output.innerText = "Fehler: " + err.message; |
| 79 | + } |
| 80 | + } |
| 81 | + </script> |
| 82 | +</body> |
| 83 | +</html> |
0 commit comments