Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> C++ Roman Numeral Conversion Code Help, If-else Problem
Larry Rosario
post Feb 3 2007, 10:22 AM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 10
Joined: 6-January 07
Member No.: 36,680



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
post Feb 3 2007, 10:33 AM
Post #2


Privileged Member
*********

Group: Members
Posts: 630
Joined: 12-August 05
From: Melbourne, Australia
Member No.: 10,624



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
post Feb 3 2007, 01:14 PM
Post #3


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,056
Joined: 28-May 05
From: Hertfordshire, England
Member No.: 7,593
Spam Patrol



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
post Feb 7 2007, 01:38 AM
Post #4


Super Member
*********

Group: Members
Posts: 397
Joined: 14-November 04
From: Elysium
Member No.: 2,280



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
post Jul 31 2007, 06:03 PM
Post #5


Newbie [Level 3]
***

Group: Members
Posts: 47
Joined: 22-July 07
From: Dhaka, Bangladesh
Member No.: 46,859



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
post Jan 25 2008, 10:01 PM
Post #6


Newbie
*

Group: Members
Posts: 3
Joined: 25-January 08
Member No.: 56,859



QUOTE(Larry Rosario @ Feb 3 2007, 02:22 AM) *
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.

CODE

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;
}

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. C Code, Can U Solve This?(22)
  2. Code Documentation(1)
  3. Simple C File Handling In Action(3)
  4. Prob With My C Code(4)
  5. Source Code For Paint Like Program Under Dos In C(2)
  6. Wolfenstein Source Code Now In Public Domain(3)
  7. A Telephone Directory Source Code In C(2)
  8. Most Efficient Code To Get Prime Numbers(7)
  9. I Need Help(7)


 



- Lo-Fi Version Time is now: 26th July 2008 - 02:00 PM