Il peut être amusant d'effectuer des calculs avec des chiffres romains, toutefois les calculs à la main nous semblent fastidieux ou on a l'impression d'avoir oublié quelque chose ! Pour résoudre le problème, créez-vous une calculatrice ! Il suffit simplement d'effectuer une conversion des nombres romains en nombre arabe et vice versa et de l'intégré à une calculatrice classique. Voici de quoi devrait avoir l'air la calculatrice :
A l'aide du code source JavaScript suivant, vous trouverez la réponse que vous souhaitez :
- <html><head>
- <script language="JavaScript" type="text/javascript">
- var eqFlg = false, lastBut = "+", lastOp = "+", newNum = true, Total = 0;
-
- function trim(chaine) {
- return chaine.replace(/(^s*)|(s*$)/g,"");
- }
-
- function RomainToNumber(num) {
- var i = 0, n = 0;
-
- num = num.toUpperCase();
- // Calcul les milliers
- while (num.charAt(i) == "M") n+=1000, i++;
-
- // Calcul les centaines
- if(num.substr(i,2) == "CM") n+=900, i+=2; else
- if(num.substr(i,2) == "CD") n+=400, i+=2;
- else {
- if(num.charAt(i) == "D") n+= 500, i++;
- while(num.charAt(i) == "C") n+=100, i++;
- }
-
- // Calcul les dizaines
- if(num.substr(i,2) == "XC") n+=90, i += 2; else
- if(num.substr(i,2) == "XL") n+=40, i += 2;
- else {
- if(num.charAt(i) == "L") n+=50, i++;
- while (num.charAt(i) == "X") n+=10, i++;
- }
-
- // Calcul les unités
- if(num.substr(i,2) == "IX") n += 9, i+=2; else
- if(num.substr(i,2) == "IV") n += 4, i += 2;
- else {
- if(num.charAt(i) == "V") n += 5, i++;
- while(num.charAt(i) == "I") n++, i++;
- }
- return(n);
- }
-
- function NumberToRomain(X) {
- S = "";
- switch(parseInt((X % 10000) / 1000)) {
- case 4: S+="MMMM";break;
- case 3: S+="MMM";break;
- case 2: S+="MM";break;
- case 1: S+="M";break;
- }
- switch(parseInt((X % 1000) / 100)) {
- case 9: S+="CM";break;
- case 8: S+="DCCC";break;
- case 7: S+="DCC";break;
- case 6: S+="DC";break;
- case 5: S+="D";break;
- case 4: S+="CD";break;
- case 3: S+="CCC";break;
- case 2: S+="CC";break;
- case 1: S+="C";break;
- }
- switch(parseInt((X % 100) / 10)) {
- case 9: S+="XC";break;
- case 8: S+="LXXX";break;
- case 7: S+="LXX";break;
- case 6: S+="LX";break;
- case 5: S+="L";break;
- case 4: S+="XL";break;
- case 3: S+="XXX";break;
- case 2: S+="XX";break;
- case 1: S+="X";break;
- }
- switch(X % 10) {
- case 9: S+="IX";break;
- case 8: S+="VIII";break;
- case 7: S+="VII";break;
- case 6: S+="VI";break;
- case 5: S+="V";break;
- case 4: S+="IV";break;
- case 3: S+="III";break;
- case 2: S+="II";break;
- case 1: S+="I";break;
- }
- return S;
- }
-
- function ShowInput(n) {
- document.forms[0].Resultat.value = " ".substr(n.length) + n;
- }
-
- function EntNum(RomanNumber) {
- eqFlg = false; lastBut = RomanNumber;
- if(newNum) document.forms[0].elements[0].value = "", newNum = false;
- ShowInput(trim(document.forms[0].elements[0].value + RomanNumber));
- }
-
- function EntOperator(Operator) {
- if(lastBut != "+" && lastBut != "-" && lastBut != "*" && lastBut != "/") {
- var negFlg = false, n, Num = trim(document.forms[0].elements[0].value);
- if(Num.charAt(0) == "-") negFlg = true, Num = Num.substr(1);
- if(Num != ".") {
- n = RomainToNumber(Num);
- if(negFlg) n = -n;
- Total = Math.round(eval(Total + lastOp + n));
- if(Total < -4999 || Total > 4999) Num = "Erreur de débordement de valeur"; else
- if(Total == 0) Num = "."; else
- if(Total < 0) Num = "-" + NumberToRomain(-Total);
- else Num = NumberToRomain(Total);
- ShowInput(Num);
- newNum = true, eqFlg = false;
- }
- }
- lastBut = Operator, lastOp = Operator;
- }
-
- function ComputeTotal() {
- var negFlg = false, n, Num;
- if(eqFlg) Num = ".", eqFlg = false;
- else {
- Num = trim(document.forms[0].elements[0].value), eqFlg = true;
- if(Num.charAt(0) == "-") negFlg = true, Num = Num.substr(1);
- if(Num != ".") {
- n = RomainToNumber(Num);
- if(negFlg) n = -n;
- Total = Math.round(eval(Total + lastOp + n));
- if(Total < -4999 || Total > 4999) Num = "Erreur de débordement de capacité"; else
- if(Total == 0) Num = "."; else
- if(Total < 0) Num = "-" + NumberToRomain(-Total);
- else Num = NumberToRomain(Total);
- }
- lastOp = "+", newNum = true, Total = 0;
- }
- ShowInput(Num);
- }
- </script>
- <style type="text/css">
- <!--
- INPUT {
- font-family: 'Courier New', monospace;
- font-weight: bold;
- }
- -->
- </style>
- </head>
- <body>
- <table border="1" cellpadding="1" cellspacing="0">
- <tr>
- <td>
- <form>
- <table align="center">
- <tr>
- <td colspan="4">Calculatrice romaine</td>
- </tr>
- <tr>
- <td colspan="4"><input name=Resultat size="20" value=""></td>
- </tr>
- <tr>
- <td><input type="button" value=" M " onclick="EntNum('M');"></td>
- <td><input type="button" value=" D " onclick="EntNum('D');"></td>
- <td><input type="button" value=" C " onclick="EntNum('C');"></td>
- <td><input type="button" value=" + " onclick="EntOperator('+');"></td>
- </tr>
- <tr>
- <td><input type="button" value=" L " onclick="EntNum('L');"></td>
- <td><input type="button" value=" X " onclick="EntNum('X');"></td>
- <td><input type="button" value=" V " onclick="EntNum('V');"></td>
- <td><input type="button" value=" - " onclick="EntOperator('-');"></td>
- </tr>
- <tr>
- <td><input type="button" value=" I " onclick="EntNum('I');"></td>
- <td><input type="button" value=" ÷ " onclick="EntOperator('/');"></td>
- <td><input type="button" value=" × " onclick="EntOperator('*');"></td>
- <td><input type="button" value=" = " onclick="ComputeTotal();"></td>
- </tr>
- </table>
- </form>
- </td>
- </tr>
- </table>
- </body>
- </html>
Dernière mise à jour : Dimanche, le 12 janvier 2014