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.8.31 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.30 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.29 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.28 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.27 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.26 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.24 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.23 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.22 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.21 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.20 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.19 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.18 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.17 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.16 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.15 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.14 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.13 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.12 tracks/c/exercises/roman-numerals/src/example.c
trackler-2.0.8.11 tracks/c/exercises/roman-numerals/src/example.c