Home | History | Annotate | Line # | Download | only in number
number.c revision 1.6
      1 /*	$NetBSD: number.c,v 1.6 1998/09/13 15:24:57 hubertf 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: number.c,v 1.6 1998/09/13 15:24:57 hubertf 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	main __P((int, char *[]));
     85 int	number __P((char *, int));
     86 void	pfract __P((int));
     87 void	toobig __P((void));
     88 int	unit __P((int, char *));
     89 void	usage __P((void)) __attribute__((__noreturn__));
     90 
     91 int lflag;
     92 
     93 int
     94 main(argc, argv)
     95 	int argc;
     96 	char *argv[];
     97 {
     98 	int ch, first;
     99 	char line[256];
    100 
    101 	lflag = 0;
    102 	while ((ch = getopt(argc, argv, "l")) != -1)
    103 		switch (ch) {
    104 		case 'l':
    105 			lflag = 1;
    106 			break;
    107 		case '?':
    108 		default:
    109 			usage();
    110 		}
    111 	argc -= optind;
    112 	argv += optind;
    113 
    114 	if (*argv == NULL)
    115 		for (first = 1;
    116 		    fgets(line, sizeof(line), stdin) != NULL; first = 0) {
    117 			if (strchr(line, '\n') == NULL)
    118 				errx(1, "line too long.");
    119 			if (!first)
    120 				(void)printf("...\n");
    121 			convert(line);
    122 		}
    123 	else
    124 		for (first = 1; *argv != NULL; first = 0, ++argv) {
    125 			if (!first)
    126 				(void)printf("...\n");
    127 			convert(*argv);
    128 		}
    129 	exit(0);
    130 }
    131 
    132 void
    133 convert(line)
    134 	char *line;
    135 {
    136 	int flen, len, rval;
    137 	char *p, *fraction;
    138 
    139 	flen = 0;
    140 	fraction = NULL;
    141 	for (p = line; *p != '\0' && *p != '\n'; ++p) {
    142 		if (isblank(*p)) {
    143 			if (p == line) {
    144 				++line;
    145 				continue;
    146 			}
    147 			goto badnum;
    148 		}
    149 		if (isdigit(*p))
    150 			continue;
    151 		switch (*p) {
    152 		case '.':
    153 			if (fraction != NULL)
    154 				goto badnum;
    155 			fraction = p + 1;
    156 			*p = '\0';
    157 			break;
    158 		case '-':
    159 			if (p == line)
    160 				break;
    161 			/* FALLTHROUGH */
    162 		default:
    163 badnum:			errx(1, "illegal number: %s", line);
    164 			break;
    165 		}
    166 	}
    167 	*p = '\0';
    168 
    169 	if ((len = strlen(line)) > MAXNUM ||
    170 	    (fraction != NULL && (flen = strlen(fraction)) > MAXNUM))
    171 		errx(1, "number too large, max %d digits.", MAXNUM);
    172 
    173 	if (*line == '-') {
    174 		(void)printf("minus%s", lflag ? " " : "\n");
    175 		++line;
    176 		--len;
    177 	}
    178 
    179 	rval = len > 0 ? unit(len, line) : 0;
    180 	if (fraction != NULL && flen != 0)
    181 		for (p = fraction; *p != '\0'; ++p)
    182 			if (*p != '0') {
    183 				if (rval)
    184 					(void)printf("%sand%s",
    185 					    lflag ? " " : "",
    186 					    lflag ? " " : "\n");
    187 				if (unit(flen, fraction)) {
    188 					if (lflag)
    189 						(void)printf(" ");
    190 					pfract(flen);
    191 					rval = 1;
    192 				}
    193 				break;
    194 			}
    195 	if (!rval)
    196 		(void)printf("zero%s", lflag ? "" : ".\n");
    197 	if (lflag)
    198 		(void)printf("\n");
    199 }
    200 
    201 int
    202 unit(len, p)
    203 	int len;
    204 	char *p;
    205 {
    206 	int off, rval;
    207 
    208 	rval = 0;
    209 	if (len > 3) {
    210 		if (len % 3) {
    211 			off = len % 3;
    212 			len -= off;
    213 			if (number(p, off)) {
    214 				rval = 1;
    215 				(void)printf(" %s%s",
    216 				    name3[len / 3], lflag ? " " : ".\n");
    217 			}
    218 			p += off;
    219 		}
    220 		for (; len > 3; p += 3) {
    221 			len -= 3;
    222 			if (number(p, 3)) {
    223 				rval = 1;
    224 				(void)printf(" %s%s",
    225 				    name3[len / 3], lflag ? " " : ".\n");
    226 			}
    227 		}
    228 	}
    229 	if (number(p, len)) {
    230 		if (!lflag)
    231 			(void)printf(".\n");
    232 		rval = 1;
    233 	}
    234 	return (rval);
    235 }
    236 
    237 int
    238 number(p, len)
    239 	char *p;
    240 	int len;
    241 {
    242 	int val, rval;
    243 
    244 	rval = 0;
    245 	switch (len) {
    246 	case 3:
    247 		if (*p != '0') {
    248 			rval = 1;
    249 			(void)printf("%s hundred", name1[*p - '0']);
    250 		}
    251 		++p;
    252 		/* FALLTHROUGH */
    253 	case 2:
    254 		val = (p[1] - '0') + (p[0] - '0') * 10;
    255 		if (val) {
    256 			if (rval)
    257 				(void)printf(" ");
    258 			if (val < 20)
    259 				(void)printf("%s", name1[val]);
    260 			else {
    261 				(void)printf("%s", name2[val / 10]);
    262 				if (val % 10)
    263 					(void)printf("-%s", name1[val % 10]);
    264 			}
    265 			rval = 1;
    266 		}
    267 		break;
    268 	case 1:
    269 		if (*p != '0') {
    270 			rval = 1;
    271 			(void)printf("%s", name1[*p - '0']);
    272 		}
    273 	}
    274 	return (rval);
    275 }
    276 
    277 void
    278 pfract(len)
    279 	int len;
    280 {
    281 	static char *pref[] = { "", "ten-", "hundred-" };
    282 
    283 	switch(len) {
    284 	case 1:
    285 		(void)printf("tenths.\n");
    286 		break;
    287 	case 2:
    288 		(void)printf("hundredths.\n");
    289 		break;
    290 	default:
    291 		(void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
    292 		break;
    293 	}
    294 }
    295 
    296 void
    297 usage()
    298 {
    299 	(void)fprintf(stderr, "usage: number [# ...]\n");
    300 	exit(1);
    301 }
    302