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:
- Set the character pointer to point at the right-most character.
- Set the value of the summing variable to be the value of the current character.
- Move the character pointer to the left one step.
- Compare the value of the current character to the previous character:
- If the current character's value is the same or larger than the previous character, ADD its value to the sum.
- Otherwise, if the current character's value is smaller than the previous character, SUBTRACT its value from the sum.
- 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:
Post a Comment