]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - number/number.c
1 /* $NetBSD: number.c,v 1.3 1995/03/23 08:35:30 cgd 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
37 static char copyright
[] =
38 "@(#) 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.2 (Berkeley) 3/31/94";
46 static char rcsid
[] = "$NetBSD: number.c,v 1.3 1995/03/23 08:35:30 cgd Exp $";
50 #include <sys/types.h>
58 #define MAXNUM 65 /* Biggest number we handle. */
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",
68 "", "ten", "twenty", "thirty",
69 "forty", "fifty", "sixty", "seventy",
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",
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));
100 while ((ch
= getopt(argc
, argv
, "l")) != EOF
)
114 fgets(line
, sizeof(line
), stdin
) != NULL
; first
= 0) {
115 if (strchr(line
, '\n') == NULL
)
116 errx(1, "line too long.");
118 (void)printf("...\n");
122 for (first
= 1; *argv
!= NULL
; first
= 0, ++argv
) {
124 (void)printf("...\n");
134 register flen
, len
, rval
;
135 register char *p
, *fraction
;
138 for (p
= line
; *p
!= '\0' && *p
!= '\n'; ++p
) {
150 if (fraction
!= NULL
)
160 badnum
: errx(1, "illegal number: %s", line
);
166 if ((len
= strlen(line
)) > MAXNUM
||
167 fraction
!= NULL
&& (flen
= strlen(fraction
)) > MAXNUM
)
168 errx(1, "number too large, max %d digits.", MAXNUM
);
171 (void)printf("minus%s", lflag
? " " : "\n");
175 rval
= len
> 0 ? unit(len
, line
) : 0;
176 if (fraction
!= NULL
&& flen
!= 0)
177 for (p
= fraction
; *p
!= '\0'; ++p
)
180 (void)printf("%sand%s",
183 if (unit(flen
, fraction
)) {
192 (void)printf("zero%s", lflag
? "" : ".\n");
202 register int off
, rval
;
209 if (number(p
, off
)) {
211 (void)printf(" %s%s",
212 name3
[len
/ 3], lflag
? " " : ".\n");
216 for (; len
> 3; p
+= 3) {
220 (void)printf(" %s%s",
221 name3
[len
/ 3], lflag
? " " : ".\n");
225 if (number(p
, len
)) {
238 register int val
, rval
;
245 (void)printf("%s hundred", name1
[*p
- '0']);
250 val
= (p
[1] - '0') + (p
[0] - '0') * 10;
255 (void)printf("%s", name1
[val
]);
257 (void)printf("%s", name2
[val
/ 10]);
259 (void)printf("-%s", name1
[val
% 10]);
267 (void)printf("%s", name1
[*p
- '0']);
277 static char *pref
[] = { "", "ten-", "hundred-" };
281 (void)printf("tenths.\n");
284 (void)printf("hundredths.\n");
287 (void)printf("%s%sths.\n", pref
[len
% 3], name3
[len
/ 3]);
295 (void)fprintf(stderr
, "usage: number [# ...]\n");