1

I need to convert Arabic to Roman. I have a method number(int place), that gets each digit from a certain number.

Example: 5821, where the number method at place 0 = 1; number(2) = 8, etc.

I now need to write a method (with a helper), that converts these characters into roman numerals. This wouldn't normally be difficult, but I can't use arrays, and this method has to work for the three cases (1's, 10's, and 100's); so I can't write a case for each numeral (otherwise I could many switch or if's to cover the cases).

Ideas anyone?

kapa
  • 77,694
  • 21
  • 158
  • 175
Carlos
  • 11
  • 1
  • 1
    http://stackoverflow.com/questions/4986521/how-to-convert-integer-value-to-roman-numeral-string/4986630#4986630 may help somewhat. – paxdiablo May 12 '11 at 14:48
  • 2
    I think splitting the number in digits is not really useful here - simply use the modulo operator `%` to get what is needed. – Paŭlo Ebermann May 12 '11 at 14:49
  • Yes, using arrays was my first idea, but I can't use them; I'm supposed to get each digit and convert it. – Carlos May 12 '11 at 14:49
  • @Paul, I think only subtraction and comparison are needed. – Ingo May 12 '11 at 15:00

2 Answers2

2

Since this is homework, the pseudo code below is deliberately left incomplete.

string toRomanString (int aNumber)
{
  string result = "";
  if (aNumber < 1 || aNumber.toString().length() > 4)
   throw NotImplementedException();

  for(int i=0; i < aNumber.toString().length(); i++)
  {
    if(i = 0)
    {
      throw NotImplementedException();
    }
    elseif(i = 1)
    {
      throw NotImplementedException();
    }
    elseif(i = 2)
    {
      throw NotImplementedException();
    }
    else
    {
      throw NotImplementedException();
    }
  }
}
Thomas Langston
  • 3,743
  • 1
  • 25
  • 41
2

Maybe instead of thinking about getting a digit, you should think about getting a value. Think of the value you're displaying as a sum of values, each of which can be itself simply expressed in Roman numerals.

convert(5000) + convert(800) + convert(20) + convert(1)
Dave DuPlantis
  • 6,378
  • 3
  • 26
  • 30