Your Ad Here

Friday, May 21, 2010

Roman Numeral to Decimal Conversion - Algorithm 2

A second method for converting Roman numerals to decimal numbers has been posted on the Computer Science 101 website.

According to the article on that site, the algorithm boils down to this:

  1. Set the character pointer to point at the right-most character.
  2. Set the value of the summing variable to be the value of the current character.
  3. Move the character pointer to the left one step.
  4. Compare the value of the current character to the previous character:
  5. If the current character's value is the same or larger than the previous character, ADD its value to the sum.
  6. Otherwise, if the current character's value is smaller than the previous character, SUBTRACT its value from the sum.
  7. Repeat steps 3-6 until the left-most character has been evaluated.

 
You can get a full listing of a function employing this algorithm written in Visual Basic on the Computer Science 101 website.
 
 

No comments: