]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - number/number.c
1 /* $NetBSD: number.c,v 1.6 1998/09/13 15:24:57 hubertf Exp $ */
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)number.c 8.3 (Berkeley) 5/4/95";
46 __RCSID("$NetBSD: number.c,v 1.6 1998/09/13 15:24:57 hubertf Exp $");
50 #include <sys/types.h>
59 #define MAXNUM 65 /* Biggest number we handle. */
61 static char *name1
[] = {
62 "", "one", "two", "three",
63 "four", "five", "six", "seven",
64 "eight", "nine", "ten", "eleven",
65 "twelve", "thirteen", "fourteen", "fifteen",
66 "sixteen", "seventeen", "eighteen", "nineteen",
69 "", "ten", "twenty", "thirty",
70 "forty", "fifty", "sixty", "seventy",
74 "hundred", "thousand", "million", "billion",
75 "trillion", "quadrillion", "quintillion", "sextillion",
76 "septillion", "octillion", "nonillion", "decillion",
77 "undecillion", "duodecillion", "tredecillion", "quattuordecillion",
78 "quindecillion", "sexdecillion",
79 "septendecillion", "octodecillion",
80 "novemdecillion", "vigintillion",
83 void convert
__P((char *));
84 int main
__P((int, char *[]));
85 int number
__P((char *, int));
86 void pfract
__P((int));
87 void toobig
__P((void));
88 int unit
__P((int, char *));
89 void usage
__P((void)) __attribute__((__noreturn__
));
102 while ((ch
= getopt(argc
, argv
, "l")) != -1)
116 fgets(line
, sizeof(line
), stdin
) != NULL
; first
= 0) {
117 if (strchr(line
, '\n') == NULL
)
118 errx(1, "line too long.");
120 (void)printf("...\n");
124 for (first
= 1; *argv
!= NULL
; first
= 0, ++argv
) {
126 (void)printf("...\n");
141 for (p
= line
; *p
!= '\0' && *p
!= '\n'; ++p
) {
153 if (fraction
!= NULL
)
163 badnum
: errx(1, "illegal number: %s", line
);
169 if ((len
= strlen(line
)) > MAXNUM
||
170 (fraction
!= NULL
&& (flen
= strlen(fraction
)) > MAXNUM
))
171 errx(1, "number too large, max %d digits.", MAXNUM
);
174 (void)printf("minus%s", lflag
? " " : "\n");
179 rval
= len
> 0 ? unit(len
, line
) : 0;
180 if (fraction
!= NULL
&& flen
!= 0)
181 for (p
= fraction
; *p
!= '\0'; ++p
)
184 (void)printf("%sand%s",
187 if (unit(flen
, fraction
)) {
196 (void)printf("zero%s", lflag
? "" : ".\n");
213 if (number(p
, off
)) {
215 (void)printf(" %s%s",
216 name3
[len
/ 3], lflag
? " " : ".\n");
220 for (; len
> 3; p
+= 3) {
224 (void)printf(" %s%s",
225 name3
[len
/ 3], lflag
? " " : ".\n");
229 if (number(p
, len
)) {
249 (void)printf("%s hundred", name1
[*p
- '0']);
254 val
= (p
[1] - '0') + (p
[0] - '0') * 10;
259 (void)printf("%s", name1
[val
]);
261 (void)printf("%s", name2
[val
/ 10]);
263 (void)printf("-%s", name1
[val
% 10]);
271 (void)printf("%s", name1
[*p
- '0']);
281 static char *pref
[] = { "", "ten-", "hundred-" };
285 (void)printf("tenths.\n");
288 (void)printf("hundredths.\n");
291 (void)printf("%s%sths.\n", pref
[len
% 3], name3
[len
/ 3]);
299 (void)fprintf(stderr
, "usage: number [# ...]\n");