Need to format character precedence in Strings.
- by Christian
I'm currently writing a Roman Numeral Converter for the fun of it. The problem works up to the aforementioned character precedence.
Since Roman Numerals are not positional, i.e. III does not symbolize 1*whatever base^2 + 1*whatever base^1 + 1*whatever base^0.
That of course makes it hard when somebody types in XIV and I need to make sure that the I is not added in this case, but rather subtracted. I'm not sure how to do this. What would be the best approach to tackle this?
I have both the Roman Symbols and their respective decimal numbers stored in arrays:
const char cRomanArray[] = "IVXLCDM";
const int romanArray[] = { 1, 5, 10, 50, 100, 500, 1000 };
so it wouldn't be too hard for me to brute-force the damn thing by simply checking the precedence within the array, i.e. if the symbol is smaller than the next symbol, i.e. in the exampe 'XIV' if 'I' is smaller than 'V', in which case it would be because I have ordered them in the array, then I could make it subtract the value instead of add.
But this seems like a very ugly solution. Are there perhaps any better ones? I was thinking something along the lines of Regular Expressions maybe( forgive me if that sounds like a horrible idea, I haven't used RegExp yet, but it sounds like it could do what I need, and that's to determine the characters in a string.)