Sha256: 8d5a9a5b0e405979687fc3407e3a7a9a6cf7f56c1dba74581dbed77e9616bc39

Contents?: true

Size: 897 Bytes

Versions: 115

Compression:

Stored size: 897 Bytes

Contents

#include "roman_numerals.h"
#include <stdlib.h>
#include <string.h>

#define NUM_OF_ELEMENTS(a)    (sizeof(a) / sizeof(a[0]))
#define MAX_NUMERAL_LENGTH    (7)

typedef struct {
   char *numeral;
   unsigned int value;
} numeral_values_t;

const numeral_values_t numeral_values[] = {
   {"M", 1000},
   {"CM", 900},
   {"D", 500},
   {"CD", 400},
   {"C", 100},
   {"XC", 90},
   {"L", 50},
   {"XL", 40},
   {"X", 10},
   {"IX", 9},
   {"V", 5},
   {"IV", 4},
   {"I", 1}
};

char *to_roman_numeral(unsigned int number)
{
   char *numerals = calloc(0, sizeof(char) * MAX_NUMERAL_LENGTH);

   for (size_t i = 0; i < NUM_OF_ELEMENTS(numeral_values); i++) {
      while (number >= numeral_values[i].value) {
         strncat(numerals, numeral_values[i].numeral,
                 MAX_NUMERAL_LENGTH - strlen(numerals));
         number -= numeral_values[i].value;
      }
   }

   return numerals;
}

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
trackler-2.0.5.14 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.13 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.12 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.11 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.10 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.9 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.8 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.7 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.6 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.5 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.4 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.3 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.2 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.1 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.5.0 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.4.0 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.3.9 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.3.8 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.3.7 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.3.6 tracks/c/exercises/roman-numerals/src/example.c