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