Last active
June 26, 2018 11:59
-
-
Save janbrohl/eda33eb83475f5089a78e5a0b6185c63 to your computer and use it in GitHub Desktop.
Eingeschränkter Rechner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="de"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Eingeschränkter Rechner</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/4.4.2/math.min.js" | |
integrity="sha256-EQV4YufMtvIwabqDMp7fTc4CKjbfB9NPYzYbabeNClI=" | |
crossorigin="anonymous"></script> | |
</head> | |
<body> | |
<!-- Ein- und Ausgabe --> | |
<label for="rechnung_in">Rechnung:</label> | |
<input type="text" id="rechnung_in"> | |
<output id="rechnung_out" for="rechnung_in"></output> | |
<!-- Programmcode --> | |
<script> | |
'use strict'; | |
math.config({ 'number': 'Fraction' }); // Versuche Zahlen als Bruch zu betrachten | |
//var filter=new RegExp('^[ 0-9.()*/+-]+$','i'); // Einschränkung auf Grundrechenarten | |
var filter = new RegExp('^[ 0-9.()^*/+-]+$', 'i'); // Einschränkung auf Grundrechenarten und Potenz | |
var rechnung_in = document.getElementById('rechnung_in'); | |
var rechnung_out = document.getElementById('rechnung_out'); | |
function berechne() { | |
var rechnung = rechnung_in.value; | |
var match = rechnung.match(filter); | |
var ergebnis; | |
if (match == rechnung) { // Prüfen ob potentiell gültige Eingabe | |
var tree = null; | |
try { | |
tree = math.parse(rechnung); | |
} catch (e) { | |
if (e.char) { | |
ergebnis = 'Eingabefehler nach "' + rechnung.slice(0, e.char) + '"'; | |
} else { | |
ergebnis = 'Eingabefehler: ' + e | |
} | |
} | |
if (tree != null) { // Erfolgreich geparst? | |
try { | |
ergebnis = tree.eval(); | |
} catch (e) { | |
ergebnis = 'Auswertungsfehler: ' + e; | |
} | |
} | |
} else { | |
ergebnis = 'Eingabefehler nach "' + match + '"'; | |
} | |
rechnung_out.value = ergebnis; | |
} | |
rechnung_in.addEventListener('input', berechne); | |
</script> | |
<!-- Hier endet der eigentliche Rechner --> | |
<hr> | |
<a href="https://saythanks.io/to/janbrohl"> | |
<img src="https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg" alt="Say Thanks!"> | |
</a> | |
<small> | |
<p xmlns:dct="http://purl.org/dc/terms/" xmlns:vcard="http://www.w3.org/2001/vcard-rdf/3.0#"> | |
<a rel="license" href="http://creativecommons.org/publicdomain/zero/1.0/"> | |
<img src="https://licensebuttons.net/p/zero/1.0/80x15.png" style="border-style: none;" alt="CC0" /> | |
</a> | |
<br /> To the extent possible under law, | |
<a rel="dct:publisher" href="https://gist.github.com/janbrohl/eda33eb83475f5089a78e5a0b6185c63"> | |
<span property="dct:title">Jan Brohl</span> | |
</a> | |
has waived all copyright and related or neighboring rights to | |
<span property="dct:title">Eingeschränkter Rechner</span>. This work is published from: | |
<span property="vcard:Country" datatype="dct:ISO3166" content="DE" about="https://gist.github.com/janbrohl/eda33eb83475f5089a78e5a0b6185c63"> | |
Deutschland</span>. | |
</p> | |
<p> | |
Diese Seite benutzt die | |
<a href="http://mathjs.org/">math.js-Bibliothek</a>: | |
<br> Copyright (C) 2013-2018 Jos de Jong [email protected] | |
<br> Licensed under the | |
<a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a> | |
</p> | |
</small> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment