Click to See Complete Forum and Search --> : A simple program with a tough logic!!!
akshayakrsh
October 25th, 2004, 03:31 PM
hi everyone
this question came to my mind and has troubling me now for almost a month....
so guys plz help me on this!!!
the program is very simple. in it a user has to enter a no. and what he gets the output is the same no. but in words instead of figures i.e. the output is somewhat like as follows:
Enter a no.:1121
The no. is:One thousand one hundred and twenty one
now that's what i want!!! and if anybody does this for me i'll be highly obliged!!!!
so guys i m waiting for ur answers!!!
sec_ware
October 25th, 2004, 03:41 PM
Take this as a start:
Number to Text Demo [VB5]
This demonstration program shows how to convert numbers to English as you might see in a check writing program. For example, 12345.67 is converted to "Twelve thousand, three hundred forty-five and 67/100".[1]
[1] http://www.softcircuits.com/sw_vbsrc.htm
akshayakrsh
October 25th, 2004, 03:47 PM
i just forgot to mention the main point!
i m sorry about that!
the point is i wanna do this using C/C++
i m sorry again!
plz send me the answers!
Guus
October 25th, 2004, 03:50 PM
A simple solution whould be to break down your original number in digits, and parse those digits one by one, using a switch/case-statement. In psuedo, you'd get something like this:
var $number = 1234
var $thousands = 1
var $hundreds = 2
var $tens = 3
var $ones = 4
switch ($thousands)
{
case: 1: $thousandstring = "One thousand "; break;
case: 2: $thousandstring = "Two thousand "; break;
case: 3: $thousandstring = "Three thousand "; break;
// etc
}
switch ($hundreds)
{
case: 1: $hundredsstring = "One hundred "; break;
case: 2: $hundredsstring = "Two hundred "; break;
// etc
}
// do the same for the tens and ones
// finally, concat the four strings you got:
$result = $thousandstring + $hundredsstring + $tensstring + $onesstring;
akshayakrsh
October 25th, 2004, 03:55 PM
var $number = 1234
var $thousands = 1
var $hundreds = 2
var $tens = 3
var $ones = 4
switch ($thousands)
{
case: 1: $thousandstring = "One thousand "; break;
case: 2: $thousandstring = "Two thousand "; break;
case: 3: $thousandstring = "Three thousand "; break;
// etc
}
switch ($hundreds)
{
case: 1: $hundredsstring = "One hundred "; break;
case: 2: $hundredsstring = "Two hundred "; break;
// etc
}
// do the same for the tens and ones
// finally, concat the four strings you got:
$result = $thousandstring + $hundredsstring + $tensstring + $onesstring;
hey i know this will work in C/C++ but plz tell me what header files i'll require to run this program especially "var"!!!!!
do tell me
thnx anyway!
sec_ware
October 25th, 2004, 03:57 PM
remark 1: take care of exceptions. all you need is given in the vb I attached:
Ones(0) = "zero"
Ones(1) = "one"
Ones(2) = "two"
Ones(3) = "three"
Ones(4) = "four"
Ones(5) = "five"
Ones(6) = "six"
Ones(7) = "seven"
Ones(8) = "eight"
Ones(9) = "nine"
Teens(0) = "ten"
Teens(1) = "eleven"
Teens(2) = "twelve"
Teens(3) = "thirteen"
Teens(4) = "fourteen"
Teens(5) = "fifteen"
Teens(6) = "sixteen"
Teens(7) = "seventeen"
Teens(8) = "eighteen"
Teens(9) = "nineteen"
Tens(0) = ""
Tens(1) = "ten"
Tens(2) = "twenty"
Tens(3) = "thirty"
Tens(4) = "forty"
Tens(5) = "fifty"
Tens(6) = "sixty"
Tens(7) = "seventy"
Tens(8) = "eighty"
Tens(9) = "ninety"
Thousands(0) = ""
Thousands(1) = "thousand" 'US numbering
Thousands(2) = "million"
Thousands(3) = "billion"
Thousands(4) = "trillion"
remark 2: how to get the digits:
...
thousands=int(your_number)/1000;
your_number-=1000*thousands;
...
hundreds=int(your_number)/100 ;
your_number-=100*hundreds;
...
Can you do the rest?
Cheers
/edit: some output example
...
if (thousands!=0)
printf("... %s thosand\t",ones[thousands]);
if (hundredss!=0)
printf("... %s hundred\t",ones[hundreds]);
...
akshayakrsh
October 25th, 2004, 03:58 PM
one more thing above in the code we are making cases but what in case of 99000, will i hav to 99 cases each time? is there a more simpler way?
SirDice
October 25th, 2004, 04:00 PM
Have a look at the Lingua::Num2Word perl module.
http://search.cpan.org/~rvasicek/Lingua-Num2Word-0.07/Num2Word.pm
Edit: Ah. You need something in C/C++ :D
akshayakrsh
October 25th, 2004, 04:01 PM
hey sec_ware
this may sound stupid
but what u have given is VB, right?
but i want it in C/C++
thnx anyway
akshayakrsh
October 25th, 2004, 04:11 PM
thank u SirDice that really worked
sec_ware
October 25th, 2004, 04:11 PM
Ok.
First: Create these string-arrays, for example with
char one[3][5]={"zero","one","two"}; // HORRIBLE! :D
//test:
printf("%s",one[1]);
Second: get the digits, as suggested, of your number, for example
...
thousands=int(your_number)/1000;
your_number-=1000*thousands;
...
hundreds=int(your_number)/100 ;
your_number-=100*hundreds;
...
Be careful if the number after the "hundreds"-step is smaller than 20 -> additional cases.
Third: Print out according to the way you want to present the number
for example with
...
if (thousands!=0)
printf("... %s thousand\t",ones[thousands]);
if (hundredss!=0)
printf("... %s hundred\t",ones[hundreds]);
...
Remark: this is all c.
/edit:about your problem: 99000
you can solve it writing a recursive function print_the_number:
...
if (thousands!=0)
printf("... %s thousand\t",print_the_number(thousands));
...
where print_the_number is again the same function with which
you initially wanted to print your starting number (recursive approach).
akshayakrsh
October 25th, 2004, 04:33 PM
hey sec_ware
now i got the point what u wanted to say(i suppose)
will hav a work on that thing
n tell u what figures out
but still check out the link given SirDice, which is:
http://search.cpan.org/~rvasicek/Li....07/Num2Word.pm
thank u
skiddieleet
October 25th, 2004, 05:02 PM
I hope you're not in my class cause you're a couple of weeks late on this assignment.
akshayakrsh
October 25th, 2004, 05:08 PM
hey h3r3tic
yes i m late for the class u say
but plz tell me what's that class and what it is about
i'll be glad to know that
thnx
Guus
October 25th, 2004, 05:11 PM
Oh, come on, do at least -something- yourself.
AO members have been more than helpful. If you want them to write the damn thing completely, ask that instead, but stop giving 'subtle' hints for more information.
With all the information you've been given, you should be able to do it yourself by now.
skiddieleet
October 25th, 2004, 05:18 PM
Here is the logic we used. We would have functions for each category such as single digits, teens, tens (twenty, thirty...), hundreds, thousands... then it sort of repeats from there. So we had a function to convert each case to a string down the line. Here is a little of the code.
static char * digitToString(int number)
/* precondition: number < 10 */
{
char * word = (char *)malloc(8);
if(word == NULL)
{
fprintf(stderr, "out of memory in digitToString\n");
exit(1);
}
if(number == 0) return strcpy(word,"zero");
.
.
.
static char * teensToString(int number)
/* precondition: 10 < number < 20 */
{
char * word = (char *)malloc(12);
if(word == NULL)
{
fprintf(stderr, "out of memory in teensToString\n");
exit(1);
}
if(number == 11) return strcpy(word,"eleven");
.
.
.
static char * tensPrefix(int number)
/* precondition: number is a multiple of 10 less than 100 */
{
char * word = (char *)malloc(8);
if(word == NULL)
{
fprintf(stderr, "out of memory in tensPrefix\n");
exit(1);
}
if(number == 10) return strcpy(word,"ten");
.
.
.
static char * under100 (int number)
/* precondition: number < 100 */
{
if(number < 10)
return digitToString(number);
else if(number % 10 == 0)
return tensPrefix(number);
else if(10 < number && number < 20)
return teensToString(number);
else
{
char * temp = tensPrefix(number/10*10);
char * word = (char *)malloc(24);
if(word == NULL)
{
fprintf(stderr, "out of memeory in digitToString\n");
exit(1);
}
.
.
.
static char * under1000(int number)
/* precondition: number < 1000 */
{
if(number < 100)
return (char*)under100(number);
else
{
if(number % 100 == 0)
{
char * temp = digitToString(number/100);
char * word = (char *)malloc(20);
if(word == NULL)
{
fprintf(stderr, "out of memory in under1000\n");
exit(1);
}
strcpy(word, temp);
free(temp);
strcat(word, "-hundred");
return word;
}
else
{
char * temp = digitToString(number/100);
char * word = (char *)malloc(32);
if(word == NULL)
{
fprintf(stderr, "out of memory in digitToString\n");
exit(1);
}
.
.
.
char * numToString(int number)
/* calls the previous functions accordingly */
I know there are many ways to do it, but that's how we did it for our assignment, in a nutshell. Hopefully you can piece it together from there. Our program just went from 0 - 1000000. Remember to free the memory you allocate once you're through with what is using it. Peace.
sec_ware
October 25th, 2004, 05:35 PM
"And you shall not" misuse faithful AO members!
Puzzles, questions out of curiosity are a nice thing -
it keeps the brain "to be on the ball".
Some AO members like challenges like this, even if
they are simple from time to time.
Don't misuse the knowledge/skills/whatever of others
in order to get done what you (sic) are supposed to do.
Letting "us" solve your assignments without telling
us in advance so is not good behaviour.