• Welcome to Valhalla Legends Archive.
 

Decimal to Roman Numeral Convertor

Started by Vision, March 03, 2004, 02:03 PM

Previous topic - Next topic

Vision

Im in c++ class right now and we need to write a program that you input a number and it outputs the number in Roman numerals can someone give me an idea for wat to use because i havent been to school to find out how were suppost to and the teachers gay and wont tell me...

Eibro

#1
Something that immediatly comes to mind:

std::string returnString;

const long rnChars[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };
const long rnDigits[] = { 1000, 500, 100, 50, 10, 5, 1, 0 };

for ( long i = 0; i < rnDigits[i] != 0; ++i )
{
  long quotient = input / rnDigits[i];
  long remainder = input % rnDigits[i];
   
  returnString.append( quotient,  rnChars[i] );
  input = remainder;
}
The above will *not* work. It should at least get you started. Basically, you'll want to keep extracting digits and dividing to figure out how many multiples of each letter to add.
Eibro of Yeti Lovers.

Vision


Mephisto

You should just do this by yourself.  :P

I've written one of these along time ago.  I'll post the source if you find you desperately need it.  :P  But it's rather simple, just a matter of logic more or less.