diff options
author | Joursoir <chat@joursoir.net> | 2021-02-06 13:10:05 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-02-06 13:10:05 +0000 |
commit | b0ff101f2d97cff3ce77af9cc68f55e2bf50e4f5 (patch) | |
tree | 03fd95faf5c0b53b041627f0ab6030d663457b90 | |
parent | 8c798815691cfddd751757d7d4f40053fc0cf6ed (diff) | |
download | snc-b0ff101f2d97cff3ce77af9cc68f55e2bf50e4f5.tar.gz snc-b0ff101f2d97cff3ce77af9cc68f55e2bf50e4f5.tar.bz2 snc-b0ff101f2d97cff3ce77af9cc68f55e2bf50e4f5.zip |
add arabic to roman converter
-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 */ |