Convierte un numero romano en arabigo en el rango de I a MMMCMXCIX (Ingles)
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | /******************************** R2A_ENG.C *********************************/ /* Created by: Manuel F Mart¡nez. manpaz@email.com */ /* Lenguage: C/C++ */ /* Date: Guatemala May 19, 2002 */ /* ************************************************************************ *//* Object: Convert an roman numeral to arabical, the input can be a number */ /* between 1 to 3999. */ /* Description: The program take each letter and store it in one letter */ /* array, after convert each letter and store it in the same */ /* position of one numbers array. *//* */ /* In this array be analized if is a number of two letters or */ /* one letter; if is of two letters do a difference if the */ /* pair is correctly write, and this value is amounted to the */ /* integer variable that store the result, and if the number *//* is of one letter only amount the value of this roman */ /* letter. */ /****************************************************************************/ /* NOTE: IF YOU USE VISUAL C++, FIRST CREATE A WORKSPACE, SECOND CLEAR THE */ /* LINE "clrscr();" THEN COMPILE THE PROGRAM. *//****************************************************************************/ /* Header files */ #include <conio.h>; #include <ctype.h>;#include <stdio.h>; #include <dos.h>; int main (void) { int numR = 0; /* Decimal value of roman numeral */ int decR = 0; /* Decimal value of each roman numeral */ int cont = 0; /* String position */ int cont3R = 0; /* 3 incurs counter */ int numsR[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* Array that store the value of each roman letter */ char letterR = ' '; /* Contain the temporal roman letter */ char roman[15] = ""; /* Contain the input string */ /* Clear the screen and display the input message */ clrscr(); printf ("Enter a roman numeral between range I to MMMCMXCIX:n"); /* Read from keyboard until appear an <ENTER> */ while ((letterR != 'n') && (cont < 15)) { letterR = toupper(getchar()); switch (letterR) { /* V, L y D only can appear one time */ case 'V': case 'L': case 'D': /* If appear more of one time, then finish the program */ if ((cont > 0) && (roman[cont - 1] == letterR)) { printf ("nData incorrect"); delay (1000); exit (0); } /* If appear one time, be save the char in the string that store the characters of roman numeral */ else { roman[cont++] = letterR; } break; /* I, X, C y M can appear until 3 times */ case 'I': case 'X': case 'C': case 'M': /* If appear correctly, be save the char in the string that that store the characters of roman numeral */ if (cont3R <= 3) { roman[cont++] = letterR; } cont3R++; /* If appear more of 3 times, then finish the program */ if ((cont3R > 3) && (roman[cont - 2] == letterR)) { printf ("nData incorrect"); delay (1000); exit (0); } /* If the 3 incurs counter get to be 3 but the preceding char is different of current, then reset the 3 incurs counter */ if ((cont > 1) && ((cont3R > 3) || (roman[cont - 2] != letterR))) { cont3R = 1; } break; /* Invalidate the n char as default char */ case 'n': break; /* If the input is a char that not concern to roman numerals, then finish the program */ default: printf ("nData incorrect"); delay (1000); exit (0); } } /* Reutilization of cont3R var as index of iterations for no create a new var and then optimize the program */ /* Convert to decimal each letter of roman numeral that was input */ for (cont3R = 0; cont3R <= cont; cont3R++) { switch (roman[cont3R]) { case 'I': numsR[cont3R] = 1; break; case 'V': numsR[cont3R] = 5; break; case 'X': numsR[cont3R] = 10; break; case 'L': numsR[cont3R] = 50; break; case 'C': numsR[cont3R] = 100; break; case 'D': numsR[cont3R] = 500; break; case 'M': numsR[cont3R] = 1000; break; } } /* Do a sum over stored numbers */ for (cont3R = 0; cont3R <= cont; cont3R++) { /* */ /* Add decimal value of each roman numeral */ if (numsR[cont3R] >= numsR[cont3R + 1]) { decR = numsR[cont3R]; } /* If precedent number is less than current value and this is 10% or 5% of current value, then do a difference between two roman numerals to obtain the final value i.e. IX = 10 - 1 = 9 */ if ((numsR[cont3R] == (numsR[cont3R + 1] / 10)) || (numsR[cont3R] == (numsR[cont3R + 1] / 5))) { decR = numsR[cont3R + 1] - numsR[cont3R]; cont3R++; } /* If precedent numbre is less than current value and not is 10% or 5% of current value, then finish the program */ if (decR < numsR[cont3R + 1]) { printf ("nData incorrect"); delay (1000); exit (0); } numR += decR; } /* Diplay result in the screen */ printf ("The value is %d", numR); /* Press any key to finish the program */ printf ("nnttt...Press any key to exit."); getch(); /* Return succesfully */ return 0; } |
¿Has encontrado algún error? ¿Tienes la solución? Dejame tu correción ;-)
Antes de comentar: Gran parte de los ejercicios propuestos no tienen librerías debido a que Wordpress las eliminó al verlas como etiquetas HTML. Si sabes/tienes/conoces las librerías que hacen falta, déjalo en los comentarios. Y lo mas importante: Todos los ejemplos fueron realizados por personas con únicamente conocimiento básico del lenguaje, no de programación.
Otro punto importante: Si vas a sugerir un segmento de código en algún lenguaje debes hacerlo así:
- Si es lenguaje C <code lang="c">Código en C</code>
- Si es lenguaje Pascal <code lang="pascal">Aquí dentro el código de Pascal</code>.
De esta manera el código coloreas el código.
Otro punto importante para muchos que crees que te he ignorado: Todos los comentarios los reviso y en su debido momento los apruebo, pero ojo con el con lo siguiente:Me reservo el derecho de alterar, publicar o no los comentarios así como también cambiar mis condiciones en el momento que yo lo requiera.
¿Si estas de acuerdo? Adelante! que ya te he quitado bastante tiempo leyendo esta basura :)