A simple program with a tough logic!!! - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: A simple program with a tough logic!!!

  1. #11
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Ok.

    First: Create these string-arrays, for example with
    Code:
    	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
    Code:
    ...
    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
    Code:
    ...
    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:
    Code:
    ...
    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).


    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  2. #12
    Banned
    Join Date
    Apr 2004
    Posts
    94
    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

  3. #13
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    I hope you're not in my class cause you're a couple of weeks late on this assignment.

  4. #14
    Banned
    Join Date
    Apr 2004
    Posts
    94
    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

  5. #15
    Hi mom!
    Join Date
    Aug 2001
    Posts
    1,103
    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.
    I wish to express my gratitude to the people of Italy. Thank you for inventing pizza.

  6. #16
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    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.
    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.

  7. #17
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    "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.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides