Thread: Converting Roman numerals View Single Post

April 22nd, 2016, 10:37 AM
Rod Stephens
Wrox Author
 Points: 3,166, Level: 23
 Activity: 0%

Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

Hi Maximilian,

I'm glad you're enjoying the book. Tell your friends and coworkers!

You're exactly right about getting practice. That's the best way to learn to program, and I wouldn't wait until I'd finished the book. It;s best to experiment with things as you learn them.

This sounds like a good project to work on. I think you can just loop through the letters from right-to-left (with a for loop). Start with a total variable initialized to 0. then when you reach a letter, there are two cases:
1. If the new letter's value is greater than the value of the letter before it (to the right), then it's a case like IV. In that case subtract the letter's value from a total.
2. If the new letter's value is not greater than the value of the previous letter, then it's a case like III. In that case add the letter's value to the total.
For example, with XIV you would add the values like this: +5, -1, +10 to get the final value 14.

Let me know if you get stuck.

Rod
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 The Following User Says Thank You to Rod Stephens For This Useful Post: Maximilian (April 25th, 2016)