#include <stdio.h>
#include <stdlib.h>
void ArabicToRoman (unsigned int numero, char *resultado) {
char *centenas[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
char *dezenas[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
char *unidades[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
int size[] = { 0, 1, 2, 3, 2, 1, 2, 3, 4, 2,};
while (numero >= 1000)
{
*resultado++ = 'M';
numero -= 1000;
}
strcpy (resultado
, centenas
[numero
/100]); resultado
+= size
[numero
/100]; numero
= numero
% 100; strcpy (resultado
, dezenas
[numero
/10]); resultado
+= size
[numero
/10]; numero
= numero
% 10; strcpy (resultado
, unidades
[numero
]); resultado
+= size
[numero
]; }
int main()
{
char *romanNumber
= malloc(sizeof(char) * 1024); int numero;
puts("Digite um valor a ser convertido: ");
ArabicToRoman(numero, romanNumber);
printf("O valor %d equivale %s em romano\n", numero
, romanNumber
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnZvaWQgQXJhYmljVG9Sb21hbiAodW5zaWduZWQgaW50IG51bWVybywgY2hhciAqcmVzdWx0YWRvKSB7CiAgICBjaGFyICpjZW50ZW5hc1tdID0geyIiLCAiQyIsICJDQyIsICJDQ0MiLCAiQ0QiLCAiRCIsICJEQyIsICJEQ0MiLCAiRENDQyIsICJDTSJ9OwogICAgY2hhciAqZGV6ZW5hc1tdID0geyIiLCAiWCIsICJYWCIsICJYWFgiLCAiWEwiLCAiTCIsICJMWCIsICJMWFgiLCAiTFhYWCIsICJYQyJ9OwogICAgY2hhciAqdW5pZGFkZXNbXSA9IHsiIiwgIkkiLCAiSUkiLCAiSUlJIiwgIklWIiwgIlYiLCAiVkkiLCAiVklJIiwgIlZJSUkiLCAiSVgifTsKICAgIGludCAgIHNpemVbXSA9IHsgMCwgMSwgMiwgMywgMiwgMSwgMiwgMywgNCwgMix9O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