Can't Copy and Paste this?

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

 

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

//     

//INCLUDE files for :Convert long intege

//     rs to roman numerals

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

//     

/* +++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 */

/*

**L2ROMAN.C - Converts long integers to Roman numerals

**

**Jim Walsh, Dann Corbit, Bob Stout, and Others made this.

**

**This Program Is Released To The public Domain

**

** ----------------------------------------------------------------------

** Compiling:

**If the symbol ALLOW_BAR_NOTATION is defined, then bar notation is

**allowed. if the Romans wanted to record one million, they did not

**write down 1000 M's. They would right one M with a bar over it.

**Any Roman numeral with a bar over it is multiplied by 1000.

**Unfortunately, this is not a standard character in most character sets.

**If you choose to #define ALLOW_BAR_NOTATION, then you will have to

**translate the symbols to final form yourself.

*/

#include 

#include "sniptype.h"

    typedef struct tag_RomanToDecimal {

    long PostValue;

    char *PostLetter;

    long PreValue;

    char *PreLetter;

} R2D;

/*

** Set PrefixesAreOK = True_ to enable prefix notation (e.g. 4 = "IV")

** Set PrefixesAreOK = False_ to disable prefix notation (e.g. 4 = "IIII")

*/

static Boolean_T PrefixesAreOK = True_;

    static R2D RomanConvert[] = {

    #if defined(ALLOW_BAR_NOTATION)

    /*

    ** A Roman numeral with a bar over it is

    ** that value multiplied by 1000.

    */

        {1000000L, "", 900000L, ""},

            {500000L, "", 400000L, ""},

                {100000L, "", 90000L, ""},

                    {50000L, "", 40000L, ""},

                        {10000L, "", 9000L, ""},

                            {5000L, "", 4000L, ""},

                            #endif

                                {1000L, "M", 900L, "CM"},

                                    {500L, "D", 400L, "CD"},

                                        {100L, "C", 90L, "XC"},

                                            {50L, "L", 40L, "XL"},

                                                {10L, "X", 9L, "IX"},

                                                    {5L, "V", 4L, "IV"},

                                                        {1L, "I", 1L, "I"}

                                                    };

                                                    /*

                                                    ** long2roman() - Convert a long integer into roman numerals

                                                    **

                                                    ** Arguments: 1 - Value to convert

                                                    ** 2 - Buffer to receive the converted roman numeral string

                                                    ** 3 - Length of the string buffer

                                                    **

                                                    ** Returns: Pointer to the buffer, else NULL if error or buffer overflow

                                                    */

                                                    char *long2roman(long val, char *buf, size_t buflen)

                                                        {

                                                        size_t posn = 0;

                                                        size_t place = 0;

                                                        #if !defined(ALLOW_BAR_NOTATION)

                                                        if (val > 3999L)

                                                        return NULL;

                                                        #endif

                                                        do

                                                            {

                                                            while (val >= RomanConvert[place].PostValue)

                                                                {

                                                                posn += sprintf(&buf[posn], "%s",

                                                                RomanConvert[place].PostLetter);

                                                                val -= RomanConvert[place].PostValue;

                                                                if (posn >= buflen)

                                                                return NULL;

                                                            }

                                                            if (PrefixesAreOK)

                                                                {

                                                                if (val >= RomanConvert[place].PreValue)

                                                                    {

                                                                    posn += sprintf(&buf[posn], "%s",

                                                                    RomanConvert[place].PreLetter);

                                                                    val -= RomanConvert[place].PreValue;

                                                                    if (posn >= buflen)

                                                                    return NULL;

                                                                }

                                                            }

                                                            place++;

                                                        } while (val > 0);

                                                        return buf;

                                                    }

                                                    #ifdef TEST

                                                    #include 

                                                    int main(int argc, char *argv[])

                                                        {

                                                        long value;

                                                        char buf[128];

                                                        while (--argc)

                                                            {

                                                            value = atol(*(++argv));

                                                            printf("\n%ld = %s\n", value, long2roman(value, buf, 128));

                                                        }

                                                        return 0;

                                                    }

                                                    #endif /* TEST */