It is necessary to format the priority of characters in the lines

I am currently writing a Roman Numeral envelope for fun. The problem works with the aforementioned character precedence.

Since Roman numerals are not positional, i.e. III does not symbolize 1 * any base ^ 2 + 1 * any base ^ 1 + 1 * regardless of the base ^ 0.

This, of course, complicates when someone types in XIV, and I need to make sure that I am not added in this case, but rather subtracted. I am not sure how to do this. What would be the best approach to solving this problem?

I have both roman characters and their corresponding 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 difficult for me to overdo this damn thing by simply checking the priority in the array, i.e. if the character is less than the next character, i.e. in example XIV, if 'I' is less than 'V', in which case it would be because I ordered them in an array, then I could make it subtract the value instead of adding.

But that seems like a very ugly solution. Are there perhaps the best? I was thinking of something like regular expressions (forgive me if this sounds like a terrible idea, I haven't used RegExp yet, but it looks like it can do what I need and what to determine the characters in the string.)

+3
source share
2 answers

. , , ( ), , .

. XLIV

V, 5.
I, , 1.
L, , 50.
X, , 10.

44, .


10, 1... 9 I, II, III, IV... IX 10... 90 X, XX, XXX, LX....XL, L ..

& V, 1-9, X am, 10-90 ..

+3

, , , , .

:

// Decimal digit lookup tables
static const char* thousands[] = { "", "M", "MM", "MMM" } ;
static const char* hundreds[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" } ;
static const char* tens[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" } ;
static const char* units[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" } ;
static const char** digits[] = { thousands, hundreds, tens, units } ;

, ( ), " [] []" . , , .

, , 1234 "M" + "CC" + "XXX" + "IV" = "MCCXXXIV".

, , Wikipedia: Roman Numerals - Symbols, , "", (.. - , !). , , 3999, , ASCII, 1 3999, Unicode.

, , , , , , . , , .

+1

Source: https://habr.com/ru/post/1739675/


All Articles