I have to create a program that counts roman numerals. I'm not sure how I would do the math in it? I know if the smallest number is in front, you minus them... but I don't know how I would write that? Here is my code so far :
Here's a function that will turn a roman numeral string to an int. If I feel like it tonight I may try and write one that converts an int back to a roman numeral string.
int RomanCharToInt(char in)
{ // Converts a single roman character to an integer
if(in <0x60) in += 0x20; // Converts to lower-case (if applicable)
switch (in)
{
case'm': return 1000;
case'd': return 500;
case'c': return 100;
case'l': return 50;
case'x': return 10;
case'v': return 5;
case'i': return 1;
}
return -9999999; // Very obscure number so the user knows there was a problem.
}
int RomanToInt(std::string &in)
{ // Converts a whole string of roman numerals to an integer
int number = 0;
int i = in.size()-1;
// Get the first one
number += RomanCharToInt(in[i--]);
//Now loop for the rest
while ( i > -1)
if (RomanCharToInt(in[i]) < RomanCharToInt(in[i+1]))
number -= RomanCharToInt(in[i--]); // If it's less than the one before, subtract the value
else
number += RomanCharToInt(in[i--]); // else add the value
return number;
}