]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - number/number.c
merge with Lite, new RCS id conventions, etc.
[bsdgames-darwin.git] / number / number.c
1 /* $NetBSD: number.c,v 1.3 1995/03/23 08:35:30 cgd 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 #ifndef lint
37 static char copyright[] =
38 "@(#) 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.2 (Berkeley) 3/31/94";
45 #else
46 static char rcsid[] = "$NetBSD: number.c,v 1.3 1995/03/23 08:35:30 cgd Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51
52 #include <ctype.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <err.h>
57
58 #define MAXNUM 65 /* Biggest number we handle. */
59
60 static char *name1[] = {
61 "", "one", "two", "three",
62 "four", "five", "six", "seven",
63 "eight", "nine", "ten", "eleven",
64 "twelve", "thirteen", "fourteen", "fifteen",
65 "sixteen", "seventeen", "eighteen", "nineteen",
66 },
67 *name2[] = {
68 "", "ten", "twenty", "thirty",
69 "forty", "fifty", "sixty", "seventy",
70 "eighty", "ninety",
71 },
72 *name3[] = {
73 "hundred", "thousand", "million", "billion",
74 "trillion", "quadrillion", "quintillion", "sextillion",
75 "septillion", "octillion", "nonillion", "decillion",
76 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
77 "quindecillion", "sexdecillion",
78 "septendecillion", "octodecillion",
79 "novemdecillion", "vigintillion",
80 };
81
82 void convert __P((char *));
83 int number __P((char *, int));
84 void pfract __P((int));
85 void toobig __P((void));
86 int unit __P((int, char *));
87 void usage __P((void));
88
89 int lflag;
90
91 int
92 main(argc, argv)
93 int argc;
94 char *argv[];
95 {
96 int ch, first;
97 char line[256];
98
99 lflag = 0;
100 while ((ch = getopt(argc, argv, "l")) != EOF)
101 switch (ch) {
102 case 'l':
103 lflag = 1;
104 break;
105 case '?':
106 default:
107 usage();
108 }
109 argc -= optind;
110 argv += optind;
111
112 if (*argv == NULL)
113 for (first = 1;
114 fgets(line, sizeof(line), stdin) != NULL; first = 0) {
115 if (strchr(line, '\n') == NULL)
116 errx(1, "line too long.");
117 if (!first)
118 (void)printf("...\n");
119 convert(line);
120 }
121 else
122 for (first = 1; *argv != NULL; first = 0, ++argv) {
123 if (!first)
124 (void)printf("...\n");
125 convert(*argv);
126 }
127 exit(0);
128 }
129
130 void
131 convert(line)
132 char *line;
133 {
134 register flen, len, rval;
135 register char *p, *fraction;
136
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 }
174
175 rval = len > 0 ? unit(len, line) : 0;
176 if (fraction != NULL && flen != 0)
177 for (p = fraction; *p != '\0'; ++p)
178 if (*p != '0') {
179 if (rval)
180 (void)printf("%sand%s",
181 lflag ? " " : "",
182 lflag ? " " : "\n");
183 if (unit(flen, fraction)) {
184 if (lflag)
185 (void)printf(" ");
186 pfract(flen);
187 rval = 1;
188 }
189 break;
190 }
191 if (!rval)
192 (void)printf("zero%s", lflag ? "" : ".\n");
193 if (lflag)
194 (void)printf("\n");
195 }
196
197 int
198 unit(len, p)
199 register int len;
200 register char *p;
201 {
202 register int off, rval;
203
204 rval = 0;
205 if (len > 3) {
206 if (len % 3) {
207 off = len % 3;
208 len -= off;
209 if (number(p, off)) {
210 rval = 1;
211 (void)printf(" %s%s",
212 name3[len / 3], lflag ? " " : ".\n");
213 }
214 p += off;
215 }
216 for (; len > 3; p += 3) {
217 len -= 3;
218 if (number(p, 3)) {
219 rval = 1;
220 (void)printf(" %s%s",
221 name3[len / 3], lflag ? " " : ".\n");
222 }
223 }
224 }
225 if (number(p, len)) {
226 if (!lflag)
227 (void)printf(".\n");
228 rval = 1;
229 }
230 return (rval);
231 }
232
233 int
234 number(p, len)
235 register char *p;
236 int len;
237 {
238 register int val, rval;
239
240 rval = 0;
241 switch (len) {
242 case 3:
243 if (*p != '0') {
244 rval = 1;
245 (void)printf("%s hundred", name1[*p - '0']);
246 }
247 ++p;
248 /* FALLTHROUGH */
249 case 2:
250 val = (p[1] - '0') + (p[0] - '0') * 10;
251 if (val) {
252 if (rval)
253 (void)printf(" ");
254 if (val < 20)
255 (void)printf("%s", name1[val]);
256 else {
257 (void)printf("%s", name2[val / 10]);
258 if (val % 10)
259 (void)printf("-%s", name1[val % 10]);
260 }
261 rval = 1;
262 }
263 break;
264 case 1:
265 if (*p != '0') {
266 rval = 1;
267 (void)printf("%s", name1[*p - '0']);
268 }
269 }
270 return (rval);
271 }
272
273 void
274 pfract(len)
275 int len;
276 {
277 static char *pref[] = { "", "ten-", "hundred-" };
278
279 switch(len) {
280 case 1:
281 (void)printf("tenths.\n");
282 break;
283 case 2:
284 (void)printf("hundredths.\n");
285 break;
286 default:
287 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
288 break;
289 }
290 }
291
292 void
293 usage()
294 {
295 (void)fprintf(stderr, "usage: number [# ...]\n");
296 exit(1);
297 }