Here is the lhdb.c source file. Clip and save.


/*
 * lhdb - long-hand division, binary.  Converts binary #
 *	generated by lhmb to decimal.
 *
 *	Useage: lhdb < binary_text > decimal_text
 *
 *	Scott Coburn June 1992
 */

#include 
#include "lhmb.h"

/*
 * defines
 */

/*
 * static data
 */

static long int i_min = 0;		/* start index for processing a1[] */
static long int i_max = 0;		/* max index into a1[]		*/

static ULINT a1[2*SIZE];		/* scratch array for binary #	*/
static ULINT a2[2*SIZE];		/* to store final # (+ some)	*/

/*
 * too_big - check to see if the remaining binary # is greater
 *	or less than DIVISOR.  Return TRUE if greater or equal.
 */

static int too_big()
{
    long int i;

    for ( i = i_min ; i <= i_max ; i++)	/* find 1st non-zero value...	*/
	if (a1[i] != 0) break;

    if ( i > i_max)
	i_min = i_max;			/* last one...			*/
    else
	i_min = i;			/* new starting point...	*/

    if ((i_min == i_max) && (a1[i_max] < DIVISOR))
	return(FALSE);

    return(TRUE);
}

/*
 * main
 */

main()
{
    ULINT t1, t2;			/* scratch division variables	*/
    ULINT bin;				/* value read from data file	*/

    long int i;				/* index for array a1[]		*/
    long int i_a2;			/* index into a2[] for next char*/
    int leading;			/* TRUE if bin is leading 0	*/
    int ret;

    i_a2 = 0;
    leading = TRUE;
    while ((ret = scanf(HFORMAT2,&bin)) != EOF)
	{
	if (bin != 0) leading = FALSE;	/* found start of binary #	*/
	if (!leading) a1[i_max++] = bin;/* read binary # into array	*/
	}
    i_max = i_max - 1;			/* set maximum value for index	*/
    fprintf(stderr,"lines read in: %08d\n",i_max+1);

	/* too_big() sets i_min...				*/

    while ( too_big() )			/* keep dividing until < DIVISOR */
	{
	t1 = 0;
	for (i = i_min ; i <= i_max ; i++)/* divide whole # by DIVISOR	*/
	    {
	    t1 = (t1 << SHIFT) + a1[i];
	    t2 = t1 / DIVISOR;
	    t1 = t1 - (t2 * DIVISOR);
	    a1[i] = t2;
	    }
	fprintf(stderr,"%08d %08lld\n",i_a2, t1);
	fflush(stderr);
	a2[i_a2++] = t1;		/* these will be decimal digits	*/
	}				/*  to previous ones...		*/

    a2[i_a2] = a1[i_max];		/* ...last few digits...	*/

	/* print decimal digits in a2 in 'reverse' order...	*/

    for (i = i_a2 ; i >= 0 ; i--)
	printf(DFORMAT,a2[i]);
}