Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: $Revision: 1.6.2.16 $; site ucbcad.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!ucbcad!faustus From: faustus@ucbcad.UUCP Newsgroups: net.sources Subject: Re: Orphaned Response Message-ID: <-20300@ucbcad.UUCP> Date: Thu, 25-Apr-85 21:04:00 EDT Article-I.D.: ucbcad.-20300 Posted: Thu Apr 25 21:04:00 1985 Date-Received: Thu, 16-May-85 22:09:57 EDT References: <140@ucdavis.UUCP> Lines: 80 Nf-ID: #R:ucdavis:-14000:ucbcad:-20300:177600:1998 Nf-From: ucbcad!faustus Apr 26 00:04:00 1985 > Do you have a program that either converts numbers to roman numerals > or (especially!) converts roman numerals to numbers. Here is one: actually this prints the date in Roman numerals, but the routine prom() does the real stuff... Wayne /* RCS Info: $Revision: 1.1 $ on $Date: 85/04/18 17:44:51 $ * $Source: /cad1/faustus/src/misc/RCS/rdate.c,v $ * Copyright (c) 1985 Wayne A. Christopher, U. C. Berkeley CAD Group * * Print the date in roman numerals. */ main() { char buf[64], s[3][16], nn[5][16], *prom(); int q, p[2], n[5]; pipe(p); if(fork() == 0) { dup2(p[1], 1); execl("/bin/date", "date", 0); } q = read(p[0], buf, 64); buf[q] = '\0'; sscanf(buf, " %s %s %d %d:%d:%d %s %d ", s[0], s[1], &n[0], &n[1], &n[2], &n[3], s[2], &n[4]); for(q = 0; q < 5; q++) strcpy(nn[q], prom(n[q])); printf("%s %s %s %s:%s:%s %s %s\n", s[0], s[1], nn[0], nn[1], nn[2], nn[3], s[2], nn[4]); exit(0); } /* Render number in Roman Numerals. Should be unsigned... Returns * pointer to the rendering. Problems with static data, etc. */ char * prom(num) register unsigned int num; { register int pos = 0; register int *i, k; register char *j; static char rbuf[64]; /* Bad assumption? */ static int ndigits[] = { 1000, 500, 100, 50, 10, 5, 1, 0 } ; static char ddigits[] = { 'M', 'D', 'C', 'L', 'X', 'V', 'I', 'Z' } ; if(num == 0) return("Z"); /* Well, what do we do? */ while(num > 0) { for((i = ndigits, j = ddigits); *i; (i++, j++)) { if(num >= *i) { rbuf[pos++] = *j; num -= *i; break; } /* If you are on an even index in the array, the * digit to use for filling in (e.g. IX = 9) is * *i / 10, otherwise use *i / 5 (to make sure * that it is a power of 10). */ k = (!((ndigits - i) % 2) ? (*i / 10) : (*i / 5)); if(num >= (*i - k)) { rbuf[pos++] = *(j + (((ndigits - i) % 2) ? 1 : 2)); rbuf[pos++] = *j; num -= (*i - k); break; } } } rbuf[pos] = '\0'; return(rbuf); }