Convierte un numero romano en arabigo en el rango de I a MMMCMXCIX (Ingles)
c:
/******************************** 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') 038;038; (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) 038;038; (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) 038;038; (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) 038;038; ((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 ("\n\n\t\t\t...Press any key to exit.");
getch();
/* Return succesfully */
return 0;
}
/* 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') 038;038; (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) 038;038; (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) 038;038; (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) 038;038; ((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 ("\n\n\t\t\t...Press any key to exit.");
getch();
/* Return succesfully */
return 0;
}