xref: /minix/games/number/number.c (revision 5f9253d4)
1 /*	$NetBSD: number.c,v 1.15 2012/06/19 05:46:09 dholland 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)number.c	8.3 (Berkeley) 5/4/95";
41 #else
42 __RCSID("$NetBSD: number.c,v 1.15 2012/06/19 05:46:09 dholland Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 
48 #include <ctype.h>
49 #include <err.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #define	MAXNUM		65		/* Biggest number we handle. */
56 
57 static const char	*const name1[] = {
58 	"",		"one",		"two",		"three",
59 	"four",		"five",		"six",		"seven",
60 	"eight",	"nine",		"ten",		"eleven",
61 	"twelve",	"thirteen",	"fourteen",	"fifteen",
62 	"sixteen",	"seventeen",	"eighteen",	"nineteen",
63 },
64 		*const name2[] = {
65 	"",		"ten",		"twenty",	"thirty",
66 	"forty",	"fifty",	"sixty",	"seventy",
67 	"eighty",	"ninety",
68 },
69 		*const name3[] = {
70 	"hundred",	"thousand",	"million",	"billion",
71 	"trillion",	"quadrillion",	"quintillion",	"sextillion",
72 	"septillion",	"octillion",	"nonillion",	"decillion",
73 	"undecillion",	"duodecillion",	"tredecillion",	"quattuordecillion",
74 	"quindecillion",		"sexdecillion",
75 	"septendecillion",		"octodecillion",
76 	"novemdecillion",		"vigintillion",
77 };
78 
79 int	main(int, char *[]);
80 static void convert(char *);
81 static int number(const char *, int);
82 static void pfract(int);
83 static int unit(int, const char *);
84 static void usage(void) __dead;
85 
86 static int lflag;
87 
88 int
89 main(int argc, 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(char *line)
127 {
128 	int flen, len, rval;
129 	char *p, *fraction;
130 
131 	flen = 0;
132 	fraction = NULL;
133 	for (p = line; *p != '\0' && *p != '\n'; ++p) {
134 		if (isblank((unsigned char)*p)) {
135 			if (p == line) {
136 				++line;
137 				continue;
138 			}
139 			goto badnum;
140 		}
141 		if (isdigit((unsigned char)*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(int len, const char *p)
195 {
196 	int off, rval;
197 
198 	rval = 0;
199 	if (len > 3) {
200 		if (len % 3) {
201 			off = len % 3;
202 			len -= off;
203 			if (number(p, off)) {
204 				rval = 1;
205 				(void)printf(" %s%s",
206 				    name3[len / 3], lflag ? " " : ".\n");
207 			}
208 			p += off;
209 		}
210 		for (; len > 3; p += 3) {
211 			len -= 3;
212 			if (number(p, 3)) {
213 				rval = 1;
214 				(void)printf(" %s%s",
215 				    name3[len / 3], lflag ? " " : ".\n");
216 			}
217 		}
218 	}
219 	if (number(p, len)) {
220 		if (!lflag)
221 			(void)printf(".\n");
222 		rval = 1;
223 	}
224 	return (rval);
225 }
226 
227 int
228 number(const char *p, 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 
265 void
266 pfract(int len)
267 {
268 	static const char *const pref[] = { "", "ten-", "hundred-" };
269 
270 	switch(len) {
271 	case 1:
272 		(void)printf("tenths.\n");
273 		break;
274 	case 2:
275 		(void)printf("hundredths.\n");
276 		break;
277 	default:
278 		(void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
279 		break;
280 	}
281 }
282 
283 void
284 usage(void)
285 {
286 	(void)fprintf(stderr, "usage: number [# ...]\n");
287 	exit(1);
288 }
289