number.c revision 53209
156883Smjacob/* 267627Sasmodai * Copyright (c) 1988, 1993, 1994 356883Smjacob * The Regents of the University of California. All rights reserved. 456883Smjacob * 556883Smjacob * Redistribution and use in source and binary forms, with or without 656883Smjacob * modification, are permitted provided that the following conditions 756883Smjacob * are met: 856883Smjacob * 1. Redistributions of source code must retain the above copyright 956883Smjacob * notice, this list of conditions and the following disclaimer. 1056883Smjacob * 2. Redistributions in binary form must reproduce the above copyright 1156883Smjacob * notice, this list of conditions and the following disclaimer in the 1256883Smjacob * documentation and/or other materials provided with the distribution. 1356883Smjacob * 3. All advertising materials mentioning features or use of this software 1456883Smjacob * must display the following acknowledgement: 1556883Smjacob * This product includes software developed by the University of 1656883Smjacob * California, Berkeley and its contributors. 1756883Smjacob * 4. Neither the name of the University nor the names of its contributors 1856883Smjacob * may be used to endorse or promote products derived from this software 1956883Smjacob * without specific prior written permission. 2056883Smjacob * 2156883Smjacob * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2256883Smjacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2356883Smjacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2456883Smjacob * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2556883Smjacob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2656883Smjacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2756883Smjacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28359204Sasomers * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2956883Smjacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3079538Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3156883Smjacob * SUCH DAMAGE. 3256883Smjacob * 3375670Sru * $FreeBSD: head/games/number/number.c 53209 1999-11-16 02:45:03Z billf $ 3456883Smjacob */ 3556883Smjacob 3656883Smjacob#ifndef lint 3756883Smjacobstatic char copyright[] = 3868962Sru"@(#) Copyright (c) 1988, 1993, 1994\n\ 3956883Smjacob The Regents of the University of California. All rights reserved.\n"; 4056883Smjacob#endif /* not lint */ 4156883Smjacob 4256883Smjacob#ifndef lint 4356883Smjacobstatic char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95"; 4456883Smjacob#endif /* not lint */ 4556883Smjacob 4656883Smjacob#include <sys/types.h> 4756883Smjacob 4856883Smjacob#include <ctype.h> 4956883Smjacob#include <err.h> 5056883Smjacob#include <stdio.h> 5156883Smjacob#include <stdlib.h> 5256883Smjacob#include <string.h> 5356883Smjacob#include <unistd.h> 5456883Smjacob 5556883Smjacob#define MAXNUM 65 /* Biggest number we handle. */ 5679687Sschweikh 5756883Smjacobstatic char *name1[] = { 5856883Smjacob "", "one", "two", "three", 5968962Sru "four", "five", "six", "seven", 6056883Smjacob "eight", "nine", "ten", "eleven", 6156883Smjacob "twelve", "thirteen", "fourteen", "fifteen", 6256883Smjacob "sixteen", "seventeen", "eighteen", "nineteen", 6356883Smjacob}, 6456883Smjacob *name2[] = { 6556883Smjacob "", "ten", "twenty", "thirty", 6670466Sru "forty", "fifty", "sixty", "seventy", 6756883Smjacob "eighty", "ninety", 6856883Smjacob}, 6956883Smjacob *name3[] = { 7056883Smjacob "hundred", "thousand", "million", "billion", 7170466Sru "trillion", "quadrillion", "quintillion", "sextillion", 7256883Smjacob "septillion", "octillion", "nonillion", "decillion", 7356883Smjacob "undecillion", "duodecillion", "tredecillion", "quattuordecillion", 7456883Smjacob "quindecillion", "sexdecillion", 7556883Smjacob "septendecillion", "octodecillion", 7656883Smjacob "novemdecillion", "vigintillion", 7770466Sru}; 7856883Smjacob 7956883Smjacobvoid convert __P((char *)); 80359204Sasomersint number __P((char *, int)); 8156883Smjacobvoid pfract __P((int)); 82359204Sasomersvoid toobig __P((void)); 83359204Sasomersint unit __P((int, char *)); 8456883Smjacobvoid usage __P((void)); 8570466Sru 86359204Sasomersint lflag; 87359204Sasomers 88359204Sasomersint 89359204Sasomersmain(argc, argv) 9070466Sru int argc; 91359204Sasomers char *argv[]; 92359204Sasomers{ 9356883Smjacob int ch, first; 94359204Sasomers char line[256]; 9556883Smjacob 96359204Sasomers lflag = 0; 97359204Sasomers while ((ch = getopt(argc, argv, "l")) != -1) 98359204Sasomers switch (ch) { 99359204Sasomers case 'l': 100359204Sasomers lflag = 1; 101359204Sasomers break; 10270466Sru case '?': 103359204Sasomers default: 104235317Sgjb usage(); 105359204Sasomers } 10656883Smjacob argc -= optind; 107359204Sasomers argv += optind; 108359204Sasomers 109359204Sasomers if (*argv == NULL) 110359204Sasomers for (first = 1; 111359204Sasomers fgets(line, sizeof(line), stdin) != NULL; first = 0) { 112359204Sasomers if (strchr(line, '\n') == NULL) 113359204Sasomers errx(1, "line too long."); 114359204Sasomers if (!first) 115359204Sasomers (void)printf("...\n"); 116359204Sasomers convert(line); 117359204Sasomers } 118359204Sasomers else 119359204Sasomers for (first = 1; *argv != NULL; first = 0, ++argv) { 120359204Sasomers if (!first) 12170466Sru (void)printf("...\n"); 12258517Smpp convert(*argv); 12356883Smjacob } 124342210Scy exit(0); 12556883Smjacob} 12656883Smjacob 12756883Smjacobvoid 12856883Smjacobconvert(line) 12956883Smjacob char *line; 13058517Smpp{ 13158517Smpp register flen, len, rval; 13258517Smpp register char *p, *fraction; 13358517Smpp 13470466Sru fraction = NULL; 13556883Smjacob for (p = line; *p != '\0' && *p != '\n'; ++p) { 13656883Smjacob if (isblank(*p)) { 13756883Smjacob if (p == line) { 13856883Smjacob ++line; 13956883Smjacob continue; 140287482Sbapt } 141287482Sbapt goto badnum; 14256883Smjacob } 14356883Smjacob if (isdigit(*p)) 14456883Smjacob continue; 145359204Sasomers switch (*p) { 14656883Smjacob case '.': 14756883Smjacob if (fraction != NULL) 148359204Sasomers goto badnum; 149359204Sasomers fraction = p + 1; 150359204Sasomers *p = '\0'; 15158517Smpp break; 152359204Sasomers case '-': 153359204Sasomers 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 register int len; 198 register char *p; 199{ 200 register 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 register char *p; 234 int len; 235{ 236 register 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