Fix bug 4112: Translate to roman numerals for numbers higher than 20.

We now go up to 999. If you have more sections that that, you are out
of luck!


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@19363 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2007-08-08 15:10:55 +00:00
parent f3c3b9bf02
commit a95c723f5c

View File

@ -6,6 +6,7 @@
* \author Lars Gullik Bjønnes * \author Lars Gullik Bjønnes
* \author Martin Vermeer * \author Martin Vermeer
* \author André Pönitz * \author André Pönitz
* \author Richard Heck (roman numerals)
* *
* Full author contact details are available in file CREDITS. * Full author contact details are available in file CREDITS.
*/ */
@ -243,33 +244,71 @@ char hebrewCounter(int const n)
} }
docstring const lowerromanCounter(int const n)
//On the special cases, see http://mathworld.wolfram.com/RomanNumerals.html
//and for a list of roman numerals up to and including 3999, see
//http://www.research.att.com/~njas/sequences/a006968.txt. (Thanks to Joost
//for this info.)
docstring const romanCounter(int const n, bool lowercase = false)
{ {
static char const * const roman[20] = { static char const * const ones[9] = {
"i", "ii", "iii", "iv", "v", "I", "II", "III", "IV", "V",
"vi", "vii", "viii", "ix", "x", "VI", "VII", "VIII", "IX"
"xi", "xii", "xiii", "xiv", "xv",
"xvi", "xvii", "xviii", "xix", "xx"
}; };
if (n < 1 || n > 20) static char const * const tens[9] = {
"X", "XX", "XXX", "XL", "L",
"LX", "LXX", "LXXX", "XC"
};
static char const * const hunds[9] = {
"C", "CC", "CCC", "CD", "D",
"DC", "DCC", "DCCC", "CM"
};
if (n > 1000 || n < 1)
return from_ascii("??"); return from_ascii("??");
return from_ascii(roman[n - 1]);
int val = n;
string roman;
switch (n) {
//special cases
case 900:
roman = "CM";
break;
case 400:
roman = "CD";
break;
default:
if (val >= 100) {
int hundreds = val / 100;
roman = hunds[hundreds - 1];
val = val % 100;
}
if (val >= 10) {
switch (val) {
//special case
case 90:
roman = roman + "XC";
val = 0; //skip next
break;
default:
int tensnum = val / 10;
roman = roman + tens[tensnum - 1];
val = val % 10;
} // end switch
} // end tens
if (val > 0)
roman = roman + ones[val -1];
}
docstring retval = from_ascii(roman);
return lowercase ? support::lowercase(retval) : retval;
} }
docstring const romanCounter(int const n) docstring const lowerromanCounter(int const n)
{ {
static char const * const roman[20] = { return romanCounter(n, true);
"I", "II", "III", "IV", "V",
"VI", "VII", "VIII", "IX", "X",
"XI", "XII", "XIII", "XIV", "XV",
"XVI", "XVII", "XVIII", "XIX", "XX"
};
if (n < 1 || n > 20)
return from_ascii("??");
return from_ascii(roman[n - 1]);
} }
} // namespace anon } // namespace anon