1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>évaluateur de calculs</title>
<style type="text/css">
</style>
<body>
<div id="cadre">
<label for="calcul">Entrez une instruction mathématique puis quittez la zone</label><br />
<input type="text" style="font-style:normal;margin-left:50px;padding:5px;font-size:16px;width:500px;white-space:normal;letter-spacing:4px" id="calcul" onchange="go(this.value)" /><br />
<span style="visibility:hidden;margin-left:50px;padding:5px;font-style:normal;font-size:18px;border-width:0px;color:#ff0000;white-space:normal;letter-spacing:4px" id="ou">?</span>
<span id="plus"style="margin-left:-5px;padding:0px;visibility:hidden;font-size:18px;border-width:0px;color:#ff0000;">!</span>
<p style="margin-top:100px">
<span>Résultat: </span>
<input type="text" id="total" readonly="readonly" style="width:500px;border-width:0px;color:#ff0000;" />
</p>
</div>
<script type="text/javascript">
var t={"x":"*","X":"*",":":"/"}
var rg={
"/[^\\+\\*\\/\\-\\(\\)\\s\\d\\.]/":"Ce n'est pas un nombre!",
"/[\\+\\*\\/\\-\\(]\\)/":"Parenthèse terminée par une expression incorrecte!",
"/[\\+\\x\\*\\/\\-]{2,}/i":"Plusieurs opérateurs se suivent!",
"/(\\D\\.)|(\\.[\\D\\s]+)|(\\.\\d+\\.)/":"Décimal raté!",
"/\\d\\s+\\d/":"Entier coupé!",
"/\\([\\/\\*]/":"Opérateur fautif en début de parenthèses"
}
var rg2={
"/\\s+/g":" ",// espaces multiples convertis en un seul
"/([^\\.\\,\\;\\d]|^)0+([\\,\\.\\;\\d])/g":"$10$2",//partie entière de plusieurs "0" réduite
"/[,;]/g":".",// conversion des virgules et point-virgules en points
"/([^\\d\\.\\,\\;])0+(\\d)/g":"$1$2",//partie entière d'un nombre réduite
"/^(0)(\\d)/g":"$2",// zéro en début de chaîne supprimer si le nombre n'est pas un décimal
"/[\\[\\]\\|\\&\\=\\!]/g":"",//crochets, opérateurs binaires et d'identité supprimés
"/([\\d\\)])(\\()/g":"$1*$2",// opérateur de multiplication rétabli entre deux parenthèses
"/(\\))([\\d\\(])/g":"$1*$2",// opérateur de multiplication rétabli ente deux parenthèses
"/\\/+/g":"/",// plusieurs opérateurs de division convertis en un seul
"/[xX:]/g":function(o){return t[o]},// conversion des opérateurs de multiplication et division
"/\\/+/g":"/",// opérateurs de division convertis en un seul
"/^[\\*\\/]+/":""// opérateur de multiplication et division supprimés en début de chaîne
}
function e(q){
return document.getElementById(q)
}
function suite(q){
if((q.match(/\(/g)!=null && q.match(/\)/g)!=null && q.match(/\(/g).length%2!=q.match(/\)/g).length%2) || (q.match(/\(/g)==null && q.match(/\)/g)!=null) || (q.match(/\(/g)!=null && q.match(/\)/g)==null)){
e("total").value="Parenthèse non fermée!";
e("plus").style.visibility="visible";
par=q.indexOf("(")!=-1?q.indexOf("("):cop.indexOf(")");
e("ou").firstChild.data=q.substring(0,par-1);
}
else if(/.*[\+\x\*\/\-]$/.test(q)){
e("total").value="Opérateur en fin d'expression!";
e("ou").firstChild.data=q.substring(0,q.length-1);
e("plus").style.visibility="visible";
}
else{
e("total").value="Erreur non identifiée.";
}
}
function go(ch){
var flag=0;
e("total").value="";
e("plus").style.visibility="hidden";
e("ou").firstChild.data="?";
for(i in rg2){
ch=ch.replace(eval(i),rg2[i])
}
e("calcul").value=ch;
try{
op=String(ch).match(/[\+\x\*\/\-]{2,}/gi);
ch=ch.toUpperCase()
ch=eval(ch)
if(op!=null){
alert("Plusieurs opérateurs se suivent...\nLe résultat est probablement erroné!");
}
dec=!isNaN(ch) ? ch.toFixed(3) : "C'est une chaîne!";
e("total").value=String(dec).search(/\.0+$/)!=-1 ? String(dec).substring(0,String(dec).indexOf(".")) :dec;
}
catch(er){
for(i in rg){
if(ch.match(eval(i))!=null){
e("total").value=rg[i];
e("plus").style.visibility="visible";
e("ou").firstChild.data=ch.substring(0,ch.match(eval(i)).index);
break;
}
flag++;
if(flag==5){
suite(ch)
}
}
}
}
</script>
</body>
</html> |
Partager