| | thanks |
|
|
|
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
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.
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.
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.
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; }
Recent Queries:-
Keywords : c, roman, numeral, conversion, code, problem
(7) Can anyone write a more efficient code than this to get the prime numbers from 1 -999? // (2) Well i had made this project some years before. It is a telephone directory which has a very good (3) the Wolfenstein game which we enjoy it alot becouse of its highly graphic technique now u can (2) this is a dos program that you have been waiting for you learn from it you how to do graphic (4) Hi Guys! this is a wonderful place.. although i wasnt a member till now i have referred to Small code snipet which covers most of basic file handling and navigat (3) Yesterday I suddenly got a lot of work. The same work we try to push off, yes you are right all A better way? (1) This is not so much a question, actually, but a bit of a plug for an interesting tool I've (2) Here is a short and basic piece of my first ever application written in Visual C++ 6.0 . I have also Another interesting problem (22) Hello, Look at the code given below CODE void fun(void) { /* Put your code here so where can i get full souce code of Turbo (2) where can i get full souce code of Turbo C++ ? Al... it must be complete... complete... includes and Looking for c, roman, numeral, conversion, code, problem
|
|
![]() C++ Roman Numeral Conversion Code Help - If-else Problem |
| ADD REPLY / Got an Opinion! | Remove these ADs! | RAPID SEARCH! | Free Web Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute more info. to help others. Ask your Doubts & Queries to get answers, So that "Together We can help others!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|