Thread: roman to arabic and vise versa

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    10

    roman to arabic and vise versa

    Hi I have a project to complete where i have to convert roman numerals to arabic values between 1 and 4999 and vise versa.

    I have coded most of the program and will post it below but i have done it in a rush and its pretty untidy and not working fully. i'm tired and was hoping someone could help me getting it to work properly.

    Code:



    Code:
    #include <conio.h>;
    #include <ctype.h>;
    #include <stdio.h>;
    #include <stdlib.h>;
    
    int main (void) 
    
    {
    	int arabic1 = 0;							
    	int arabic2 = 0;						   
    	int stringpos = 0;    						
    	int counter = 0;							
    	int roman[15] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};	
    	char tempRoman = ' ';						
    	char input[15] = "";						
    
    
    	printf ("Roman Numeral Converter for values between 1 and 4999\n\n");
    	printf ("Enter Roman Numeral(s): ");
    
    	
    	while ((tempRoman != '\n') && (stringpos < 15)) 
    	{
    		tempRoman = toupper(getchar());
    
    		switch (tempRoman) 
    		{
    			/* V, L D only can appear one time */
    			case 'V': case 'L': case 'D':
    				
    				if ((stringpos > 0) && (input[stringpos - 1] == tempRoman))	
    				{
    						printf ("\nInput values not correct!\n\n");
    						exit(100);
    				}
    				else 
    				{ 
    					input[stringpos++] = tempRoman; 
    				}
    				break;
    
    			case 'M':
    				if (counter <= 4) 
    				{
    					input[stringpos++] = tempRoman;
    				}
    					counter++;
    				if ((counter > 4) && (input[stringpos - 2] == tempRoman)) 
    				{
    					printf ("\nThis program only converts Roman Numerals between 1 and 4999!\n\n");
    					getch();
    				}
    				if ((stringpos > 1) && ((counter > 4) || (input[stringpos - 2] != tempRoman))) 
    				{
    					counter = 1;
    				}
    				break;
    			
    			case 'I': case 'X': case 'C':	/* I, X, C can appear 3 times */
    				
    				if (counter <= 3)			
    				{					
    					input[stringpos++] = tempRoman;
    				}
    					counter++;
    
    				if ((counter > 3) && (input[stringpos - 2] == tempRoman)) 
    				{
    					printf ("\nInput values not correct!\n\n");
    					exit(100);
    				}
    									
    				if ((stringpos > 1) && ((counter > 3) || (input[stringpos - 2] != tempRoman))) 
    				{																		   
    					counter = 1;
    				}
    				
    				break;
    			
    			case '\n':	
    				
    				break;
    			
    			default: 	printf ("\nInput values not correct!\n\n");		
    						exit(100);							
    	    }   
         }
    
    	
    	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;
    		}
    	}
    
    
    	for (counter = 0; counter <= stringpos; counter++) 
    	{
    		
    		if (roman[counter] >= roman[counter + 1])	
    		{
    			arabic2 = roman[counter];
    		}
    
    		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])	
    
    		{
    			 printf ("\nInput values not correct!\n\n"); 
    		}
    			arabic1+= arabic2;
    	}
    
    	
    	printf ("\nArabic Equivalent is: %d\n\n", arabic1);		
    	
    	printf ("\n\n\t\t\t...Press any key to exit.");		
    	getch();
    	
    	return 0;	
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    This is for the arabic to to roman part, its not doing the thousands properly. its correctly finding the amount of 100s and 10s and 1's but its only working up to 3000 not 4000 not sure where ive gone round.

    Code:
    char *a2roman (int value, char *c1, char *c2, char *c3);
    
    int main (void)
    {
    	int arabicNum = 1; 
    	int result;              
    	char roman[15] = "";
    
    	
    	do
    	{
    		printf ("Enter a integer in 1 to 4999 range of integers: \n\t");
    
    		
    		scanf ("%d", &arabicNum);
    	}
    	while ((arabicNum < 1) || (arabicNum > 4999));
    
    	if ((arabicNum <= 4999) && (arabicNum >= 1000))
    	{
    		result = arabicNum / 1000;
    		strcat (roman, a2roman(result, "M", " ", " "));
    		arabicNum -= (result * 1000);
    	}
    
    	if ((arabicNum < 1000) && (arabicNum >= 100))
    	{
    		result = arabicNum / 100;
    		strcat (roman, a2roman(result, "C", "D", "M"));
    		arabicNum -= (result * 100);
    	}
    
    	if ((arabicNum < 100) && (arabicNum >= 10))
    	{
    		result = arabicNum / 10;
    		strcat (roman, a2roman(result, "X", "L", "C"));
    		arabicNum -= (result * 10);
    	}
    
    	if ((arabicNum < 10) && (arabicNum >= 1))
    	{
    		strcat (roman, a2roman(arabicNum, "I", "V", "X"));
    	}
    
    	printf ("The Roman numeral is: \n\t%s\n\n", roman);
    	printf ("\t\t      ...Press any key to exit.");
    	getch();
    
    
    	return 0;
    }
    
    char *a2roman (int value, char *c1, char *c2, char *c3)
    {
    	int i;	
    	char rRoman[15] = "";
    
    
    	if ((value >= 1) && (value <= 3))
    	{
    		for (i = 0; i < value; i++)
    			strcat (rRoman, c1);
    	}
    
    	if ((value >= 5) && (value <= 8))
    	{
    		strcat (rRoman, c2);
    
    		for (i = 0; i < (value - 5); i++)
    			strcat (rRoman, c1);
    	}
    
    	if (value == 4)
    	{
    		strcat (rRoman, c1);
    		strcat (rRoman, c2);
    	}
    
    	/* If value = 9 */
    	if (value == 9)
    	{
    		strcat (rRoman, c1);
    		strcat (rRoman, c3);
    	}
    
    	return (rRoman);
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	return (rRoman);
    This returns a local variable, that is about to disappear. That's like sitting on the branch when cutting it down from the tree - outside the cut!

    You need to pass in a string to be filled in, rather than return it.

    Code:
    	if ((arabicNum <= 4999) && (arabicNum >= 1000))
    	{
    		result = arabicNum / 1000;
    		strcat (roman, a2roman(result, "M", " ", " "));
    		arabicNum -= (result * 1000);
    	}
    Code:
    	if (value == 4)
    	{
    		strcat (rRoman, c1);
    		strcat (rRoman, c2);
    	}
    That does the wrong thing, doesn't it?


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed