Code:
/* ************************************************************************ */
/* Object: Convert an roman numeral to arabical, the input can be a number */
/* between 1 to 3999. */
/* Jason Holt | 14/12/07 | v1.0 */
/* ************************************************************************ */
/* Header files */
#include <conio.h>;
#include <ctype.h>;
#include <stdio.h>;
#include <stdlib.h>;
#include <string.h>;
int romantoarabic (void);
int arabictoroman (void);
char *a2roman (int value, char *c1, char *c2, char *c3);
int main(void)
{
int modeno;
printf("Roman Numeral and Arabic Converter: Choose a Mode\n\n");
printf("1. Roman to Arabic\n");
printf("2. Arabic to Roman\n\n");
printf("Enter Mode Number: ");
scanf("%d", &modeno);
switch(modeno)
{
case 1:
romantoarabic();
break;
case 2:
arabictoroman();
break;
default:
printf("\nIncorrect Mode Choice\n\n");
break;
}
}
int romantoarabic (void)
{
int arabic1 = 0; /* Decimal value of roman numeral */
int arabic2 = 0; /* Decimal value of each roman numeral */
int stringpos = 0; /* Roman Letter String position */
int counter = 0; /* 3 incurs counter */
int roman[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* Array that stores the value of each roman letter */
char tempRoman =' '; /* Contain the temporal roman letter */
char input[15] = ""; /* Contain the input string */
printf ("Roman Numeral Converter for values between 1 and 3999\n\n");
printf ("Enter Roman Numeral(s): ");
scanf ("&c", &tempRoman);
/* Read from keyboard until appear an <ENTER> */
while ((tempRoman != '\n') && (stringpos < 15))
{
switch (tempRoman)
{
/* V, L D only can appear one time */
case 'V': case 'L': case 'D':
if ((stringpos > 0) && (input[stringpos - 1] == tempRoman)) /* If appear more of one time, then finish the program */
{
printf ("\nInput values not correct!\n\n");
}
/* If appear one time, save the char in the string that stores the characters of roman numerals */
else
{
input[stringpos++] = tempRoman;
}
break;
case 'I': case 'X': case 'C': case 'M': /* I, X, C can appear until 3 times */
if (counter <= 3) /* If appear correctly, save the char in the string that stores the characters of roman numeral */
{
input[stringpos++] = tempRoman;
}
counter++;
if ((counter > 3) && (input[stringpos - 2] == tempRoman)) /* If appear more of 3 times, then finish the program */
{
printf ("\nInput values not correct!\n\n");
exit(100);
}
if ((stringpos > 1) && ((counter > 3) || (input[stringpos - 2] != tempRoman))) /* If the 3 incurs counter get to be 3 but the preceding char */
{ /*is different of current, then reset the 3 incurs counter */
counter = 1;
}
break;
case '\n': /* Invalidate the \n char as default char */
break;
default: printf ("\nInput values not correct!\n\n"); /* If the input is an invalid char then finish the program */
exit(100);
}
}
/* Reutilization of counter variable as index of iterations for no create
a new variable and then optimize the program */
for (counter = 0; counter <= stringpos; counter++)
{
switch (input[counter])
{
case 'I': roman[counter] = 1; break;
case 'V': roman[counter] = 5; break;
case 'X': roman[counter] = 10; break;
case 'L': roman[counter] = 50; break;
case 'C': roman[counter] = 100; break;
case 'D': roman[counter] = 500; break;
case 'M': roman[counter] = 1000; break;
}
}
/* Do a sum over stored numbers */
for (counter = 0; counter <= stringpos; counter++)
{
if (roman[counter] >= roman[counter + 1]) /* Add decimal value of each roman numeral */
{
arabic2 = roman[counter];
}
/* 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 ((roman[counter] == (roman[counter + 1] / 10)) || (roman[counter] == (roman[counter + 1] / 5)))
{
arabic2 = roman[counter + 1] - roman[counter];
counter++;
}
if (arabic2 < roman[counter + 1]) /* If precedent number is less than current value and not is 10% or */
/* 5% of current value, then finish the program */
{
printf ("\nInput values not correct!\n\n");
}
arabic1+= arabic2;
}
printf ("\nArabic Equivalent is: %d\n\n", arabic1); /* Diplay result in the screen */
printf ("\n\n\t\t\t...Press any key to exit."); /* Press any key to finish the program */
getch();
return 0; /* Return succesfully */
}
char *a2roman (int value, char *c1, char *c2, char *c3);
int arabictoroman (void)
{
int arabicNum = 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
{
/* Print a message to user */
printf ("Enter an integer in the range of 1 to 3999: \n\t");
/* Read the value */
scanf ("%d", &arabicNum);
}
while ((arabicNum < 1) || (arabicNum > 3999));
/* Obtain the value of thousands */
if ((arabicNum <= 3999) && (arabicNum >= 1000))
{
result = arabicNum / 1000;
strcat (roman, a2roman(result, "M", " ", " "));
arabicNum -= (result * 1000);
}
/* Obtain the value of hundreds */
if ((arabicNum < 1000) && (arabicNum >= 100))
{
result = arabicNum / 100;
strcat (roman, a2roman(result, "C", "D", "M"));
arabicNum -= (result * 100);
}
/* Obtain the value of tens */
if ((arabicNum < 100) && (arabicNum >= 10))
{
result = arabicNum / 10;
strcat (roman, a2roman(result, "X", "L", "C"));
arabicNum -= (result * 10);
}
/* Obtain the value of units */
if ((arabicNum < 10) && (arabicNum >= 1))
{
strcat (roman, a2roman(arabicNum, "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) && (value <= 3))
{
for (i = 0; i < value; i++)
strcat (rRoman, c1);
}
/* If value = 5, 6, 7, 8 */
if ((value >= 5) && (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);
}
this is seriously irritating and i dnno how to fix it GRR sorry to ask for more help