Jump to content
xisto Community
Larry Rosario

C++ Roman Numeral Conversion Code Help If-else Problem

Recommended Posts

Sorry, I don't know how to program in C, but I did do something a little similar for MS Access. I had to write a script that would convert numerals into text for the printing of cheques with a maximum value of $99,999.99. e.g. $158.50 = "One Hundred and Fifty Eight Dollars and Fifty Cents". I managed to do it using If... Else statements, but it took me a very long time to do it with a lot of lines of code. When I was doing it, I found it easiest to start from the lowest value and work my way up from there. Perhaps someone with knowledge of C can help you more. Good luck ;)

Share this post


Link to post
Share on other sites

I haven't been able to find any help doing this in C/C++, but there is a JavaScript work that does it very well, and shows off the sort of methods and logic you will have to apply. The source code is licensed under the Creative Commons. If you understand JS I suggest you read through it and see if you could do something similar for your code.

Share this post


Link to post
Share on other sites

Oh, ok. Not too bad a programming job/homework assignment. ;)

As a basic technique, you could divide the input number by each roman numeral 'letter' starting highest first.
E.g. If the input is x, you'd take the floor function of x / 1000 to find the number of M's needed, subtract a thousand times the result from x, and repeat for each following 'unit' such as 500, 100, 10, 5, and then 1, storing each floored result in a separate variable
-such as

short int M //holds how many 'M's there are

The only reason I would use an if-else statement is to calculate several exceptions (such as if you have 1 'V' and 4 'I's, write IX instead of VIIII), format the data about a bit, and of course check if the input variable is greater than 3000 (immediately exiting the program if so).

By the way, a convenient way that also takes a lot less calculation time is have lookup tables for the data -in case you're programming for performance instead of writing the code as soon as possible.

Share this post


Link to post
Share on other sites
This is not that easy problem, you have to work hard. Make your mind on the problem and think about what the relation on "integers" and "Roman Numbers". Osknockout describe in right way. Try this and tell us what happen.

Share this post


Link to post
Share on other sites

thanks

This code doesen't do Roman Numbers, but it does like Avalon talked about. It does it as 101="One Hundred and One", or if you pass the possessive to TRUE, will return "One Hundred First".

Maybe this will give you some ideas.

char *tens[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};char *ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve",  "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};char *possessiveones[] = { "", "first", "second", "third", "fourth", "fifth", "", "", "", "", "", "", "twelfth" };char *possessivetens[] = { "", "", "twentieth", "thirtieth", "fortieth", "fiftieth", "sixtieth", "seventieth", "eightieth", "ninetieth" };CString NumberToText(long num, CString Retval, bool bPossessive){   long rem;   if(num < 0)    {      Retval += "negative ";      num = - num;   }      if(num >= 1000000000)    {      rem = num % 1000000000;      Retval += NumberToText(num / 1000000000);      if(!rem && bPossessive)      {         Retval += "billionth";      }      else      {         Retval += "billion ";      }      if(rem)      {         Retval = NumberToText(rem, Retval, bPossessive);      }   }       if(num >= 1000000)    {      rem = num % 1000000;      Retval += NumberToText(num / 1000000);      if(!rem && bPossessive)      {         Retval += "millionth";      }      else      {         Retval += "million ";      }      if(rem)      {         Retval = NumberToText(rem, Retval, bPossessive);      }   }    else if(num >= 1000)    {      rem = num % 1000;      Retval += NumberToText(num / 1000);      if(!rem && bPossessive)      {         Retval += "thousandth";      }      else      {         Retval += "thousand ";      }      if(rem)      {         Retval = NumberToText(rem, Retval, bPossessive);      }   }    else if(num >= 100)    {      rem = num % 100;      Retval += NumberToText(num / 100);      if(!rem && bPossessive)      {         Retval += "hundredth";      }      else      {         Retval += "hundred ";      }      if(rem)      {         Retval = NumberToText(rem, Retval, bPossessive);      }    }   else if(num >= 20)    {      rem = num % 10;      CString ten;      if(!rem && bPossessive)      {         ten.Format("%s ", possessivetens[num/10]);      }      else      {         ten.Format("%s ", tens[num/10]);      }      Retval += ten;      if(rem)       {         Retval = NumberToText(rem, Retval, bPossessive);      }    }   else   {      CString res;      if(bPossessive && num < 6)      {         res.Format("%s", possessiveones[num]);      }      else if((bPossessive) && (num > 5 && num < 20 && num != 12))      {         res.Format("%sth", ones[num]);      }      else if(bPossessive && num == 12)      {         res.Format("%s", possessiveones[num]);      }      else       {         res.Format("%s ", ones[num]);      }      Retval += res;   }   return Retval;}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.