Algorithm in C to convert Arabic number to Roman number

Asked

Viewed 9,026 times

8

How to make this conversion?

It doesn’t need to be the algorithm ready, wanted a help at least in logic.

  • 2

    What is an Arabic number? And a Roman number? What have you done? Try to improve your question a bit...

  • 3

    Arabic numbers are the numbers we know (0,1,2,3,....). And yes I wanted to know how to convert into Roman. I didn’t do anything, I just thought, but I couldn’t come up with any solution

  • http://social.msdn.microsoft.com/Forums/vstudio/pt-BR/18484a71-f6bb-457a-b3fc-cf55fb7e1b3b/transform-integers ...

2 answers

11


#include <stdio.h>

int main () {
    for (int i = 1; i < 3000; i++) {
        int numero = i;
        char *romanos[] = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
        int arabicos[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
        // acha a quantidade de elementos no array
        int indice = (sizeof(arabicos) / sizeof(arabicos[0])) - 1;
        while (numero > 0) {
            if (numero >= arabicos[indice]) {
                printf("%s", romanos[indice]);
                numero -= arabicos[indice];
            } else {
                indice--;
            }
        }
        printf("\n");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

To make the conversion of numbers of 5000 (there are controversies about the 4000 already require the "upper dash") upwards complicates because it needs to use 2 lines or Unicode characters. Adapt to the way you think best and meet your need.

  • 2

    You have 11 votes in favor, the acceptance of the AP, you have a Demo working. I wanted to understand what the problem is found in the answer to have 2 negative.

4

#include <stdio.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: ");
    scanf("%d", &numero);

    ArabicToRoman(numero, romanNumber);
    printf("O valor %d equivale %s em romano\n", numero, romanNumber);

    return 0;
}

For example, take a case where the number to be converted to roman numeration is 635. Good, numero / 100 will result in 6, in this case, the search set is centenas, 6 amounts to DC.

Next numero = numero % 100 will result in 35, then the research set will be dezenas. numero / 10 will result in 3 that will result XXX, then in numero = numero % 10 will result 5, where the research set is unidades that will give the result V.

So the end result is DCXXXV.

Example taken from daqui.

  • 6

    You have noticed that you are playing the string on the memory dirt because of the non-allocation of foo?

  • Very good answer, thank you, helped me a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.