number.c revision 201176
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 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1988, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/games/number/number.c 201176 2009-12-29 08:43:32Z ed $"; 46#endif /* not lint */ 47 48#include <sys/types.h> 49 50#include <ctype.h> 51#include <err.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <unistd.h> 56 57#define MAXNUM 65 /* Biggest number we handle. */ 58 59static const char *name1[] = { 60 "", "one", "two", "three", 61 "four", "five", "six", "seven", 62 "eight", "nine", "ten", "eleven", 63 "twelve", "thirteen", "fourteen", "fifteen", 64 "sixteen", "seventeen", "eighteen", "nineteen", 65}, 66 *name2[] = { 67 "", "ten", "twenty", "thirty", 68 "forty", "fifty", "sixty", "seventy", 69 "eighty", "ninety", 70}, 71 *name3[] = { 72 "hundred", "thousand", "million", "billion", 73 "trillion", "quadrillion", "quintillion", "sextillion", 74 "septillion", "octillion", "nonillion", "decillion", 75 "undecillion", "duodecillion", "tredecillion", "quattuordecillion", 76 "quindecillion", "sexdecillion", 77 "septendecillion", "octodecillion", 78 "novemdecillion", "vigintillion", 79}; 80 81void convert(char *); 82int number(char *, int); 83void pfract(int); 84void toobig(void); 85int unit(int, char *); 86void usage(void); 87 88int lflag; 89 90int 91main(int argc, 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(char *line) 129{ 130 int flen, len, rval; 131 char *p, *fraction; 132 133 flen = 0; 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(int len, char *p) 197{ 198 int off, rval; 199 200 rval = 0; 201 if (len > 3) { 202 if (len % 3) { 203 off = len % 3; 204 len -= off; 205 if (number(p, off)) { 206 rval = 1; 207 (void)printf(" %s%s", 208 name3[len / 3], lflag ? " " : ".\n"); 209 } 210 p += off; 211 } 212 for (; len > 3; p += 3) { 213 len -= 3; 214 if (number(p, 3)) { 215 rval = 1; 216 (void)printf(" %s%s", 217 name3[len / 3], lflag ? " " : ".\n"); 218 } 219 } 220 } 221 if (number(p, len)) { 222 if (!lflag) 223 (void)printf(".\n"); 224 rval = 1; 225 } 226 return (rval); 227} 228 229int 230number(char *p, int len) 231{ 232 int val, rval; 233 234 rval = 0; 235 switch (len) { 236 case 3: 237 if (*p != '0') { 238 rval = 1; 239 (void)printf("%s hundred", name1[*p - '0']); 240 } 241 ++p; 242 /* FALLTHROUGH */ 243 case 2: 244 val = (p[1] - '0') + (p[0] - '0') * 10; 245 if (val) { 246 if (rval) 247 (void)printf(" "); 248 if (val < 20) 249 (void)printf("%s", name1[val]); 250 else { 251 (void)printf("%s", name2[val / 10]); 252 if (val % 10) 253 (void)printf("-%s", name1[val % 10]); 254 } 255 rval = 1; 256 } 257 break; 258 case 1: 259 if (*p != '0') { 260 rval = 1; 261 (void)printf("%s", name1[*p - '0']); 262 } 263 } 264 return (rval); 265} 266 267void 268pfract(int len) 269{ 270 static char const * const pref[] = { "", "ten-", "hundred-" }; 271 272 switch(len) { 273 case 1: 274 (void)printf("tenths.\n"); 275 break; 276 case 2: 277 (void)printf("hundredths.\n"); 278 break; 279 default: 280 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]); 281 break; 282 } 283} 284 285void 286usage(void) 287{ 288 (void)fprintf(stderr, "usage: number [-l] [# ...]\n"); 289 exit(1); 290} 291