Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

//**************************************

//     

//INCLUDE files for :Convert numeric str

//     ing to 7-segment displays

//**************************************

//     

/* +++Date last modified: 05-Jul-1997 */

/*

** NUMCNVRT.H - Header file for SNIPPETS numerical <=> string conversions

*/

#ifndef NUMCNVRT__H

#define NUMCNVRT__H

#include  /* for size_t */

#include "sniptype.h"

#include "round.h"

#include "pi.h"

#define R_ERROR -2 /* EVAL.C Range error*/

/*

** Callable library functions begin here

*/

char * base_convert(const char *in, char *out,

size_t len, int rin, int rout);/* Bascnvrt.C */

char * comma_float(double num, char *buf, int dec); /* Commaflt.C */

size_t commafmt(char *buf, int bufsize, long N); /* Commafmt.C */

char * eng(double value, int places);/* Eng.C */

int evaluate(char *line, double *val);/* Eval.C */

char * fmt_money(double amt);/* Fmtmoney.C */

longhexorint(const char *string); /* Hexorint.C */

char * ltostr(long num, char *string,

size_t max_chars, unsigned base); /* Ltostr.C*/

char * ordinal_text(int number); /* Ord_Text.C */

int scanfrac (const char buf[], double *f);/* Scanfrac.C */

unsigned int hstr_i(char *cptr); /* Hstr_I.C*/

char *long2roman(long val, char *buf, size_t buflen); /* L2Roman.C */

long roman2long(const char *str);/* Roman2L.C */

#if defined(__ZTC__) && !defined(__SC__)

char * ltoa(long val, char *buf, int base); /* Ltoa.C */

#endif

/*

** File: STR27SEG.C

*/

    struct Seg7disp {

    unsigned seg_a : 1;

    unsigned seg_b : 1;

    unsigned seg_c : 1;

    unsigned seg_d : 1;

    unsigned seg_e : 1;

    unsigned seg_f : 1;

    unsigned seg_g : 1;

};

char *str27seg(char *string);

#endif /* NUMCNVRT__H */

 

code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/* +++Date last modified: 05-Jul-1997 */

/*

** STR27SEG.C - Convert numeric strings to 7-segment strings.

**

** public domain by Bob Stout

**

** Input: A string (NUL-delimited char array) containing only digits

** ('0' - '9' chars).

**

** Output: The same string with each digit converted to a 7-segment

** representation. Returns NULL on error.

*/

#include 

#include 

#include "numcnvrt.h"

#define CAST(new_type,old_object) (*((new_type *)&old_object))

#define DISP(str) fputs((str), stdout)

/*

** Define the bit significance

**

** a

**---

**||

** f||b

**| g |

**---

**||

** e||c

**||

**---

** d

*/

    struct Seg7disp Seg7digits[10] = {

        { 1, 1, 1, 1, 1, 1, 0 }, /* 0 */

            { 0, 1, 1, 0, 0, 0, 0 }, /* 1 */

                { 1, 1, 0, 1, 1, 0, 1 }, /* 2 */

                    { 1, 1, 1, 1, 0, 0, 1 }, /* 3 */

                        { 0, 1, 1, 0, 0, 1, 1 }, /* 4 */

                            { 1, 0, 1, 1, 0, 1, 1 }, /* 5 */

                                { 1, 0, 1, 1, 1, 1, 1 }, /* 6 */

                                    { 1, 1, 1, 0, 0, 0, 0 }, /* 7 */

                                        { 1, 1, 1, 1, 1, 1, 1 }, /* 8 */

                                            { 1, 1, 1, 1, 0, 1, 1 }/* 9 */

                                        };

                                        char *str27seg(char *string)

                                            {

                                            char *str;

                                            int ch;

                                            for (str = string ; *str; ++str)

                                                {

                                                if (!isdigit(*str))

                                                return NULL;

                                                ch = CAST(int, Seg7digits[*str - '0']);

                                                *str = (char)(ch & 0xff);

                                            }

                                            return string;

                                        }

                                        #ifdef TEST

                                        main()

                                            {

                                            char tstrng[] = "0123456789", *segs;

                                            printf("str27seg() returned %p", segs = str27seg(tstrng));

                                            if (segs)

                                                {

                                                char *ptr;

                                                struct Seg7disp ch;

                                                int i;

                                                puts(" & produced:\n");

                                                for (ptr = segs ; *ptr; ++ptr)

                                                    {

                                                    ch = CAST(struct Seg7disp, *ptr);

                                                    if (ch.seg_a)

                                                    DISP(" --- ");

                                                    else DISP(" ");

                                                    DISP(" ");

                                                }

                                                puts("");

                                                for (i = 0; i < 3; ++i)

                                                    {

                                                    for (ptr = segs ; *ptr; ++ptr)

                                                        {

                                                        ch = CAST(struct Seg7disp, *ptr);

                                                        if (ch.seg_f)

                                                        DISP("|");

                                                        else DISP("");

                                                        if (ch.seg_b)

                                                        DISP("|");

                                                        else DISP(" ");

                                                        DISP(" ");

                                                    }

                                                    puts("");

                                                }

                                                for (ptr = segs ; *ptr; ++ptr)

                                                    {

                                                    ch = CAST(struct Seg7disp, *ptr);

                                                    if (ch.seg_g)

                                                    DISP(" --- ");

                                                    else DISP(" ");

                                                    DISP(" ");

                                                }

                                                puts("");

                                                for (i = 0; i < 3; ++i)

                                                    {

                                                    for (ptr = segs ; *ptr; ++ptr)

                                                        {

                                                        ch = CAST(struct Seg7disp, *ptr);

                                                        if (ch.seg_e)

                                                        DISP("|");

                                                        else DISP("");

                                                        if (ch.seg_c)

                                                        DISP("|");

                                                        else DISP(" ");

                                                        DISP(" ");

                                                    }

                                                    puts("");

                                                }

                                                for (ptr = segs ; *ptr; ++ptr)

                                                    {

                                                    ch = CAST(struct Seg7disp, *ptr);

                                                    if (ch.seg_d)

                                                    DISP(" --- ");

                                                    else DISP(" ");

                                                    DISP(" ");

                                                }

                                                puts("");

                                            }

                                            else puts("\n");

                                            return 0;

                                        }

                                        #endif /* TEST */