(Re LBL) a routine to convert to Roman numerals

Chris Torek chris at umcp-cs.UUCP
Sat Jul 6 00:26:50 AEST 1985


I noticed while scanning through the source to lbl that it converts
things to Roman.  Here is a rather clever way to do the same, due
to Knuth, converted to cryptic C code.

/*
 * Print a roman numeral representation of the positive integer 'n'
 * (negative numbers produce no output).
 */
pr_roman (n)
register int n;
{
    register int    u,
                    v;
    register char  *p,
                   *q;

    p = "m\2d\5c\2l\5x\2v\5i";
    v = 1000;
    for (;;) {
	while (n >= v)
	    putchar (*p), n -= v;
	if (n <= 0)
	    return;
	q = p + 1;
	u = v / *q;
	if (*q == 2)
	    u /= *(q += 2);
	if (n + u >= v) {
	    putchar (*++q);
	    n += u;
	}
	else {
	    p++;
	    v /= *p++;
	}
    }
}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.sources.unix mailing list