This code is provided as shown in below
Now i would like to simplify it and hope someone can share wif me .
Thanks
Instruction
User is required to enter a Roman number to be converted Decimal number. The symbols used in Roman numeral system and their equivalents are given below:
Roman Decimal
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
You have to ask the user to re-enter a Roman number if the user enter any other characters not given in the table above.
Steps to convert:
1. Set the value of the decimal number to zero.
2. Check the string containing the Roman characters from left to right:
- if the next character is a null character (if the current character is the last character), add the value of the current character to the decimal value.
- if the value of the current character is greater than or equal to the value of the next character, add the value of the current character to the decimal value.
- if the value of the current character is less than the next character, subtract the value of the current character from the decimal value
#include <stdio.h>
#include <string.h>
void main(void)
{
const char *Input_String = "Please enter Roman number:";
const char *ReInput_String = "Please re-enter Roman number:";
const char *OutPut_String = "The equivalent Decimal number is:%d\n";
const char *ErrorString = "Invalid input!\n";
const char *OutPutPtr = OutPut_String,*InputPtr = Input_String;
int NumberIndex[MaxNumber],ni=0,DecNumber = 0,RomaLength =0,PreNumber=0,Temp =0;
char RomaString[MaxCharLength];
unsigned char bLoop = 0;
//////////////////////////////////////////////////////////////////////////
memset(&NumberIndex[0],0,sizeof(NumberIndex));
NumberIndex['I'] = 1;
NumberIndex['V'] = 5;
NumberIndex['X'] = 10;
NumberIndex['L'] = 50;
NumberIndex['C'] = 100;
NumberIndex['D'] = 500;
NumberIndex['M'] = 1000;
do
{
bLoop = 0;
OutPutPtr = OutPut_String;
printf(InputPtr);
scanf("%s",RomaString);
RomaLength = strlen(RomaString);
for(ni =0; ni < RomaLength; ni++)
{
Temp = NumberIndex[RomaString[ni]];
if(Temp)
{
if(RomaString[ni+1] ==0)
{
DecNumber+=Temp;
break;
}
else if(Temp <NumberIndex[RomaString[ni+1]])
{
Temp=-Temp;
}
DecNumber+=Temp;
}
else
{
OutPutPtr = ErrorString;
InputPtr = ReInput_String;
bLoop = 1;
break;
}
}
printf(OutPutPtr,DecNumber);
}while(bLoop);
}