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
|
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
string decimal2roman(int input)
{
string romanvalue = "";
if(input >= 4000)
{
int x = (input - input % 4000) / 1000;
romanvalue = "(" + decimal2roman(x) + ")" ;
input %= 4000;
}
const string roman[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
const int decimal[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for (int i = 0; i < 13; i++)
{
while (input >= decimal[i])
{
input -= decimal[i];
romanvalue += roman[i];
}
}
return romanvalue;
}
int main()
{
cout << decimal2roman(1899) << endl;
cout << decimal2roman(4000) << endl;
cout << decimal2roman(4564789) << endl;
return 0;
}
|
MDCCCXCIX
(IV)
((IV)DLXIV)DCCLXXXIX |