1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <string>
std::string to_roman(unsigned int i)
{
if (i > 3999) return {}; // 3999, or MMMCMXCIX, is the largest standard Roman numeral
static const std::string ms[] { "","M","MM","MMM" };
static const std::string cds[] { "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM" };
static const std::string xls[] { "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC" };
static const std::string ivs[] { "","I","II","III","IV","V","VI","VII","VIII","IX" };
return ms[i / 1000] + cds[(i % 1000) / 100] + xls[(i % 100) / 10] + ivs[i % 10];
}
// return values for individual Roman Number characters
unsigned int from_roman(char c)
{
switch (c)
{
case 'I': return 1; case 'V': return 5; case 'X': return 10;
case 'L': return 50; case 'C': return 100; case 'D': return 500;
case 'M': return 1000; default: return 0;
}
}
// parse the full string for individual characters
unsigned int from_roman(std::string roman)
{
unsigned int result {};
for (size_t i {}, n { roman.length() }; i < n; ++i)
{
const auto j { from_roman(roman[i]) }; // Integer value of the i'th roman digit
// Look at the next digit (if there is one) to know whether to add or subtract j
if (i + 1 == n || j >= from_roman(roman[i + 1])) result += j; else result -= j;
}
return result;
}
int main()
{
std::cout << "1234 in Roman numerals is " << to_roman(1234) << std::endl;
std::cout << "MMXX in Arabic numerals is " << from_roman("MMXX") << std::endl;
}
|