code to share 
Author Message
 code to share

Inspired by Kylie Jenkins' problem, I wrote this program to convert currency in
numerals to spoken form. I'm in a sharing mood, so I thought I'd post the code.
Let me know if you make any improvements. Thanks.

        david

/*
 * convert currency in numerals to spoken-form
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUL     '\0'
#define ZERO    ones[0]
#define ONE     ones[1]

enum {
    MAXDIGITS = 15    /* largest whole currency amount */

Quote:
};

char  *reverse(char *);
char  *amount(char *);
int    valid(char *);

char *ones[] = {
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thir{*filter*}",
    "four{*filter*}",
    "fif{*filter*}",
    "six{*filter*}",
    "seven{*filter*}",
    "eigh{*filter*}",
    "nine{*filter*}",

Quote:
};

char *tens[] = {
    0,
    0,
    "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety",

Quote:
};

char *hundreds[] = {
    " ",
    " thousand",
    " million",
    " billion",
    " trillion",

Quote:
};

char zeros[MAXDIGITS+1];
char *argv0;

void
usage(void)
{
    fprintf(stderr, "usage: %s amount...\n", argv0);
    exit(1);

Quote:
}

int
valid(char *amt)
{
    size_t    n;
    char     *p, *e;

    /* bad decimal amount or size */
    e = amt + strlen(amt);
    if(p=strrchr(amt, '.')){
        if(e - p != 3)
            return 0;
        if(p - amt > MAXDIGITS)
            return 0;
    }else if(e - amt > MAXDIGITS)
        return 0;

    /* bad character */
    while(amt && (n=strspn(amt, "1234567890.")))
        amt += n;
    return *amt == 0;

Quote:
}

char *
reverse(char *s)
{
    static char buf[BUFSIZ];
    char *e, *p;

    p = buf;
    e = s + strlen(s) - 1;
    while(e >= s)
        *p++ = *e--;
    *p = NUL;
    return buf;

Quote:
}

char *
amount(char *amt)
{
    char      buf[BUFSIZ]={0};
    char     *p;
    size_t    n;      /* numerals to process */
    int       v,      /* value */
              h=0,    /* hundred? */
              m=0;    /* magnitude */
    int       k, offset[] = {2, 1, 1};

    n = strlen(amt);

    /* return "zero" on all zeros */
    if(!strncmp(amt, zeros, n))
        return ones[0];

    p = amt + n;
    while(n){
        k = offset[(n == 1) + h];
        p -= k;
        if(h % 2){
            v = atoi(p) % 10;
            *p = NUL;
            n -= k;
            h=0;
            m++;
            if(v)
                strcat(buf, reverse(" hundred"));
        }else{
            strcat(buf, reverse(hundreds[m]));
            v = atoi(p) % 100;
            *p = NUL;
            n -= k;
            h=1;
        }
        if(v){
            if(v < 20)
                strcat(buf, reverse(ones[v]));
            else{
                if(v%10){
                    strcat(buf, reverse(ones[v%10]));
                    strcat(buf, "-");
                }
                strcat(buf, reverse(tens[v/10]));
            }
            if(n)
                strcat(buf, " ");
        }
    }
    return reverse(buf+1);

Quote:
}

int
main(int argc, char *argv[])
{
    char *p, *s;
    char buf[BUFSIZ];

    argv0 = argv[0];
    if(argc == 1)
        usage();

    memset(zeros, '0', sizeof zeros - 1);
    zeros[sizeof zeros - 1] = NUL;

    for(argv++; *argv; argv++){
        if(valid(*argv)){
            strcpy(buf, *argv);
            if(p=strrchr(buf, '.'))
                *p = NUL;

            s = p == buf ? ZERO : amount(buf);
            printf("%s dollar%s", s, strcmp(s, ONE) ? "s" : "");

            s = p == 0 ? ZERO : amount(p+1);
            printf(" and %s cent%s\n", s, strcmp(s, ONE) ? "s" : "");
        }else
            fprintf(stderr, "invalid: %s\n", *argv);
    }
    return 0;

Quote:
}

--
If 91 were prime, it would be a counterexample to your conjecture.
    -- Bruce Wheeler


Sat, 10 Jan 2004 04:08:36 GMT  
 code to share


Quote:
> Inspired by Kylie Jenkins' problem, I wrote this program to convert
currency in
> numerals to spoken form. I'm in a sharing mood, so I thought I'd post the
code.
> Let me know if you make any improvements. Thanks.

> david

> /*
>  * convert currency in numerals to spoken-form
>  */

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>

> #define NUL     '\0'
> #define ZERO    ones[0]
> #define ONE     ones[1]

> enum {
>     MAXDIGITS = 15    /* largest whole currency amount */
> };

> char  *reverse(char *);
> char  *amount(char *);
> int    valid(char *);

> char *ones[] = {
>     "zero",
>     "one",
>     "two",
>     "three",
>     "four",
>     "five",
>     "six",
>     "seven",
>     "eight",
>     "nine",
>     "ten",
>     "eleven",
>     "twelve",
>     "thir{*filter*}",
>     "four{*filter*}",
>     "fif{*filter*}",
>     "six{*filter*}",
>     "seven{*filter*}",
>     "eigh{*filter*}",
>     "nine{*filter*}",
> };

> char *tens[] = {
>     0,
>     0,
>     "twenty",
>     "thirty",
>     "forty",
       "fourty",
>     "fifty",
>     "sixty",
>     "seventy",
>     "eighty",
>     "ninety",
> };

> char *hundreds[] = {
>     " ",
>     " thousand",
>     " million",
>     " billion",
>     " trillion",
> };

> char zeros[MAXDIGITS+1];
> char *argv0;

> void
> usage(void)
> {
>     fprintf(stderr, "usage: %s amount...\n", argv0);
>     exit(1);
> }

> int
> valid(char *amt)
> {
>     size_t    n;
>     char     *p, *e;

>     /* bad decimal amount or size */
>     e = amt + strlen(amt);
>     if(p=strrchr(amt, '.')){
>         if(e - p != 3)
>             return 0;
>         if(p - amt > MAXDIGITS)
>             return 0;
>     }else if(e - amt > MAXDIGITS)
>         return 0;

>     /* bad character */
>     while(amt && (n=strspn(amt, "1234567890.")))
>         amt += n;
>     return *amt == 0;
> }

> char *
> reverse(char *s)
> {
>     static char buf[BUFSIZ];
>     char *e, *p;

>     p = buf;
>     e = s + strlen(s) - 1;
>     while(e >= s)
>         *p++ = *e--;
>     *p = NUL;
>     return buf;
> }

> char *
> amount(char *amt)
> {
>     char      buf[BUFSIZ]={0};
>     char     *p;
>     size_t    n;      /* numerals to process */
>     int       v,      /* value */
>               h=0,    /* hundred? */
>               m=0;    /* magnitude */
>     int       k, offset[] = {2, 1, 1};

>     n = strlen(amt);

>     /* return "zero" on all zeros */
>     if(!strncmp(amt, zeros, n))
>         return ones[0];

>     p = amt + n;
>     while(n){
>         k = offset[(n == 1) + h];
>         p -= k;
>         if(h % 2){
>             v = atoi(p) % 10;
>             *p = NUL;
>             n -= k;
>             h=0;
>             m++;
>             if(v)
>                 strcat(buf, reverse(" hundred"));
>         }else{
>             strcat(buf, reverse(hundreds[m]));
>             v = atoi(p) % 100;
>             *p = NUL;
>             n -= k;
>             h=1;
>         }
>         if(v){
>             if(v < 20)
>                 strcat(buf, reverse(ones[v]));
>             else{
>                 if(v%10){
>                     strcat(buf, reverse(ones[v%10]));
>                     strcat(buf, "-");
>                 }
>                 strcat(buf, reverse(tens[v/10]));
>             }
>             if(n)
>                 strcat(buf, " ");
>         }
>     }
>     return reverse(buf+1);
> }

> int
> main(int argc, char *argv[])
> {
>     char *p, *s;
>     char buf[BUFSIZ];

>     argv0 = argv[0];
>     if(argc == 1)
>         usage();

>     memset(zeros, '0', sizeof zeros - 1);
>     zeros[sizeof zeros - 1] = NUL;

>     for(argv++; *argv; argv++){
>         if(valid(*argv)){
>             strcpy(buf, *argv);
>             if(p=strrchr(buf, '.'))
>                 *p = NUL;

>             s = p == buf ? ZERO : amount(buf);
>             printf("%s dollar%s", s, strcmp(s, ONE) ? "s" : "");

>             s = p == 0 ? ZERO : amount(p+1);
>             printf(" and %s cent%s\n", s, strcmp(s, ONE) ? "s" : "");
>         }else
>             fprintf(stderr, "invalid: %s\n", *argv);
>     }
>     return 0;
> }

> --
> If 91 were prime, it would be a counterexample to your conjecture.
>     -- Bruce Wheeler



Mon, 12 Jan 2004 01:45:35 GMT  
 code to share

Quote:


>>     "forty",
>        "fourty",

Believe it or not, it really IS "forty". And you quoted a *202-line*
post to add this? Are you trolling or something?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"I am not very happy acting pleased whenever prominent scientists overmagnify
intellectual enlightenment."
   - Anon



Mon, 12 Jan 2004 01:53:24 GMT  
 code to share

Quote:



>>>     "forty",
>>        "fourty",
> Believe it or not, it really IS "forty". And you quoted a *202-line*
> post to add this? Are you trolling or something?

  i dont think hes trolling intentionally.  after seeing two
completely brain dead posts by the ingenious sef, i searched
for his other work.
  after a little reading, i've concluded that he's some sort
of invertebrate which has managed to squirm away from the well
known lineage of evolution in lieu of developing the ablility
to use a terminal with as little brain power as possible.  the
existence of such a creature, while a marvel to science,
produces nothing but the most annoying, aggrivating, and
trivial usenet ng posts.
  never before have i come in contact with a being harboring
such a perfect and extreme mix of idiocy and {*filter*}-ness.

sef, we pray for you timely and painful death.  never post here
again.

--

cash
    http://www.*-*-*.com/ ~gte727u



Mon, 12 Jan 2004 03:15:23 GMT  
 code to share

Quote:

>   i dont think hes trolling intentionally.  after seeing two
> completely brain dead posts by the ingenious sef, i searched
> for his other work.
>   after a little reading, i've concluded that he's some sort
> of invertebrate which has managed to squirm away from the well
> known lineage of evolution in lieu of developing the ablility
> to use a terminal with as little brain power as possible.  the
> existence of such a creature, while a marvel to science,
> produces nothing but the most annoying, aggrivating, and
> trivial usenet ng posts.
>   never before have i come in contact with a being harboring
> such a perfect and extreme mix of idiocy and {*filter*}-ness.

> sef, we pray for you timely and painful death.  never post here
> again.

Wow.  I see.

And who the hell are /you/?

While I am not impressed by sef's current knowledge of C programming,
I see nothing to indicate that it might not improve, and I sure as
hell am not praying for his timely and painful death.  I doubt very
much anyone else here is praying for this, either.  As for his
"{*filter*}-ness", I have not seen this side of him yet at all, or I'm
sure I would have plonked him long ago.  As yet, he has merely spread
some misinformation, which is hardly grounds for a plonk around here
(au contraire) - though it is grounds for some lightly humiliating
correction (make that scathing, if the corrector is Dan Pop ;) ).

OTOH, such utter tripe as you have just posted above in such an
immature and insulting manner, is.  I don't think it helps your case
to post without proper capitalization, either.

Please take a moment and ponder the effect of the post you have
written as quoted above, which has not lowered my opinion of Sef one
iota; but has certainly left a disfavorable impression of yourself.

Micah

--
"When discussing OSes in a fairly neutral way, I try to do so. When
discussing a point where the OS in question behaves particularly
bletcherously (and IME, this is one such), I try as well, but the
temptation often gets the better of me :-/"     -- Richard Bos



Mon, 12 Jan 2004 05:16:20 GMT  
 code to share
Quote:

>   i dont think hes trolling intentionally.  after seeing two
> completely brain dead posts by the ingenious sef, i searched
> for his other work.

<snip>

How many pfaffs for this post?

Quote:
> sef, we pray for you timely and painful death.  never post here
> again.

Mind if I ask _you_ to never post here again?
And no, I do not pray for "you (sic) timely and painful death".

--
Peter Pichler (Increment my "From:" address if your email bounces.)
This message is not solicitation for contact via email or phone.



Mon, 12 Jan 2004 06:01:05 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Back to VS.Net beta2, there was a code share plugin featured browsing code from Internet

2. Share source code securely, inexpensively

3. shared code

4. Sharing object code between configurations

5. how to share the codes in two projects of one solution in visual studio .net

6. shared DLL code among separate executables??

7. Any Codes to "Share a Folder"?

8. DLLs - Shared Code?

9. Shared Libraries vs. Shared Executables

10. UNIX - Shared, non shared libraries need some information about this subject

11. DLL with shared data/class not sharing...

12. file sharing under DOS (share not necessarily loaded)

 

 
Powered by phpBB® Forum Software