Discuss Convert Symbols to Integers in the C Programming forum on Dev Shed. Convert Symbols to Integers C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 921
Time spent in forums: 1 Week 11 h 50 m 18 sec
Reputation Power: 535
Look at the order of things in the roman numerals. If small things (I, V) are on the left, they get subtracted from bigger things to the right. So IX is 10 - 1 = 9. If small things are on the right of big things, they get added. So XI = 10 + 1 = 11
If things are repeated, they get added repeatedly. So VIII = 5 + 1 + 1 + 1 = 8. If things get repeated, they should not be on the left (i.e. they don't get subtracted).
That's my rough memory of how roman numerals work. Check - I may have some of the details wrong.
__________________
Right 98% of the time, and don't care about the other 3%.
“It has been said that the great scientific disciplines are examples of giants standing on the shoulders of other giants. It has also been said that the software industry is an example of midgets standing on the toes of other midgets.” (Alan Cooper)
Posts: 194
Time spent in forums: 1 Day 9 h 39 m 32 sec
Reputation Power: 70
Walk the Roman numeral, left to right, char by char, adding as you go. If the "now" char is greater than the previous char, then subtract the previous char's value.
Now subtract the previous char from the "now" char (array[i] probably), and then add that amount onto the running sum you have going.
It may help to have a char array, where 'I' is at roman[1], and 'V' is roman[5], and 'X' is the value at roman[10] and like that, up to a thousand.
Sure, it's a bit of wasted memory, but on the other hand, you can include every single char roman numeral that way, in a simple look up.
Posts: 64
Time spent in forums: 1 Day 13 h 7 m 34 sec
Reputation Power: 93
Ok. Use a loop to iterate over all the letters forming your Roman numeral, starting at the END of the numeral.
Inside the for-loop, you have to convert the current letter into a number. Keep track of two things: The decimal value of the previous letter you converted, and the total sum (which will become your final output).
Depending on which number is bigger (the decimal value of the current letter, or the value of the last letter converted), you just add or subtract the decimal value of the current letter to/from your sum.
That's it.
Note: Don't forget to initialize your 'previous value'- variable (with a suitable constant), and to intitialize your 'sum'- variable with 0.
Posts: 194
Time spent in forums: 1 Day 9 h 39 m 32 sec
Reputation Power: 70
Quote:
Originally Posted by xaykogeki
so....
char x[10]'s value is really 10?
but, what if I want the user to input a whole string of those letters.
MXIII
how can I read each and every line of those and at the same time, compare?
Code:
if(roman[i] == yourChar)
romanSum += i;
You want the user to input all the roman numerals char's, all at once. Then you "walk" the resulting char string, char by char, with a for or while loop:
"Here char, here little char, you can't hide little char"