xref: /dflybsd-src/games/number/number.c (revision fbdce4bfa22143251eb90d530696c0c855fdfb84)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1988, 1993, 1994 The Regents of the University of California.  All rights reserved.
30  * @(#)number.c	8.3 (Berkeley) 5/4/95
31  * $FreeBSD: src/games/number/number.c,v 1.12 1999/12/12 03:22:35 billf Exp $
32  */
33 
34 #include <sys/types.h>
35 
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #define	MAXNUM		65		/* Biggest number we handle. */
44 
45 static const char	*name1[] = {
46 	"",		"one",		"two",		"three",
47 	"four",		"five",		"six",		"seven",
48 	"eight",	"nine",		"ten",		"eleven",
49 	"twelve",	"thirteen",	"fourteen",	"fifteen",
50 	"sixteen",	"seventeen",	"eighteen",	"nineteen",
51 },
52 		*name2[] = {
53 	"",		"ten",		"twenty",	"thirty",
54 	"forty",	"fifty",	"sixty",	"seventy",
55 	"eighty",	"ninety",
56 },
57 		*name3[] = {
58 	"hundred",	"thousand",	"million",	"billion",
59 	"trillion",	"quadrillion",	"quintillion",	"sextillion",
60 	"septillion",	"octillion",	"nonillion",	"decillion",
61 	"undecillion",	"duodecillion",	"tredecillion",	"quattuordecillion",
62 	"quindecillion",		"sexdecillion",
63 	"septendecillion",		"octodecillion",
64 	"novemdecillion",		"vigintillion",
65 };
66 
67 void	convert (char *);
68 int	number (char *, int);
69 void	pfract (int);
70 void	toobig (void);
71 int	unit (int, char *);
72 void	usage (void);
73 
74 int lflag;
75 
76 int
77 main(int argc, char **argv)
78 {
79 	int ch, first;
80 	char line[256];
81 
82 	lflag = 0;
83 	while ((ch = getopt(argc, argv, "l")) != -1)
84 		switch (ch) {
85 		case 'l':
86 			lflag = 1;
87 			break;
88 		case '?':
89 		default:
90 			usage();
91 		}
92 	argc -= optind;
93 	argv += optind;
94 
95 	if (*argv == NULL)
96 		for (first = 1;
97 		    fgets(line, sizeof(line), stdin) != NULL; first = 0) {
98 			if (strchr(line, '\n') == NULL)
99 				errx(1, "line too long.");
100 			if (!first)
101 				printf("...\n");
102 			convert(line);
103 		}
104 	else
105 		for (first = 1; *argv != NULL; first = 0, ++argv) {
106 			if (!first)
107 				printf("...\n");
108 			convert(*argv);
109 		}
110 	exit(0);
111 }
112 
113 void
114 convert(char *line)
115 {
116 	int flen, len, rval;
117 	char *p, *fraction;
118 
119 	flen = 0;
120 	fraction = NULL;
121 	for (p = line; *p != '\0' && *p != '\n'; ++p) {
122 		if (isblank(*p)) {
123 			if (p == line) {
124 				++line;
125 				continue;
126 			}
127 			goto badnum;
128 		}
129 		if (isdigit(*p))
130 			continue;
131 		switch (*p) {
132 		case '.':
133 			if (fraction != NULL)
134 				goto badnum;
135 			fraction = p + 1;
136 			*p = '\0';
137 			break;
138 		case '-':
139 			if (p == line)
140 				break;
141 			/* FALLTHROUGH */
142 		default:
143 badnum:			errx(1, "illegal number: %s", line);
144 			break;
145 		}
146 	}
147 	*p = '\0';
148 
149 	if ((len = strlen(line)) > MAXNUM ||
150 	    (fraction != NULL && ((flen = strlen(fraction)) > MAXNUM)))
151 		errx(1, "number too large, max %d digits.", MAXNUM);
152 
153 	if (*line == '-') {
154 		printf("minus%s", lflag ? " " : "\n");
155 		++line;
156 		--len;
157 	}
158 
159 	rval = len > 0 ? unit(len, line) : 0;
160 	if (fraction != NULL && flen != 0)
161 		for (p = fraction; *p != '\0'; ++p)
162 			if (*p != '0') {
163 				if (rval)
164 					printf("%sand%s",
165 					    lflag ? " " : "",
166 					    lflag ? " " : "\n");
167 				if (unit(flen, fraction)) {
168 					if (lflag)
169 						printf(" ");
170 					pfract(flen);
171 					rval = 1;
172 				}
173 				break;
174 			}
175 	if (!rval)
176 		printf("zero%s", lflag ? "" : ".\n");
177 	if (lflag)
178 		printf("\n");
179 }
180 
181 int
182 unit(int len, char *p)
183 {
184 	int off, rval;
185 
186 	rval = 0;
187 	if (len > 3) {
188 		if (len % 3) {
189 			off = len % 3;
190 			len -= off;
191 			if (number(p, off)) {
192 				rval = 1;
193 				printf(" %s%s",
194 				    name3[len / 3], lflag ? " " : ".\n");
195 			}
196 			p += off;
197 		}
198 		for (; len > 3; p += 3) {
199 			len -= 3;
200 			if (number(p, 3)) {
201 				rval = 1;
202 				printf(" %s%s",
203 				    name3[len / 3], lflag ? " " : ".\n");
204 			}
205 		}
206 	}
207 	if (number(p, len)) {
208 		if (!lflag)
209 			printf(".\n");
210 		rval = 1;
211 	}
212 	return (rval);
213 }
214 
215 int
216 number(char *p, int len)
217 {
218 	int val, rval;
219 
220 	rval = 0;
221 	switch (len) {
222 	case 3:
223 		if (*p != '0') {
224 			rval = 1;
225 			printf("%s hundred", name1[*p - '0']);
226 		}
227 		++p;
228 		/* FALLTHROUGH */
229 	case 2:
230 		val = (p[1] - '0') + (p[0] - '0') * 10;
231 		if (val) {
232 			if (rval)
233 				printf(" ");
234 			if (val < 20)
235 				printf("%s", name1[val]);
236 			else {
237 				printf("%s", name2[val / 10]);
238 				if (val % 10)
239 					printf("-%s", name1[val % 10]);
240 			}
241 			rval = 1;
242 		}
243 		break;
244 	case 1:
245 		if (*p != '0') {
246 			rval = 1;
247 			printf("%s", name1[*p - '0']);
248 		}
249 	}
250 	return (rval);
251 }
252 
253 void
254 pfract(int len)
255 {
256 	static const char *pref[] = { "", "ten-", "hundred-" };
257 
258 	switch(len) {
259 	case 1:
260 		printf("tenths.\n");
261 		break;
262 	case 2:
263 		printf("hundredths.\n");
264 		break;
265 	default:
266 		printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
267 		break;
268 	}
269 }
270 
271 void
272 usage(void)
273 {
274 	fprintf(stderr, "usage: number [# ...]\n");
275 	exit(1);
276 }
277