Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- std::string IntToRomanStr(unsigned short value)
- {
- if (value < 1)
- return "0";
- if (value > 5000)
- return "";
- struct SIntToRomanStr
- {
- unsigned short value;
- std::string strRoman;
- };
- static SIntToRomanStr const digits_subs[] =
- {
- 1000, "M", 900, "CM", 500, "D", 400, "CD", 100, "C", 90, "XC", 50, "L", 40, "XL", 10, "X", 9, "IX", 5, "V", 4, "IV", 1, "I"
- };
- std::string substr;
- for (SIntToRomanStr const* type_identifier = digits_subs; type_identifier->value > 0; ++type_identifier)
- {
- for (; value >= type_identifier->value;)
- {
- substr += type_identifier->strRoman;
- value -= type_identifier->value;
- }
- }
- return substr.c_str();
- }
- //@Lua - Darisil
- IntToRomanStr = function(int)
- local real_int = tonumber(int);
- if (real_int < 1) then
- return "0";
- elseif (real_int > 5000) then
- return "";
- end -- if/elseif
- local str = "";
- local array = {
- [1] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
- [10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
- [100] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
- [1000] = {"", "M", "MM", "MMM", "MMMM", "MMMMM"}
- };
- for x = 3, 0, -1 do
- local index = math.pow(10, x);
- local subindex = math.floor(real_int / index);
- str = string.format("%s%s", str, array[index][subindex+1]);
- real_int = real_int - subindex * index;
- end -- for
- return str;
- end -- function
Advertisement
Add Comment
Please, Sign In to add comment