 # vlzware's solution

## to Roman Numerals in the C Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

`````` 1  => I
10  => X
7  => VII
``````

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_roman_numerals.c

``````#include "vendor/unity.h"
#include "../src/roman_numerals.h"
#include <stdlib.h>

void setUp(void)
{
}

void tearDown(void)
{
}

static void test_conversion(int number, char *expected)
{
char *result = to_roman_numeral(number);
TEST_ASSERT_EQUAL_STRING(expected, result);
free(result);
}

void test_one_yields_I(void)
{
test_conversion(1, "I");
}

void test_two_yields_II(void)
{
TEST_IGNORE();               // delete this line to run test
test_conversion(2, "II");
}

void test_three_yields_III(void)
{
TEST_IGNORE();
test_conversion(3, "III");
}

void test_four_yields_IV(void)
{
TEST_IGNORE();
test_conversion(4, "IV");
}

void test_five_yields_V(void)
{
TEST_IGNORE();
test_conversion(5, "V");
}

void test_six_yields_VI(void)
{
TEST_IGNORE();
test_conversion(6, "VI");
}

void test_nine_yields_IX(void)
{
TEST_IGNORE();
test_conversion(9, "IX");
}

void test_twenty_seven_yields_XXVII(void)
{
TEST_IGNORE();
test_conversion(27, "XXVII");
}

void test_forty_eight_yields_XLVIII(void)
{
TEST_IGNORE();
test_conversion(48, "XLVIII");
}

void test_forty_nine_yields_XLIX(void)
{
TEST_IGNORE();
test_conversion(49, "XLIX");
}

void test_fifty_nine_yields_LIX(void)
{
TEST_IGNORE();
test_conversion(59, "LIX");
}

void test_ninety_three_yields_XCIII(void)
{
TEST_IGNORE();
test_conversion(93, "XCIII");
}

void test_one_hundred_forty_one_yields_CXLI(void)
{
TEST_IGNORE();
test_conversion(141, "CXLI");
}

void test_one_hundred_sixty_three_yields_CLXIII(void)
{
TEST_IGNORE();
test_conversion(163, "CLXIII");
}

void test_four_hundred_two_yields_CDII(void)
{
TEST_IGNORE();
test_conversion(402, "CDII");
}

void test_five_hundred_seventy_five_yields_DLXXV(void)
{
TEST_IGNORE();
test_conversion(575, "DLXXV");
}

void test_nine_hundred_eleven_yields_CMXI(void)
{
TEST_IGNORE();
test_conversion(911, "CMXI");
}

void test_one_thousand_twenty_four_yields_MXXIV(void)
{
TEST_IGNORE();
test_conversion(1024, "MXXIV");
}

void test_three_thousand_yields_MMM(void)
{
TEST_IGNORE();
test_conversion(3000, "MMM");
}

int main(void)
{
UnityBegin("test/test_roman_numerals.c");

RUN_TEST(test_one_yields_I);
RUN_TEST(test_two_yields_II);
RUN_TEST(test_three_yields_III);
RUN_TEST(test_four_yields_IV);
RUN_TEST(test_five_yields_V);
RUN_TEST(test_nine_yields_IX);
RUN_TEST(test_six_yields_VI);
RUN_TEST(test_twenty_seven_yields_XXVII);
RUN_TEST(test_forty_eight_yields_XLVIII);
RUN_TEST(test_forty_nine_yields_XLIX);
RUN_TEST(test_fifty_nine_yields_LIX);
RUN_TEST(test_ninety_three_yields_XCIII);
RUN_TEST(test_one_hundred_forty_one_yields_CXLI);
RUN_TEST(test_one_hundred_sixty_three_yields_CLXIII);
RUN_TEST(test_four_hundred_two_yields_CDII);
RUN_TEST(test_five_hundred_seventy_five_yields_DLXXV);
RUN_TEST(test_nine_hundred_eleven_yields_CMXI);
RUN_TEST(test_one_thousand_twenty_four_yields_MXXIV);
RUN_TEST(test_three_thousand_yields_MMM);

UnityEnd();
return 0;
}``````

### src/roman_numerals.c

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

#define MAXROMAN 12
#define ELEMENTS 13

const int dec[ELEMENTS] = {
1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
};
const char* rom[ELEMENTS] = {
"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"
};
const int sizes[ELEMENTS] = {
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1
};

char *to_roman_numeral(int n)
{
if (n <= 0)
return NULL;

char *res = (char*) malloc(MAXROMAN + 1);
if (res == NULL)
return NULL;

char *tmp = res;

int i;
for (i = 0; i < ELEMENTS; i++) {
while (n >= dec[i]) {

n-= dec[i];
strcpy(tmp, rom[i]);

tmp += sizes[i];
if (tmp - res > MAXROMAN) {
free(res);
return 0;
}
}
}
*tmp = '\0';

return res;
}``````

### src/roman_numerals.h

``````#ifndef ROMAN_NUMS_H
#define ROMAN_NUMS_H

char *to_roman_numeral(int n);

#endif``````