C: roman numerals

This commit is contained in:
2021-08-16 16:58:27 +02:00
parent e357f5c26a
commit 04f1f5e23b
5 changed files with 266 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
#include "roman_numerals.h"
#include <stdio.h>
#include <string.h>
#include <malloc.h>
/* V1: initial working version
* V2 & V3: code simplification
*/
static struct conv_s {
unsigned char r;
unsigned d;
} conv[] = {
{ 'M', 1000 },
{ 'D', 500 },
{ 'C', 100 },
{ 'L', 50 },
{ 'X', 10 },
{ 'V', 5 },
{ 'I', 1 }
};
/* rules could differ (like IIII/VIIII instead of IV/IX), so it is better not
* to harcode values like 4, 9, 40, 90 etc...
* Example: the Colosseum gate 44 was written XLIIII, and not XLIV.
* we can therefore add a parameter in function below, and add a rule in
* the code to easily return a different notation.
*/
char *to_roman_numeral(unsigned int n /*, int notation */ )
{
unsigned cur=0, mult, pos=0, i;
char res[64]={0}, *to;
/* max number = 3999 = MMMCMXCIX */
if (n >= 4000) /* could be different if we */
return NULL; /* accept the 5000 sign */
while (n) {
/* we could : switch (notation) {
* case ROMAN_RULE:
*/
if ((mult=n/conv[cur].d)) { /* >= current multiplier */
if (mult%5==4) { /* 4 & 9 */
res[pos++]=conv[cur].r;
res[pos++]=conv[cur-mult/4].r; /* 4/4=1, 9/4=2 */
} else { /* 1-3 & 5-8 */
if (mult >= 5)
res[pos++]=conv[cur-1].r;
for (i=mult%5; i>0; --i)
res[pos++]=conv[cur].r;
}
}
n-=mult*conv[cur].d;
cur+=2;
}
if ((to=malloc(pos+1)))
memcpy(to, res, pos+1);
return to;
}
/* See GNUmakefile below for explanation
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#ifdef UNIT_TEST
#include <stdlib.h>
int main(int ac, char **av)
{
int arg=1;
unsigned i;
for (; arg<ac; ++arg) {
i=atoi(av[arg]);
printf("roman(%d)=%s\n", i, to_roman_numeral(i));
}
}
#endif

View File

@@ -0,0 +1,20 @@
#ifndef ROMAN_NUMERALS_H
#define ROMAN_NUMERALS_H
#define ROMAN_RULE 0
char *to_roman_numeral(unsigned int number);
/* See GNUmakefile below for explanation
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
*/
#if defined UNIT_TEST || defined DEBUG
#include <stdio.h>
#include <stdlib.h>
#endif
#ifdef TESTALL
#undef TEST_IGNORE
#define TEST_IGNORE() {}
#endif
#endif