2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)number.c 8.3 (Berkeley) 5/4/95
31 * $FreeBSD: src/games/number/number.c,v 1.12 1999/12/12 03:22:35 billf Exp $
34 #include <sys/types.h>
43 #define MAXNUM 65 /* Biggest number we handle. */
45 static const char *name1[] = {
46 "", "one", "two", "three",
47 "four", "five", "six", "seven",
48 "eight", "nine", "ten", "eleven",
49 "twelve", "thirteen", "fourteen", "fifteen",
50 "sixteen", "seventeen", "eighteen", "nineteen",
53 "", "ten", "twenty", "thirty",
54 "forty", "fifty", "sixty", "seventy",
58 "hundred", "thousand", "million", "billion",
59 "trillion", "quadrillion", "quintillion", "sextillion",
60 "septillion", "octillion", "nonillion", "decillion",
61 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
62 "quindecillion", "sexdecillion",
63 "septendecillion", "octodecillion",
64 "novemdecillion", "vigintillion",
67 static void convert(char *);
68 static int number(char *, int);
69 static void pfract(int);
70 static int unit(int, char *);
71 static void usage(void) __dead2;
76 main(int argc, char **argv)
82 while ((ch = getopt(argc, argv, "l")) != -1)
96 fgets(line, sizeof(line), stdin) != NULL; first = 0) {
97 if (strchr(line, '\n') == NULL)
98 errx(1, "line too long.");
104 for (first = 1; *argv != NULL; first = 0, ++argv) {
120 for (p = line; *p != '\0' && *p != '\n'; ++p) {
132 if (fraction != NULL)
142 badnum: errx(1, "illegal number: %s", line);
148 if ((len = strlen(line)) > MAXNUM ||
149 (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
150 errx(1, "number too large, max %d digits.", MAXNUM);
153 printf("minus%s", lflag ? " " : "\n");
158 rval = len > 0 ? unit(len, line) : 0;
159 if (fraction != NULL && flen != 0)
160 for (p = fraction; *p != '\0'; ++p)
166 if (unit(flen, fraction)) {
175 printf("zero%s", lflag ? "" : ".\n");
181 unit(int len, char *p)
190 if (number(p, off)) {
193 name3[len / 3], lflag ? " " : ".\n");
197 for (; len > 3; p += 3) {
202 name3[len / 3], lflag ? " " : ".\n");
206 if (number(p, len)) {
215 number(char *p, int len)
224 printf("%s hundred", name1[*p - '0']);
229 val = (p[1] - '0') + (p[0] - '0') * 10;
234 printf("%s", name1[val]);
236 printf("%s", name2[val / 10]);
238 printf("-%s", name1[val % 10]);
246 printf("%s", name1[*p - '0']);
255 static const char *pref[] = { "", "ten-", "hundred-" };
262 printf("hundredths.\n");
265 printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
273 fprintf(stderr, "usage: number [# ...]\n");