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);
}

Recommended Answers

All 13 Replies

Hi, fruitkiwi!

There are many inaccuracies in your script. In the Roman's numerical system, the symbol I, X, C, M are repeated three times or at most four times, while the symbol V, L, D aren't repeated.

You can find IV or IIII (4), but IIIII doesn't exist.

LXX is equal to 70, but XXXXXXX doesn't exist.

_ __
For large numbers: 5000 (V), 6000 (VI), you will find V or VI with a bar etc.. not MMMMM or MMMMMM

I am trying to develop this alghoritm (for the bar I'll use the hyphen). When I will have finish, I'll tell you.

PS: Sorry for the language... I don't speak English very well :)

>>void main(void)

Read This
lines 11 and 12: where did you defind MaxNumber and MaxCharLength?

http://www.gidnetwork.com/b-66.html >>void main(void)
Read this.

lines 11 and 12: where did you defind MaxNumber and MaxCharLength?

thanks for ur help.
u are right.i should not put void main(void).
nw i attach the .c file again.kindly give me a hand

#include <stdio.h>
#include <string.h>
#define MaxCharLength 1024
#define MaxNumber 128
void main(void)
{
	char StartString[MaxCharLength],inversestring[MaxCharLength];
	char NumberIndex[MaxNumber];
	char InputString[MaxCharLength],Input =0;
	 int i=0,CheckCount=0,strLength=0;
	char *Input_String = "Please enter a sentence:";
	char *OutPut_String = "Your sentence is a palindrome.\n";
	char *ErrorString = "Your sentence is not a palindrome.\n";
	char *OutPutPtr = OutPut_String;
	//////////////////////////////////////////////////////////////////////////
	memset(&NumberIndex[0],0,sizeof(NumberIndex));
	memset(&NumberIndex['A'],1,'Z'-'A');
	printf(Input_String);
	getchar();
	gets_s(&InputString[0],MaxCharLength);
	
	while(1)
	{
		if(InputString[i] ==0)
		{
			break;
		}
		if(NumberIndex[InputString[i]])
		{
			inversestring[CheckCount] = InputString[i];
			CheckCount++;		
		}
		i++;
	}
	strLength = CheckCount;
	inversestring[CheckCount] = '\0';
	StartString[CheckCount] = '\0';
	for(i =0; i < strLength; i++)
	{
		CheckCount--;		
		StartString[CheckCount] = inversestring[i];
	}
	OutPutPtr = memcmp(StartString,inversestring,strLength)?ErrorString:OutPut_String;
	printf(OutPutPtr);
}

first function depends on second. caller is responsible for error-checking input into "convert( )"

example : value = convert("mcmlxix");
returns: value = 1969

int convert(char *roman)
{
    int arabic = 0, tmp;
    char *str_p  = roman;
    
    while(*str_p)
    {
        tmp = letterValue(*str_p);
        if (letterValue(*++str_p) > tmp)
            arabic -= tmp;
        else
            arabic += tmp;
    }
    return arabic;
}
    
int letterValue(char letter)
{
    if (toupper(letter) == 'I') return 1;
    if (toupper(letter) == 'V') return 5;
    if (toupper(letter) == 'X') return 10;
    if (toupper(letter) == 'L') return 50;
    if (toupper(letter) == 'C') return 100;
    if (toupper(letter) == 'D') return 500;
    if (toupper(letter) == 'M') return 1000;
    return 0;
}

thanks jephtlah.i try to run ur code but it seems like got some problem.kindly help me again.

#include <stdio.h>
#include <string.h>
#include<ctype.h>
int letterValue(char letter);

int main(char *roman)
{
    int arabic = 0, tmp;
    char *str_p  = roman;
    
    while(*str_p)
    {
        tmp = letterValue(*str_p);
        if (letterValue(*++str_p) > tmp)
            arabic -= tmp;
        else
            arabic += tmp;
    }
    return arabic;
}
    
int letterValue(char letter)
{
    if (toupper(letter) == 'I') return 1;
    if (toupper(letter) == 'V') return 5;
    if (toupper(letter) == 'X') return 10;
    if (toupper(letter) == 'L') return 50;
    if (toupper(letter) == 'C') return 100;
    if (toupper(letter) == 'D') return 500;
    if (toupper(letter) == 'M') return 1000;
    
}

when i compile this code by click F7, dun have problem.
but once i click F5, it cannot run normally .and a box come out say that my exe file stop working.can u help me to check it ?

this one is wrong code .dun look at it .as i attach wrong file already .
sorry

#include <stdio.h>
#include <string.h>
#define MaxCharLength 1024
#define MaxNumber 128
void main(void)
{
	char StartString[MaxCharLength],inversestring[MaxCharLength];
	char NumberIndex[MaxNumber];
	char InputString[MaxCharLength],Input =0;
	 int i=0,CheckCount=0,strLength=0;
	char *Input_String = "Please enter a sentence:";
	char *OutPut_String = "Your sentence is a palindrome.\n";
	char *ErrorString = "Your sentence is not a palindrome.\n";
	char *OutPutPtr = OutPut_String;
	//////////////////////////////////////////////////////////////////////////
	memset(&NumberIndex[0],0,sizeof(NumberIndex));
	memset(&NumberIndex['A'],1,'Z'-'A');
	printf(Input_String);
	getchar();
	gets_s(&InputString[0],MaxCharLength);
	
	while(1)
	{
		if(InputString[i] ==0)
		{
			break;
		}
		if(NumberIndex[InputString[i]])
		{
			inversestring[CheckCount] = InputString[i];
			CheckCount++;		
		}
		i++;
	}
	strLength = CheckCount;
	inversestring[CheckCount] = '\0';
	StartString[CheckCount] = '\0';
	for(i =0; i < strLength; i++)
	{
		CheckCount--;		
		StartString[CheckCount] = inversestring[i];
	}
	OutPutPtr = memcmp(StartString,inversestring,strLength)?ErrorString:OutPut_String;
	printf(OutPutPtr);
}

this one is wrong code .dun look at it .as i attach wrong file already .
sorry

you cant change my "convert" function to main.

convert must be called BY main.

in main, you must get the string input from the user, do you error checking, and then display results.

thanks jephtlah.i try to run ur code but it seems like got some problem.kindly help me again.

#include <stdio.h>
#include <string.h>
#include<ctype.h>
int letterValue(char letter);

int main(char *roman)
{
    int arabic = 0, tmp;
    char *str_p  = roman;
    
    while(*str_p)
    {
        tmp = letterValue(*str_p);
        if (letterValue(*++str_p) > tmp)
            arabic -= tmp;
        else
            arabic += tmp;
    }
    return arabic;
}
    
int letterValue(char letter)
{
    if (toupper(letter) == 'I') return 1;
    if (toupper(letter) == 'V') return 5;
    if (toupper(letter) == 'X') return 10;
    if (toupper(letter) == 'L') return 50;
    if (toupper(letter) == 'C') return 100;
    if (toupper(letter) == 'D') return 500;
    if (toupper(letter) == 'M') return 1000;
    
}

when i compile this code by click F7, dun have problem.
but once i click F5, it cannot run normally .and a box come out say that my exe file stop working.can u help me to check it ?

Heavens! Are you understanding the basics?
main can accept an int as the first parameter, and array of pointers as the second. But not a string pointer. Those are supposed to be functions outside main.

yeah, dude... you can't just take a function like: int convert (char *) and turn it into your main routine

"convert( )" is a function that must be called INSIDE main, like this:

int main()
{
   blah   // setup
   blah
   blah
   blah

   scanf("%s",romanString);

   blah  // error-checking
   blah 
   blah 
   blah

   arabicValue = convert(romanString);

   printf("\nRoman  : %s\n",romanString);
   printf("\nArabic : %d\n",arabicValue);

   return 0;
}

yeah, dude... you can't just take a function like: int convert (char *) and turn it into your main routine

"convert( )" is a function that must be called INSIDE main, like this:

int main()
{
   blah   // setup
   blah
   blah
   blah

   scanf("%s",romanString);

   blah  // error-checking
   blah 
   blah 
   blah

   arabicValue = convert(romanString);

   printf("\nRoman  : %s\n",romanString);
   printf("\nArabic : %d\n",arabicValue);

   return 0;
}

Thanks for jephtah and Aia guide.You are correct.I really nid to study the basic again since i am a freshman for C program,
Now i rewrite the code again.It alreay can run,But nw i nid to let it can do the error checking.I use a while loop to do it .but all i get is Invalid output .and the system keep ask user to re-enter a Roman number.can u guide me again?

#include <stdio.h>
#include <string.h>
#include<ctype.h>
int letterValue(char letter);
int convert(char*roman);
int main()
{
	int arabicValue,j=0;
	char romanString[50];
	printf("Please enter a Roman number:");
    
	
    do 
	{   gets(romanString);
        printf("Invalid input\n");
	    printf("Please re-enter Roman number:");
        }while(romanString[j]!="I,V,X,L,C,D,M");
        arabicValue=convert(romanString);
   
   
   printf("The equivalent Decimal number is %d\n",arabicValue);

   return 0;
}
int convert(char *roman)
	{
	int arabic = 0, tmp;
    char *str_p  = roman;
    
    while(*str_p)
    {
        tmp = letterValue(*str_p);
        if (letterValue(*++str_p) > tmp)
            arabic -= tmp;
        else
            arabic += tmp;
    }
    return arabic;
}
    
int letterValue(char letter)
{
    if (toupper(letter) == 'I') return 1;
    if (toupper(letter) == 'V') return 5;
    if (toupper(letter) == 'X') return 10;
    if (toupper(letter) == 'L') return 50;
    if (toupper(letter) == 'C') return 100;
    if (toupper(letter) == 'D') return 500;
    if (toupper(letter) == 'M') return 1000;
    return 0;
}

this is a complete mess. study this and see why it can not work, then fix it.

do 
{   
   gets(romanString);
   printf("Invalid input\n");
   printf("Please re-enter Roman number:");     

} while(romanString[j]!="I,V,X,L,C,D,M");
        
arabicValue=convert(romanString);

this is a complete mess. study this and see why it can not work, then fix it.

do 
{   
   gets(romanString);
   printf("Invalid input\n");
   printf("Please re-enter Roman number:");     

} while(romanString[j]!="I,V,X,L,C,D,M");
        
arabicValue=convert(romanString);

yup,u are correct.
i figure out it at about 2am .
thanks for ur guide .
this problem i think can close nw.
thanks for sharing ur idea and knowlege of u
i appreciate it .
thank you very much .

yes. you're quite welcome.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.