Convierte un numero arabigo en romano en el rango de 1 a 3000 (Ingles)
c:
/********************************** A2R.C ***********************************/
/* Created by: Manuel F MartÃÂÂÂnez. manpaz@email.com */
/* Language: C/C++ */
/* Date: Guatemala April 15, 2001 */
/* ************************************************************************ */
/* Target: Convert an Arabical numeral to Roman, the input is any value in */
/* 1 to 3000 range of integers. */
/* Description: The program take the input value and the value of this */
/* position. */
/* e.g.: int ArabicalNumeral = 3 7 5 9 */
/* The number 3 to be in the first position, the number 7 in the second, */
/* the number 5 ... ; to obtain the 3 do: */
/* result = ArabicalNumeral / 1000 */
/* Then result store the value of "thousands". */
/* After, the function a2roman ( int, char, char, char ), receive 1 int */
/* parameter and 3 char, the char parameters indicates the three possible */
/* roman values to this number and, the int indicates the value of this */
/* position, the a2roman analizes the number and return a string with the */
/* equivalent roman number. After , do the same operation for the */
/* "hundreds", only that Result is equal to "Arabical % 100" and etc. */
/****************************************************************************/
/* NOTE: IF YOU USE VISUAL C++, FIRST CREATE A WORKSPACE, SECOND CLEAR THE */
/* LINE "clrscr();" THEN COMPILE THE PROGRAM. */
/****************************************************************************/
/* HEADER FILES */
#include <stdio.h>
#include <conio.h>
#include <string.h>
/* The char "c1" and "c2" are initialized in order that not affect the thousands */
char *a2roman (int value, char *c1, char *c2, char *c3);
int main (void)
{
int arabicalNumeral = 1; /* Initialization for read althought one time */
int result; /* "result" store the position value */
char roman[15] = ""; /* "roman" contain the roman-numeral string */
/* This cicle allow to continue if "arabicalNumber" to be in the range */
do
{
/* Clear the screen */
clrscr();
/* Print a message to user */
printf ("Enter a integer in 1 to 3000 range of integers: \n\t");
/* Read the value */
scanf ("%d", 038;arabicalNumeral);
}
while ((arabicalNumeral < 1) || (arabicalNumeral > 3000));
/* Obtain the value of thousands */
if ((arabicalNumeral <= 3000) 038;038; (arabicalNumeral >= 1000))
{
result = arabicalNumeral / 1000;
strcat (roman, a2roman(result, "M", " ", " "));
arabicalNumeral -= (result * 1000);
}
/* Obtain the value of hundreds */
if ((arabicalNumeral < 1000) 038;038; (arabicalNumeral >= 100))
{
result = arabicalNumeral / 100;
strcat (roman, a2roman(result, "C", "D", "M"));
arabicalNumeral -= (result * 100);
}
/* Obtain the value of tens */
if ((arabicalNumeral < 100) 038;038; (arabicalNumeral >= 10))
{
result = arabicalNumeral / 10;
strcat (roman, a2roman(result, "X", "L", "C"));
arabicalNumeral -= (result * 10);
}
/* Obtain the value of units */
if ((arabicalNumeral < 10) 038;038; (arabicalNumeral >= 1))
{
strcat (roman, a2roman(arabicalNumeral, "I", "V", "X"));
}
/* Display the Roman numeral */
printf ("The Roman numeral is: \n\t%s\n\n", roman);
printf ("\t\t ...Press any key to exit.");
getch();
/* Succesfull return */
return 0;
}
char *a2roman (int value, char *c1, char *c2, char *c3)
{
int i; /* "i" is the index of the iteration */
char rRoman[15] = "";
/* If value = 1, 2, 3 */
if ((value >= 1) 038;038; (value <= 3))
{
for (i = 0; i < value; i++)
strcat (rRoman, c1);
}
/* If value = 5, 6, 7, 8 */
if ((value >= 5) 038;038; (value <= 8))
{
strcat (rRoman, c2);
for (i = 0; i < (value - 5); i++)
strcat (rRoman, c1);
}
/* If value = 4 */
if (value == 4)
{
strcat (rRoman, c1);
strcat (rRoman, c2);
}
/* If value = 9 */
if (value == 9)
{
strcat (rRoman, c1);
strcat (rRoman, c3);
}
return (rRoman);
}
/* Created by: Manuel F MartÃÂÂÂnez. manpaz@email.com */
/* Language: C/C++ */
/* Date: Guatemala April 15, 2001 */
/* ************************************************************************ */
/* Target: Convert an Arabical numeral to Roman, the input is any value in */
/* 1 to 3000 range of integers. */
/* Description: The program take the input value and the value of this */
/* position. */
/* e.g.: int ArabicalNumeral = 3 7 5 9 */
/* The number 3 to be in the first position, the number 7 in the second, */
/* the number 5 ... ; to obtain the 3 do: */
/* result = ArabicalNumeral / 1000 */
/* Then result store the value of "thousands". */
/* After, the function a2roman ( int, char, char, char ), receive 1 int */
/* parameter and 3 char, the char parameters indicates the three possible */
/* roman values to this number and, the int indicates the value of this */
/* position, the a2roman analizes the number and return a string with the */
/* equivalent roman number. After , do the same operation for the */
/* "hundreds", only that Result is equal to "Arabical % 100" and etc. */
/****************************************************************************/
/* NOTE: IF YOU USE VISUAL C++, FIRST CREATE A WORKSPACE, SECOND CLEAR THE */
/* LINE "clrscr();" THEN COMPILE THE PROGRAM. */
/****************************************************************************/
/* HEADER FILES */
#include <stdio.h>
#include <conio.h>
#include <string.h>
/* The char "c1" and "c2" are initialized in order that not affect the thousands */
char *a2roman (int value, char *c1, char *c2, char *c3);
int main (void)
{
int arabicalNumeral = 1; /* Initialization for read althought one time */
int result; /* "result" store the position value */
char roman[15] = ""; /* "roman" contain the roman-numeral string */
/* This cicle allow to continue if "arabicalNumber" to be in the range */
do
{
/* Clear the screen */
clrscr();
/* Print a message to user */
printf ("Enter a integer in 1 to 3000 range of integers: \n\t");
/* Read the value */
scanf ("%d", 038;arabicalNumeral);
}
while ((arabicalNumeral < 1) || (arabicalNumeral > 3000));
/* Obtain the value of thousands */
if ((arabicalNumeral <= 3000) 038;038; (arabicalNumeral >= 1000))
{
result = arabicalNumeral / 1000;
strcat (roman, a2roman(result, "M", " ", " "));
arabicalNumeral -= (result * 1000);
}
/* Obtain the value of hundreds */
if ((arabicalNumeral < 1000) 038;038; (arabicalNumeral >= 100))
{
result = arabicalNumeral / 100;
strcat (roman, a2roman(result, "C", "D", "M"));
arabicalNumeral -= (result * 100);
}
/* Obtain the value of tens */
if ((arabicalNumeral < 100) 038;038; (arabicalNumeral >= 10))
{
result = arabicalNumeral / 10;
strcat (roman, a2roman(result, "X", "L", "C"));
arabicalNumeral -= (result * 10);
}
/* Obtain the value of units */
if ((arabicalNumeral < 10) 038;038; (arabicalNumeral >= 1))
{
strcat (roman, a2roman(arabicalNumeral, "I", "V", "X"));
}
/* Display the Roman numeral */
printf ("The Roman numeral is: \n\t%s\n\n", roman);
printf ("\t\t ...Press any key to exit.");
getch();
/* Succesfull return */
return 0;
}
char *a2roman (int value, char *c1, char *c2, char *c3)
{
int i; /* "i" is the index of the iteration */
char rRoman[15] = "";
/* If value = 1, 2, 3 */
if ((value >= 1) 038;038; (value <= 3))
{
for (i = 0; i < value; i++)
strcat (rRoman, c1);
}
/* If value = 5, 6, 7, 8 */
if ((value >= 5) 038;038; (value <= 8))
{
strcat (rRoman, c2);
for (i = 0; i < (value - 5); i++)
strcat (rRoman, c1);
}
/* If value = 4 */
if (value == 4)
{
strcat (rRoman, c1);
strcat (rRoman, c2);
}
/* If value = 9 */
if (value == 9)
{
strcat (rRoman, c1);
strcat (rRoman, c3);
}
return (rRoman);
}