number.c revision 1.4       1 /*	$NetBSD: number.c,v 1.4 1997/01/07 12:16:57 tls Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #ifndef lint
     37 static char copyright[] =
     38 "@(#) Copyright (c) 1988, 1993, 1994\n\
     39 	The Regents of the University of California.  All rights reserved.\n";
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)number.c	8.3 (Berkeley) 5/4/95";
     45 #else
     46 static char rcsid[] = "$NetBSD: number.c,v 1.4 1997/01/07 12:16:57 tls Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 
     52 #include <ctype.h>
     53 #include <err.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 
     59 #define	MAXNUM		65		/* Biggest number we handle. */
     60 
     61 static char	*name1[] = {
     62 	"",		"one",		"two",		"three",
     63 	"four",		"five",		"six",		"seven",
     64 	"eight",	"nine",		"ten",		"eleven",
     65 	"twelve",	"thirteen",	"fourteen",	"fifteen",
     66 	"sixteen",	"seventeen",	"eighteen",	"nineteen",
     67 },
     68 		*name2[] = {
     69 	"",		"ten",		"twenty",	"thirty",
     70 	"forty",	"fifty",	"sixty",	"seventy",
     71 	"eighty",	"ninety",
     72 },
     73 		*name3[] = {
     74 	"hundred",	"thousand",	"million",	"billion",
     75 	"trillion",	"quadrillion",	"quintillion",	"sextillion",
     76 	"septillion",	"octillion",	"nonillion",	"decillion",
     77 	"undecillion",	"duodecillion",	"tredecillion",	"quattuordecillion",
     78 	"quindecillion",		"sexdecillion",
     79 	"septendecillion",		"octodecillion",
     80 	"novemdecillion",		"vigintillion",
     81 };
     82 
     83 void	convert __P((char *));
     84 int	number __P((char *, int));
     85 void	pfract __P((int));
     86 void	toobig __P((void));
     87 int	unit __P((int, char *));
     88 void	usage __P((void));
     89 
     90 int lflag;
     91 
     92 int
     93 main(argc, argv)
     94 	int argc;
     95 	char *argv[];
     96 {
     97 	int ch, first;
     98 	char line[256];
     99 
    100 	lflag = 0;
    101 	while ((ch = getopt(argc, argv, "l")) != EOF)
    102 		switch (ch) {
    103 		case 'l':
    104 			lflag = 1;
    105 			break;
    106 		case '?':
    107 		default:
    108 			usage();
    109 		}
    110 	argc -= optind;
    111 	argv += optind;
    112 
    113 	if (*argv == NULL)
    114 		for (first = 1;
    115 		    fgets(line, sizeof(line), stdin) != NULL; first = 0) {
    116 			if (strchr(line, '\n') == NULL)
    117 				errx(1, "line too long.");
    118 			if (!first)
    119 				(void)printf("...\n");
    120 			convert(line);
    121 		}
    122 	else
    123 		for (first = 1; *argv != NULL; first = 0, ++argv) {
    124 			if (!first)
    125 				(void)printf("...\n");
    126 			convert(*argv);
    127 		}
    128 	exit(0);
    129 }
    130 
    131 void
    132 convert(line)
    133 	char *line;
    134 {
    135 	register flen, len, rval;
    136 	register char *p, *fraction;
    137 
    138 	fraction = NULL;
    139 	for (p = line; *p != '\0' && *p != '\n'; ++p) {
    140 		if (isblank(*p)) {
    141 			if (p == line) {
    142 				++line;
    143 				continue;
    144 			}
    145 			goto badnum;
    146 		}
    147 		if (isdigit(*p))
    148 			continue;
    149 		switch (*p) {
    150 		case '.':
    151 			if (fraction != NULL)
    152 				goto badnum;
    153 			fraction = p + 1;
    154 			*p = '\0';
    155 			break;
    156 		case '-':
    157 			if (p == line)
    158 				break;
    159 			/* FALLTHROUGH */
    160 		default:
    161 badnum:			errx(1, "illegal number: %s", line);
    162 			break;
    163 		}
    164 	}
    165 	*p = '\0';
    166 
    167 	if ((len = strlen(line)) > MAXNUM ||
    168 	    fraction != NULL && (flen = strlen(fraction)) > MAXNUM)
    169 		errx(1, "number too large, max %d digits.", MAXNUM);
    170 
    171 	if (*line == '-') {
    172 		(void)printf("minus%s", lflag ? " " : "\n");
    173 		++line;
    174 	}
    175 
    176 	rval = len > 0 ? unit(len, line) : 0;
    177 	if (fraction != NULL && flen != 0)
    178 		for (p = fraction; *p != '\0'; ++p)
    179 			if (*p != '0') {
    180 				if (rval)
    181 					(void)printf("%sand%s",
    182 					    lflag ? " " : "",
    183 					    lflag ? " " : "\n");
    184 				if (unit(flen, fraction)) {
    185 					if (lflag)
    186 						(void)printf(" ");
    187 					pfract(flen);
    188 					rval = 1;
    189 				}
    190 				break;
    191 			}
    192 	if (!rval)
    193 		(void)printf("zero%s", lflag ? "" : ".\n");
    194 	if (lflag)
    195 		(void)printf("\n");
    196 }
    197 
    198 int
    199 unit(len, p)
    200 	register int len;
    201 	register char *p;
    202 {
    203 	register int off, rval;
    204 
    205 	rval = 0;
    206 	if (len > 3) {
    207 		if (len % 3) {
    208 			off = len % 3;
    209 			len -= off;
    210 			if (number(p, off)) {
    211 				rval = 1;
    212 				(void)printf(" %s%s",
    213 				    name3[len / 3], lflag ? " " : ".\n");
    214 			}
    215 			p += off;
    216 		}
    217 		for (; len > 3; p += 3) {
    218 			len -= 3;
    219 			if (number(p, 3)) {
    220 				rval = 1;
    221 				(void)printf(" %s%s",
    222 				    name3[len / 3], lflag ? " " : ".\n");
    223 			}
    224 		}
    225 	}
    226 	if (number(p, len)) {
    227 		if (!lflag)
    228 			(void)printf(".\n");
    229 		rval = 1;
    230 	}
    231 	return (rval);
    232 }
    233 
    234 int
    235 number(p, len)
    236 	register char *p;
    237 	int len;
    238 {
    239 	register int val, rval;
    240 
    241 	rval = 0;
    242 	switch (len) {
    243 	case 3:
    244 		if (*p != '0') {
    245 			rval = 1;
    246 			(void)printf("%s hundred", name1[*p - '0']);
    247 		}
    248 		++p;
    249 		/* FALLTHROUGH */
    250 	case 2:
    251 		val = (p[1] - '0') + (p[0] - '0') * 10;
    252 		if (val) {
    253 			if (rval)
    254 				(void)printf(" ");
    255 			if (val < 20)
    256 				(void)printf("%s", name1[val]);
    257 			else {
    258 				(void)printf("%s", name2[val / 10]);
    259 				if (val % 10)
    260 					(void)printf("-%s", name1[val % 10]);
    261 			}
    262 			rval = 1;
    263 		}
    264 		break;
    265 	case 1:
    266 		if (*p != '0') {
    267 			rval = 1;
    268 			(void)printf("%s", name1[*p - '0']);
    269 		}
    270 	}
    271 	return (rval);
    272 }
    273 
    274 void
    275 pfract(len)
    276 	int len;
    277 {
    278 	static char *pref[] = { "", "ten-", "hundred-" };
    279 
    280 	switch(len) {
    281 	case 1:
    282 		(void)printf("tenths.\n");
    283 		break;
    284 	case 2:
    285 		(void)printf("hundredths.\n");
    286 		break;
    287 	default:
    288 		(void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
    289 		break;
    290 	}
    291 }
    292 
    293 void
    294 usage()
    295 {
    296 	(void)fprintf(stderr, "usage: number [# ...]\n");
    297 	exit(1);
    298 }
    299