When converting Roman Numerals, how do I tell the code to consider the fact that the letters are also used for subtraction? Like, X=10 and L=50, but XL is 40 and not 60. How do I do this ( instead of listing all combination )?
include <string>
using std::string;
class decimal2roman {
public string romanvalue;
decimal2roman(int input) {
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};
romanvalue = "";
for (int i = 0; i < 13; i++) {
while (input >= decimal[i]) {
input -= decimal[i];
romanvalue += roman[i];
}
}
}
publicstring toString ()
{
return romanvalue;
}
Does anybody know how I can add that? I can't think of anything, honestly.