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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| <!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>...</title>
<style type="text/css">
#calcul {
font-style:normal;
margin-left:50px;
padding:5px;
font-size:16px;
width:800px;
white-space:normal;
letter-spacing:4px;
}
#ou {
visibility:hidden;
margin-left:50px;
padding:5px;
font-style:normal;
font-size:18px;
white-space:normal;
letter-spacing:4px
}
#plus {
margin-left:-5px;
padding:0px;
visibility:hidden;
font-size:18px;
border-width:0px;
color:#ff0000;
}
#total {
width:300px;
padding:5px;
font-size:16px;
border-width:0px;
font-weight:bold;
color:#000fff;
margin-left:50px;
}
</style>
<body>
<div id="cadre">
<label for="calcul" style="font-weight:bold;margin-left:50px;">Entrez une instruction mathématique puis quittez la zone</label><br />
<input type="text" id="calcul" onchange="go(this.value)" /><br />
<span id="ou">?</span>
<span id="plus">!</span>
<p style="margin-top:100px">
<span style="font-weight:bold;margin-left:50px;">Résultat</span><br />
<input type="text" id="total" readonly="readonly" />
</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,})|([\\+\\x\\*\\/\\-]\\s[\\+\\x\\*\\/\\-])/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",
"/^[\\*\\/]+/":"Opérateur fautif en début de chaîne"
}
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 supprimé 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
}
function e(q){
return document.getElementById(q)
}
function suite(q){
var par1=q.match(/\(/g);
var par2=q.match(/\)/g);
if((par1!=null && par2!=null && par1.length !=par2.length) || (par1==null && par2!=null) || (par1!=null && par2==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").style.color="#000fff";
e("total").value=String(dec).search(/\.0+$/)!=-1 ? String(dec).substring(0,String(dec).indexOf(".")) :dec;
}
catch(er){
e("total").style.color="#ff0000";
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> |