]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - games/number/number.c
This commit was generated by cvs2svn to compensate for changes in r38980,
[FreeBSD/FreeBSD.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 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 static char sccsid[] = "@(#)number.c    8.3 (Berkeley) 5/4/95";
42 #endif /* not lint */
43
44 #include <sys/types.h>
45
46 #include <ctype.h>
47 #include <err.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #define MAXNUM          65              /* Biggest number we handle. */
54
55 static char     *name1[] = {
56         "",             "one",          "two",          "three",
57         "four",         "five",         "six",          "seven",
58         "eight",        "nine",         "ten",          "eleven",
59         "twelve",       "thirteen",     "fourteen",     "fifteen",
60         "sixteen",      "seventeen",    "eighteen",     "nineteen",
61 },
62                 *name2[] = {
63         "",             "ten",          "twenty",       "thirty",
64         "forty",        "fifty",        "sixty",        "seventy",
65         "eighty",       "ninety",
66 },
67                 *name3[] = {
68         "hundred",      "thousand",     "million",      "billion",
69         "trillion",     "quadrillion",  "quintillion",  "sextillion",
70         "septillion",   "octillion",    "nonillion",    "decillion",
71         "undecillion",  "duodecillion", "tredecillion", "quattuordecillion",
72         "quindecillion",                "sexdecillion",
73         "septendecillion",              "octodecillion",
74         "novemdecillion",               "vigintillion",
75 };
76
77 void    convert __P((char *));
78 int     number __P((char *, int));
79 void    pfract __P((int));
80 void    toobig __P((void));
81 int     unit __P((int, char *));
82 void    usage __P((void));
83
84 int lflag;
85
86 int
87 main(argc, argv)
88         int argc;
89         char *argv[];
90 {
91         int ch, first;
92         char line[256];
93
94         lflag = 0;
95         while ((ch = getopt(argc, argv, "l")) != -1)
96                 switch (ch) {
97                 case 'l':
98                         lflag = 1;
99                         break;
100                 case '?':
101                 default:
102                         usage();
103                 }
104         argc -= optind;
105         argv += optind;
106
107         if (*argv == NULL)
108                 for (first = 1;
109                     fgets(line, sizeof(line), stdin) != NULL; first = 0) {
110                         if (strchr(line, '\n') == NULL)
111                                 errx(1, "line too long.");
112                         if (!first)
113                                 (void)printf("...\n");
114                         convert(line);
115                 }
116         else
117                 for (first = 1; *argv != NULL; first = 0, ++argv) {
118                         if (!first)
119                                 (void)printf("...\n");
120                         convert(*argv);
121                 }
122         exit(0);
123 }
124
125 void
126 convert(line)
127         char *line;
128 {
129         register flen, len, rval;
130         register char *p, *fraction;
131
132         fraction = NULL;
133         for (p = line; *p != '\0' && *p != '\n'; ++p) {
134                 if (isblank(*p)) {
135                         if (p == line) {
136                                 ++line;
137                                 continue;
138                         }
139                         goto badnum;
140                 }
141                 if (isdigit(*p))
142                         continue;
143                 switch (*p) {
144                 case '.':
145                         if (fraction != NULL)
146                                 goto badnum;
147                         fraction = p + 1;
148                         *p = '\0';
149                         break;
150                 case '-':
151                         if (p == line)
152                                 break;
153                         /* FALLTHROUGH */
154                 default:
155 badnum:                 errx(1, "illegal number: %s", line);
156                         break;
157                 }
158         }
159         *p = '\0';
160
161         if ((len = strlen(line)) > MAXNUM ||
162             fraction != NULL && (flen = strlen(fraction)) > MAXNUM)
163                 errx(1, "number too large, max %d digits.", MAXNUM);
164
165         if (*line == '-') {
166                 (void)printf("minus%s", lflag ? " " : "\n");
167                 ++line;
168                 --len;
169         }
170
171         rval = len > 0 ? unit(len, line) : 0;
172         if (fraction != NULL && flen != 0)
173                 for (p = fraction; *p != '\0'; ++p)
174                         if (*p != '0') {
175                                 if (rval)
176                                         (void)printf("%sand%s",
177                                             lflag ? " " : "",
178                                             lflag ? " " : "\n");
179                                 if (unit(flen, fraction)) {
180                                         if (lflag)
181                                                 (void)printf(" ");
182                                         pfract(flen);
183                                         rval = 1;
184                                 }
185                                 break;
186                         }
187         if (!rval)
188                 (void)printf("zero%s", lflag ? "" : ".\n");
189         if (lflag)
190                 (void)printf("\n");
191 }
192
193 int
194 unit(len, p)
195         register int len;
196         register char *p;
197 {
198         register int off, rval;
199
200         rval = 0;
201         if (len > 3) {
202                 if (len % 3) {
203                         off = len % 3;
204                         len -= off;
205                         if (number(p, off)) {
206                                 rval = 1;
207                                 (void)printf(" %s%s",
208                                     name3[len / 3], lflag ? " " : ".\n");
209                         }
210                         p += off;
211                 }
212                 for (; len > 3; p += 3) {
213                         len -= 3;
214                         if (number(p, 3)) {
215                                 rval = 1;
216                                 (void)printf(" %s%s",
217                                     name3[len / 3], lflag ? " " : ".\n");
218                         }
219                 }
220         }
221         if (number(p, len)) {
222                 if (!lflag)
223                         (void)printf(".\n");
224                 rval = 1;
225         }
226         return (rval);
227 }
228
229 int
230 number(p, len)
231         register char *p;
232         int len;
233 {
234         register int val, rval;
235
236         rval = 0;
237         switch (len) {
238         case 3:
239                 if (*p != '0') {
240                         rval = 1;
241                         (void)printf("%s hundred", name1[*p - '0']);
242                 }
243                 ++p;
244                 /* FALLTHROUGH */
245         case 2:
246                 val = (p[1] - '0') + (p[0] - '0') * 10;
247                 if (val) {
248                         if (rval)
249                                 (void)printf(" ");
250                         if (val < 20)
251                                 (void)printf("%s", name1[val]);
252                         else {
253                                 (void)printf("%s", name2[val / 10]);
254                                 if (val % 10)
255                                         (void)printf("-%s", name1[val % 10]);
256                         }
257                         rval = 1;
258                 }
259                 break;
260         case 1:
261                 if (*p != '0') {
262                         rval = 1;
263                         (void)printf("%s", name1[*p - '0']);
264                 }
265         }
266         return (rval);
267 }
268
269 void
270 pfract(len)
271         int len;
272 {
273         static char *pref[] = { "", "ten-", "hundred-" };
274
275         switch(len) {
276         case 1:
277                 (void)printf("tenths.\n");
278                 break;
279         case 2:
280                 (void)printf("hundredths.\n");
281                 break;
282         default:
283                 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
284                 break;
285         }
286 }
287
288 void
289 usage()
290 {
291         (void)fprintf(stderr, "usage: number [# ...]\n");
292         exit(1);
293 }