answersLogoWhite

0


Best Answer

I use the following function in C++:

unsigned int RomanToArabic(

const char * pRoman,

int len,

const unsigned int uStandard /* = 2 */)

{

// Assume invalid until proven otherwise.

unsigned int arabic = 0;

// Ensure the string parameter is non-null.

if( !pRoman ) return( arabic );

// Ensure the length parameter is positive.

if( len < 1 ) return( arabic );

// Ensure the string parameter is null-terminated

// within len chars.

char * pR = ( char * ) pRoman;

while( *pR && pR - pRoman < len - 1 ) ++pR;

if( *pR ) return( arabic );

// Adjust length (exlude null-terminator).

len = pR - pRoman;

int buffer = len + 1; // include null-terminator in buffer.

// Copy the string.

char * pCopy = ( char * ) malloc( buffer );

memset( pCopy, 0, buffer );

strcpy_s( pCopy, buffer, pRoman );

// Convert the copy to lowercase.

_strlwr( pCopy );

// Ensure the copy contains valid Roman numerals.

if( strspn( pCopy, "ivxlcdm") != len )

{

free( pCopy );

return( arabic );

}

// Universal standard (applies to all Roman numerals).

if(

// Invalid double sequences.

strstr( pCopy, "vv" ) // = x

strstr( pCopy, "ll" ) // = c

strstr( pCopy, "dd" ) // = m

// Sequences of 5 are never permitted.

strstr( pCopy, "iiiii" ) // = v

strstr( pCopy, "xxxxx" ) // = l

strstr( pCopy, "ccccc" ) // = d

strstr( pCopy, "mmmmm")) // no alternative

{

free( pCopy );

return( arabic );

}

// Accepted standard (includes universal standard).

if( uStandard && (

// Sequences of 4 are not permitted.

strstr( pCopy, "iiii" )

strstr( pCopy, "xxxx" )

strstr( pCopy, "cccc" )

strstr( pCopy, "mmmm")

// Invalid subtractions.

strstr( pCopy, "vx" )

strstr( pCopy, "lc" )

strstr( pCopy, "dm" )

// Two small values before a large value are not permitted.

strstr( pCopy, "iiv" )

strstr( pCopy, "iix" )

strstr( pCopy, "iil" )

strstr( pCopy, "iic" )

strstr( pCopy, "iid" )

strstr( pCopy, "iim" )

strstr( pCopy, "xxl" )

strstr( pCopy, "xxc" )

strstr( pCopy, "xxd" )

strstr( pCopy, "xxm" )

strstr( pCopy, "ccd" )

strstr( pCopy, "ccm" )

))

{

free( pCopy );

return( arabic );

}

// Strict standard (includes accepted standard).

if( uStandard > 1 && (

// i subtracts from v and x only.

strstr( pCopy, "il" )

strstr( pCopy, "ic" )

strstr( pCopy, "id" )

strstr( pCopy, "im" )

// v cannot be subtracted.

strstr( pCopy, "vl" )

strstr( pCopy, "vc" )

strstr( pCopy, "vd" )

strstr( pCopy, "vm" )

// x subtracts from l and c only.

strstr( pCopy, "xd" )

strstr( pCopy, "xm" )

// l cannot be subtracted.

strstr( pCopy, "ld" )

strstr( pCopy, "lm" )

))

{

free( pCopy );

return( arabic );

}

// Point to the last numeral.

pR = (( char * ) pCopy );

pR += len;

int value = 0; // The current value.

int previous = 0; // The value to the right.

// Begin with the last Roman numeral.

int index = buffer;

// Iterate the Roman numerals.

while( index )

{

// Evaluate the current digit.

switch( *pR )

{

case('i'): value = 1; break;

case('v'): value = 5; break;

case('x'): value = 10; break;

case('l'): value = 50; break;

case('c'): value = 100; break;

case('d'): value = 500; break;

case('m'): value = 1000; break;

}

// Subtract?

if( value < previous )

{

// Accepted and strict only.

if( uStandard )

value *= (-1);

else

{

// Invalid!!

value = 0;

arabic = 0;

index = 1;

}

}

// Update the arabic numeral.

arabic += value;

// Update the previous value for next iteration.

previous = value;

// Point to the numeral to the left.

if( --index ) --pR;

}

free( pCopy );

return( arabic );

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you make a program that convert roman numerals to hindu arabic numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Math & Arithmetic

How would you add up the four numbers of 49 119 199 and 1409 when converted into Roman numerals?

I would convert them back into Hindu-Arabic numerals, do the addition and then convert the answer to Roman numerals.


What is the Arabic numbers for 1859?

The Arabic numbers for 1859 are 1859, the Roman numerals for 1859 are MDCCCLIX.


Why are Arabic numerals called Arabic numerals when they actually came from India?

Arabic numerals are called that because the European interpretation of those numbers comes from the Arabs. The numerals are called Arabic numerals (even though they do not resemble modern Arabic numerals in the slightest) to distinguish them from Roman numerals.


When was Roman Numerals changed to numbers?

Bear in mind that Roman numerals actually are numbers, they are just not the kind of numbers that we presently use, which are called Arabic numerals. Arabic numerals were first used in Europe in the year 976 AD. Roman numerals still have not entirely fallen out of use, although for most purposes we use Arabic numerals.


What are facts that numbers and roman numerals are different?

A) Arabic numerals are in numbers whereas Roman numerals are in letters.B) Even if Roman numerals are in letters the symbols are easier to understand, despite the fact that Australians and Americans and most probably you write numbers using the system of Arabic numerals.C) The system of Roman numerals was invented before the system of Arabic numerals, but people use the system of Arabic numerals to write more frequently.

Related questions

How would you add up the four numbers of 49 119 199 and 1409 when converted into Roman numerals?

I would convert them back into Hindu-Arabic numerals, do the addition and then convert the answer to Roman numerals.


What is the Arabic numbers for 1859?

The Arabic numbers for 1859 are 1859, the Roman numerals for 1859 are MDCCCLIX.


What is mmdxcvi in Roman Numerals?

The M represents 1000 and the D is 500. This would convert to 2,594 in Arabic numbers.


What is xlix in Arabic numbers?

XLIX in Arabic Numerals is 49 and in Arabic Numbers is &#1636;&#1641; .


What is the Arabic of numbers?

roman numerals


Why are Arabic numerals called Arabic numerals when they actually came from India?

Arabic numerals are called that because the European interpretation of those numbers comes from the Arabs. The numerals are called Arabic numerals (even though they do not resemble modern Arabic numerals in the slightest) to distinguish them from Roman numerals.


When was Roman Numerals changed to numbers?

Bear in mind that Roman numerals actually are numbers, they are just not the kind of numbers that we presently use, which are called Arabic numerals. Arabic numerals were first used in Europe in the year 976 AD. Roman numerals still have not entirely fallen out of use, although for most purposes we use Arabic numerals.


What is the number 56435455654656 in roman numerals?

It is quite easy to convert the above Hindu-Arabic numerals into Roman numerals by means of an abacus calculating device which the Romans would use for such large numbers. Unfortunately this computer wont allow such large numbers to be converted into Roman numerals.


What are facts that numbers and roman numerals are different?

A) Arabic numerals are in numbers whereas Roman numerals are in letters.B) Even if Roman numerals are in letters the symbols are easier to understand, despite the fact that Australians and Americans and most probably you write numbers using the system of Arabic numerals.C) The system of Roman numerals was invented before the system of Arabic numerals, but people use the system of Arabic numerals to write more frequently.


Why do people call numbers Hindu-Arabic numerals?

Because that was the region of the world where Hindu-Arabic numerals originated from.


Could you please show us how to add together 1449 and 550 using Roman numerals?

Convert from Roman numerals to Arabic numerals, add, convert back to Roman numerals.


Where do you find the Hindu Arabic numerals?

In most of the world, the numbers in daily normal use are based on Indo-Arabic numerals.