Initial import from FreeBSD RELENG_4:
[games.git] / games / number / number.c
1 /*
2  * Copyright (c) 1988, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993, 1994\n\
37         The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)number.c    8.3 (Berkeley) 5/4/95";
43 #endif
44 static const char rcsid[] =
45  "$FreeBSD: src/games/number/number.c,v 1.12 1999/12/12 03:22:35 billf Exp $";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49
50 #include <ctype.h>
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #define MAXNUM          65              /* Biggest number we handle. */
58
59 static const char       *name1[] = {
60         "",             "one",          "two",          "three",
61         "four",         "five",         "six",          "seven",
62         "eight",        "nine",         "ten",          "eleven",
63         "twelve",       "thirteen",     "fourteen",     "fifteen",
64         "sixteen",      "seventeen",    "eighteen",     "nineteen",
65 },
66                 *name2[] = {
67         "",             "ten",          "twenty",       "thirty",
68         "forty",        "fifty",        "sixty",        "seventy",
69         "eighty",       "ninety",
70 },
71                 *name3[] = {
72         "hundred",      "thousand",     "million",      "billion",
73         "trillion",     "quadrillion",  "quintillion",  "sextillion",
74         "septillion",   "octillion",    "nonillion",    "decillion",
75         "undecillion",  "duodecillion", "tredecillion", "quattuordecillion",
76         "quindecillion",                "sexdecillion",
77         "septendecillion",              "octodecillion",
78         "novemdecillion",               "vigintillion",
79 };
80
81 void    convert __P((char *));
82 int     number __P((char *, int));
83 void    pfract __P((int));
84 void    toobig __P((void));
85 int     unit __P((int, char *));
86 void    usage __P((void));
87
88 int lflag;
89
90 int
91 main(argc, argv)
92         int argc;
93         char *argv[];
94 {
95         int ch, first;
96         char line[256];
97
98         lflag = 0;
99         while ((ch = getopt(argc, argv, "l")) != -1)
100                 switch (ch) {
101                 case 'l':
102                         lflag = 1;
103                         break;
104                 case '?':
105                 default:
106                         usage();
107                 }
108         argc -= optind;
109         argv += optind;
110
111         if (*argv == NULL)
112                 for (first = 1;
113                     fgets(line, sizeof(line), stdin) != NULL; first = 0) {
114                         if (strchr(line, '\n') == NULL)
115                                 errx(1, "line too long.");
116                         if (!first)
117                                 (void)printf("...\n");
118                         convert(line);
119                 }
120         else
121                 for (first = 1; *argv != NULL; first = 0, ++argv) {
122                         if (!first)
123                                 (void)printf("...\n");
124                         convert(*argv);
125                 }
126         exit(0);
127 }
128
129 void
130 convert(line)
131         char *line;
132 {
133         int flen, len, rval;
134         char *p, *fraction;
135
136         flen = NULL;
137         fraction = NULL;
138         for (p = line; *p != '\0' && *p != '\n'; ++p) {
139                 if (isblank(*p)) {
140                         if (p == line) {
141                                 ++line;
142                                 continue;
143                         }
144                         goto badnum;
145                 }
146                 if (isdigit(*p))
147                         continue;
148                 switch (*p) {
149                 case '.':
150                         if (fraction != NULL)
151                                 goto badnum;
152                         fraction = p + 1;
153                         *p = '\0';
154                         break;
155                 case '-':
156                         if (p == line)
157                                 break;
158                         /* FALLTHROUGH */
159                 default:
160 badnum:                 errx(1, "illegal number: %s", line);
161                         break;
162                 }
163         }
164         *p = '\0';
165
166         if ((len = strlen(line)) > MAXNUM ||
167             (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
168                 errx(1, "number too large, max %d digits.", MAXNUM);
169
170         if (*line == '-') {
171                 (void)printf("minus%s", lflag ? " " : "\n");
172                 ++line;
173                 --len;
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         int len;
201         char *p;
202 {
203         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         char *p;
237         int len;
238 {
239         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 }