diff options
-rw-r--r-- | snc.c | 25 | ||||
-rw-r--r-- | snc.h | 4 |
2 files changed, 28 insertions, 1 deletions
@@ -18,6 +18,8 @@ ***/ #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include "snc.h" @@ -80,6 +82,29 @@ int romanToArabic(char *str) return answer; } +char *arabicToRoman(char *str) +{ + int number = atoi(str); + if(!number || number > SNC_ROMAN_MAX_NUMBER) + return NULL; + + const char *units[] = {"","I","II","III","IV", + "V","VI","VII","VIII","IV"}; + const char *tens[] = {"","X","XX","XXX","XL", + "L","LX","LXX","LXXX","XC"}; + const char *hundreds[] = {"","C","CC","CCC","CD", + "D","DC","DCC","DCCC","CM"}; + const char *thousands[] = {"","M","MM","MMM"}; + char *answer = malloc(sizeof(char) * (SNC_ROMAN_MAXLEN + 1)); + + strcpy(answer, thousands[number / 1000]); + strcat(answer, hundreds[number / 100 % 10]); + strcat(answer, tens[number / 10 % 10]); + strcat(answer, units[number % 10]); + + return answer; +} + int main(int argc, char *argv[]) { if(argc < 2) @@ -27,7 +27,9 @@ enum { SNC_ROMAN_L = 50, SNC_ROMAN_C = 100, SNC_ROMAN_D = 500, - SNC_ROMAN_M = 1000 + SNC_ROMAN_M = 1000, + SNC_ROMAN_MAX_NUMBER = 3999, + SNC_ROMAN_MAXLEN = 15 // MMMDCCCLXXXVIII }; #endif /* SNC_NUMBERS_H */ |