number.c revision 53210
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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD: head/games/number/number.c 53210 1999-11-16 02:58:06Z billf $ 34 */ 35 36#ifndef lint 37static 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 43static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95"; 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 57static char *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 *name2[] = { 65 "", "ten", "twenty", "thirty", 66 "forty", "fifty", "sixty", "seventy", 67 "eighty", "ninety", 68}, 69 *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 79void convert __P((char *)); 80int number __P((char *, int)); 81void pfract __P((int)); 82void toobig __P((void)); 83int unit __P((int, char *)); 84void usage __P((void)); 85 86int lflag; 87 88int 89main(argc, argv) 90 int argc; 91 char *argv[]; 92{ 93 int ch, first; 94 char line[256]; 95 96 lflag = 0; 97 while ((ch = getopt(argc, argv, "l")) != -1) 98 switch (ch) { 99 case 'l': 100 lflag = 1; 101 break; 102 case '?': 103 default: 104 usage(); 105 } 106 argc -= optind; 107 argv += optind; 108 109 if (*argv == NULL) 110 for (first = 1; 111 fgets(line, sizeof(line), stdin) != NULL; first = 0) { 112 if (strchr(line, '\n') == NULL) 113 errx(1, "line too long."); 114 if (!first) 115 (void)printf("...\n"); 116 convert(line); 117 } 118 else 119 for (first = 1; *argv != NULL; first = 0, ++argv) { 120 if (!first) 121 (void)printf("...\n"); 122 convert(*argv); 123 } 124 exit(0); 125} 126 127void 128convert(line) 129 char *line; 130{ 131 flen, len, rval; 132 char *p, *fraction; 133 134 fraction = NULL; 135 for (p = line; *p != '\0' && *p != '\n'; ++p) { 136 if (isblank(*p)) { 137 if (p == line) { 138 ++line; 139 continue; 140 } 141 goto badnum; 142 } 143 if (isdigit(*p)) 144 continue; 145 switch (*p) { 146 case '.': 147 if (fraction != NULL) 148 goto badnum; 149 fraction = p + 1; 150 *p = '\0'; 151 break; 152 case '-': 153 if (p == line) 154 break; 155 /* FALLTHROUGH */ 156 default: 157badnum: errx(1, "illegal number: %s", line); 158 break; 159 } 160 } 161 *p = '\0'; 162 163 if ((len = strlen(line)) > MAXNUM || 164 fraction != NULL && (flen = strlen(fraction)) > MAXNUM) 165 errx(1, "number too large, max %d digits.", MAXNUM); 166 167 if (*line == '-') { 168 (void)printf("minus%s", lflag ? " " : "\n"); 169 ++line; 170 --len; 171 } 172 173 rval = len > 0 ? unit(len, line) : 0; 174 if (fraction != NULL && flen != 0) 175 for (p = fraction; *p != '\0'; ++p) 176 if (*p != '0') { 177 if (rval) 178 (void)printf("%sand%s", 179 lflag ? " " : "", 180 lflag ? " " : "\n"); 181 if (unit(flen, fraction)) { 182 if (lflag) 183 (void)printf(" "); 184 pfract(flen); 185 rval = 1; 186 } 187 break; 188 } 189 if (!rval) 190 (void)printf("zero%s", lflag ? "" : ".\n"); 191 if (lflag) 192 (void)printf("\n"); 193} 194 195int 196unit(len, p) 197 int len; 198 char *p; 199{ 200 int off, rval; 201 202 rval = 0; 203 if (len > 3) { 204 if (len % 3) { 205 off = len % 3; 206 len -= off; 207 if (number(p, off)) { 208 rval = 1; 209 (void)printf(" %s%s", 210 name3[len / 3], lflag ? " " : ".\n"); 211 } 212 p += off; 213 } 214 for (; len > 3; p += 3) { 215 len -= 3; 216 if (number(p, 3)) { 217 rval = 1; 218 (void)printf(" %s%s", 219 name3[len / 3], lflag ? " " : ".\n"); 220 } 221 } 222 } 223 if (number(p, len)) { 224 if (!lflag) 225 (void)printf(".\n"); 226 rval = 1; 227 } 228 return (rval); 229} 230 231int 232number(p, len) 233 char *p; 234 int len; 235{ 236 int val, rval; 237 238 rval = 0; 239 switch (len) { 240 case 3: 241 if (*p != '0') { 242 rval = 1; 243 (void)printf("%s hundred", name1[*p - '0']); 244 } 245 ++p; 246 /* FALLTHROUGH */ 247 case 2: 248 val = (p[1] - '0') + (p[0] - '0') * 10; 249 if (val) { 250 if (rval) 251 (void)printf(" "); 252 if (val < 20) 253 (void)printf("%s", name1[val]); 254 else { 255 (void)printf("%s", name2[val / 10]); 256 if (val % 10) 257 (void)printf("-%s", name1[val % 10]); 258 } 259 rval = 1; 260 } 261 break; 262 case 1: 263 if (*p != '0') { 264 rval = 1; 265 (void)printf("%s", name1[*p - '0']); 266 } 267 } 268 return (rval); 269} 270 271void 272pfract(len) 273 int len; 274{ 275 static char *pref[] = { "", "ten-", "hundred-" }; 276 277 switch(len) { 278 case 1: 279 (void)printf("tenths.\n"); 280 break; 281 case 2: 282 (void)printf("hundredths.\n"); 283 break; 284 default: 285 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]); 286 break; 287 } 288} 289 290void 291usage() 292{ 293 (void)fprintf(stderr, "usage: number [# ...]\n"); 294 exit(1); 295} 296