New function : convertRomanArabicNumbers

This commit is contained in:
Divlo 2019-09-21 19:45:54 +02:00
parent cb215f60d6
commit 2c8a118be4
8 changed files with 111 additions and 0 deletions

View File

@ -25,6 +25,7 @@ Le projet est disponible sur [function.divlo.fr](https://function.divlo.fr/).
| **convertCurrency(value, currency, url)** | Convertis une valeur dans une devise dans une autre devise. [exchangeratesapi.io](https://exchangeratesapi.io/) | - value : la valeur à convertir - currency : la devise à avoir après conversion - url : l'url de la requête à l'API en fonction du paramètre dans l'url '?base=' |
| **utf8ToBin(s)** | UTF-8 vers Binaire | - s : la valeur à convertir |
| **binToUtf8(s)** | Binaire vers UTF-8 | - s : la valeur à convertir |
| **convertRomanArabicNumbers(nombre)** | Convertis un nombre arabe en nombre romain. | - nombre : le nombre à convertir |
## La liste des Fonctions Annexes :
| Nom | Description | Paramètre(s) |

View File

@ -81,6 +81,11 @@ header {
#convertIn {
width: 6.7em;
}
#roman-numerals {
width: 20%;
min-width: 200px;
padding-bottom: 20px;
}
.table {
color: white;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 KiB

View File

@ -68,6 +68,11 @@ switch ($currentpage) {
$description = "Convertis du texte (encodé en UTF-8) en binaire et l'inverse aussi.";
$image = 'https://function.divlo.fr/img/function-image/convertBinaryText.png';
break;
case '/views/function-views/convertRomanArabicNumbers.php':
$title = "Conversion d'un nombre arabe en nombre romain";
$description = "Convertis un nombre arabe en nombre romain.";
$image = 'https://function.divlo.fr/img/function-image/convertRomanArabicNumbers.png';
break;
default:
$title = 'Erreur 404';
$description = "Cette page n'existe pas!";

View File

@ -261,3 +261,54 @@ function binToUtf8(s){
return s;
}
}
// Convertis un nombre arabe en nombre romain
function convertRomanArabicNumbers(nombre) {
// Tableau contenant chaque correspondance entre un nombre arabe et un nombre romain
const correspondances =
[
[1000, "M"],
[900, "CM"],
[500, "D"],
[400, "CD"],
[100, "C"],
[90, "XC"],
[50, "L"],
[40, "XL"],
[10, "X"],
[9, "IX"],
[5, "V"],
[4, "IV"],
[1, "I"],
];
// Initialisation de la variable qui va contenir le résultat de la conversion
let chiffresRomains = "";
/*
Étapes pour écrire un nombre romain :
On vérifie quand le nombre arabe est >= à la plus grande valeur possible dans la table de correspondance des nombres romains de haut en bas puis on rajoute la lettre romaine correspondante à la plus grande valeur possible dans la variable chiffresRomains et on soustrait la valeur du chiffre romain qu'on vient d'ajouter au nombre arabe puis on répète l'opération jusqu'à nombre arabe vaut 0...
Exemple avec 27 :
27 - X (10) = 17
17 - X (10) = 7
7 - V (5) = 2
2 - I (1) = 1
1 - I (1) = 0
XXVII
*/
function extraireChiffreRomain(valeurLettre, lettres) {
while (nombre >= valeurLettre) {
chiffresRomains = chiffresRomains + lettres;
nombre = nombre - valeurLettre;
}
}
correspondances.forEach(correspondance => {
extraireChiffreRomain(correspondance[0], correspondance[1]);
})
return chiffresRomains;
}

View File

@ -209,6 +209,22 @@ $(function () {
}
});
$("#submitConvertRomanArabicNumbers").click(function()
{
let numbersValue = $('#numbersArabic').val();
if(isEmptyValue(numbersValue)) {
$('.results').html(emptyMessageError);
}
else if (!isNaN(parseInt(numbersValue))) {
let result = convertRomanArabicNumbers(parseFloat(numbersValue.replace(/\s/g,'')));
$('.results').html(`<b>${numbersValue}</b> s'écrit <b>${result}</b> en chiffres romains.`);
}
else {
$('.results').html(messageError);
}
});
/* Permet d'afficher l'heure en temps réel sur le footer */
window.onload = realDateTime('realDateTime');

View File

@ -52,6 +52,10 @@
<td><a href="./function-views/convertBinaryText.php">Conversion d'un texte en binaire et vice-versa</a></td>
<td>Convertis du texte (encodé en UTF-8) en binaire et l'inverse aussi.</td>
</tr>
<tr>
<td><a href="./function-views/convertRomanArabicNumbers.php">Conversion d'un nombre arabe en nombre romain</a></td>
<td>Convertis un nombre arabe en nombre romain.</td>
</tr>
</tbody>
</table>
</div>

View File

@ -0,0 +1,29 @@
<!-- Config -->
<?php include("../../php/config.php");?>
<!-- Header -->
<?php include("../../incl/header.php");?>
<!-- Page Content -->
<div class="container">
<h1><span class="important"><?php echo $title?></span> :</h1>
<p class="pt-3 text-center"><?php echo $description?></p>
<div class="text-center">
<img id="roman-numerals" src="/img/function-image/convertRomanArabicNumbers.png" alt="Roman Numerals">
</div>
<div class="form-group">
<label for="numbersArabic">Entrez votre nombre :</label>
<input name="numbersArabic" type="text" id="numbersArabic" placeholder="(e.g : 50')" class="form-control">
<div class="form-row text-center">
<div class="col-12">
<br>
<button type="submit" id="submitConvertRomanArabicNumbers" class="btn btn-dark text-center">Envoyer</button>
</div>
</div>
<br> <br>
<p class="results text-center"></p>
</div>
</div>
<!-- Footer -->
<?php include("../../incl/footer.php");?>