IPB

Welcome Guest ( Log In | Register )



Tags
This content has not been tagged yet
 
Reply to this topicStart new topic

C++ Roman Numeral Conversion Code Help

, If-else Problem


Larry Rosario
no avatar
Newbie [Level 1]
*
Group: Members
Posts: 10
Joined: 6-January 07
Member No.: 36,680



Post #1 post Feb 3 2007, 10:22 AM
thanks

This post has been edited by Larry Rosario: Oct 23 2007, 04:53 AM
Go to the top of the page
+Quote Post
Avalon
no avatar
Privileged Member
*********
Group: Members
Posts: 630
Joined: 12-August 05
From: Melbourne, Australia
Member No.: 10,624



Post #2 post Feb 3 2007, 10:33 AM
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 smile.gif
Go to the top of the page
+Quote Post
rvalkass
no avatar
apt-get moo
****************
Group: [MODERATOR]
Posts: 2,711
Joined: 28-May 05
From: Devon, England
Member No.: 7,593
Spam Patrol
myCENT:54.70



Post #3 post Feb 3 2007, 01:14 PM
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.
Go to the top of the page
+Quote Post
osknockout
no avatar
Super Member
*********
Group: Members
Posts: 399
Joined: 14-November 04
From: Elysium
Member No.: 2,280



Post #4 post Feb 7 2007, 01:38 AM
Oh, ok. Not too bad a programming job/homework assignment. smile.gif

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
CODE
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.
Go to the top of the page
+Quote Post
rajibbd
no avatar
Newbie [Level 3]
***
Group: Members
Posts: 45
Joined: 22-July 07
From: Dhaka, Bangladesh
Member No.: 46,859



Post #5 post Jul 31 2007, 06:03 PM
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.
Go to the top of the page
+Quote Post
larryf
no avatar
Newbie
*
Group: Members
Posts: 3
Joined: 25-January 08
Member No.: 56,859



Post #6 post Jan 25 2008, 10:01 PM
QUOTE(Larry Rosario @ Feb 3 2007, 02:22 AM) [snapback]307190[/snapback]
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.

[codebox]
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;
}

[/codebox]
Go to the top of the page
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   3 -Sky- 625 12th June 2009 - 08:48 PM
Last post by: -Sky-
No New Posts 7 odomike 18,953 30th April 2008 - 02:44 PM
Last post by: FeedBacker
No new   97 Xenon 47,176 29th October 2009 - 04:25 PM
Last post by: magiccode9
No New Posts   11 bttfpromo 11,511 14th September 2004 - 08:55 PM
Last post by: Magic-Node
No New Posts   6 XtremeGamer99 6,296 9th February 2005 - 10:48 AM
Last post by: alexwhin
No new   25 gijoe18 24,458 20th February 2009 - 06:55 PM
Last post by: MALISH_
No New Posts   1 BCD 150 3rd September 2009 - 09:28 PM
Last post by: truefusion
No New Posts   5 Tetraca 1,226 15th July 2006 - 11:06 PM
Last post by: Plenoptic
No New Posts   4 icss21 2,973 7th March 2009 - 09:45 PM
Last post by: andreip
No New Posts   3 jglw22 866 26th May 2008 - 02:10 PM
Last post by: Framp
No New Posts   4 Forbidden 7,552 28th March 2008 - 07:37 PM
Last post by: saran
No New Posts   6 sargilla 6,789 4th November 2004 - 02:54 PM
Last post by: Lyon
No New Posts   4 icss21 940 22nd July 2006 - 02:24 PM
Last post by: JField
No New Posts   0 Lyon 3,550 3rd November 2004 - 07:27 PM
Last post by: Lyon
No New Posts 0 Dodger 3,255 3rd November 2004 - 08:34 PM
Last post by: Dodger


 



RSS Open Discussion Time is now: 8th November 2009 - 08:50 AM

Web Hosting Powered by ComputingHost.com.