I'm a complete novice at c programming. I was wondering what would be the best way to read in roman numerals and convert them into decimal values. I tried the switch statement but it told me that the case label does not reduce to an integer constant when the case was more than two characters.
stdin to stdout
Collapse
X
-
Do them one character at a time.
Code:// Suppose the string that holds MC...VI is named RN. while RN is not empty { switch the first character of the string RN { convert it to it's integer form (case 'M': value = 500; break; // etc.) } if the previous character is less than current character { subtract previous character instead of add (total -= (2*prevVal);) } add the value to the total (total += value;) assign value to prevVal; remove the first character from string RN } -
After reviewing the rules, yeah, take as big of a chunk as you can. The rules aren't exactly clear, but there should be some scheme for uniqueness.
Rules I would use:
- Use as few numerals as possible
- Only subtract (put a smaller number before a larger one) if it's less than some percentage of the bigger number's value. Some say 10% is the cutoff. That is exactly two roman numerals before the current one. That would mean 40 = XXXX instead of XL, and 400 = CCCC instead of CD.
- For numbers considerably larger than M (1000), notations can be used to multiply each number by 1000, or even 1,000,000. That exists for all numbers except I because I * 1000 = M.
For example, 947= (1000 - 100) + (50 - 5) + (2) = CMVLIIComment
Comment