-
Lenguaje
C
-
Descripción
Pide un número romano y lo convierte a notación arábiga
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
const char *romanos[][9] = {
{"M", "MM", "MMM"},
{"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
};
int arabigo = 0, i, j, tamano, multiplo = 1000;
char original[255], *romano;
printf ("Ingrese un n\243mero romano: ");
scanf ("%s", original);
for (i=0; original[i]!='\0'; i++)
original[i] = (char) toupper (original[i]);
romano = original;
for (i=0; i<4; i++)
{
for (j = (i==0? 2: 8); j>=0; j--)
{
tamano = strlen (romanos[i][j]);
if (strncmp (romanos[i][j], romano, tamano) == 0)
{
arabigo += (j + 1) * multiplo;
romano += tamano;
break;
}
}
multiplo /= 10;
}
if (strlen (romano) == 0)
printf ("\n%s = %d\n\n", original, arabigo);
else
printf ("\nEl n\243mero romano '%s' es incorrecto.\n\n", original);
system ("pause");
return EXIT_SUCCESS;
}
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
const char *romanos[][9] = {
{"M", "MM", "MMM"},
{"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
};
int arabigo = 0, i, j, tamano, multiplo = 1000;
char original[255], *romano;
printf ("Ingrese un n\243mero romano: ");
scanf ("%s", original);
for (i=0; original[i]!='\0'; i++)
original[i] = (char) toupper (original[i]);
romano = original;
for (i=0; i<4; i++)
{
for (j = (i==0? 2: 8); j>=0; j--)
{
tamano = strlen (romanos[i][j]);
if (strncmp (romanos[i][j], romano, tamano) == 0)
{
arabigo += (j + 1) * multiplo;
romano += tamano;
break;
}
}
multiplo /= 10;
}
if (strlen (romano) == 0)
printf ("\n%s = %d\n\n", original, arabigo);
else
printf ("\nEl n\243mero romano '%s' es incorrecto.\n\n", original);
system ("pause");
return EXIT_SUCCESS;
}