number.c revision 23726
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 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 41static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95"; 42#endif /* not lint */ 43 44#include <sys/types.h> 45 46#include <ctype.h> 47#include <err.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> 52 53#define MAXNUM 65 /* Biggest number we handle. */ 54 55static char *name1[] = { 56 "", "one", "two", "three", 57 "four", "five", "six", "seven", 58 "eight", "nine", "ten", "eleven", 59 "twelve", "thirteen", "fourteen", "fifteen", 60 "sixteen", "seventeen", "eighteen", "nineteen", 61}, 62 *name2[] = { 63 "", "ten", "twenty", "thirty", 64 "forty", "fifty", "sixty", "seventy", 65 "eighty", "ninety", 66}, 67 *name3[] = { 68 "hundred", "thousand", "million", "billion", 69 "trillion", "quadrillion", "quintillion", "sextillion", 70 "septillion", "octillion", "nonillion", "decillion", 71 "undecillion", "duodecillion", "tredecillion", "quattuordecillion", 72 "quindecillion", "sexdecillion", 73 "septendecillion", "octodecillion", 74 "novemdecillion", "vigintillion", 75}; 76 77void convert __P((char *)); 78int number __P((char *, int)); 79void pfract __P((int)); 80void toobig __P((void)); 81int unit __P((int, char *)); 82void usage __P((void)); 83 84int lflag; 85 86int 87main(argc, argv) 88 int argc; 89 char *argv[]; 90{ 91 int ch, first; 92 char line[256]; 93 94 lflag = 0; 95 while ((ch = getopt(argc, argv, "l")) != EOF) 96 switch (ch) { 97 case 'l': 98 lflag = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 } 104 argc -= optind; 105 argv += optind; 106 107 if (*argv == NULL) 108 for (first = 1; 109 fgets(line, sizeof(line), stdin) != NULL; first = 0) { 110 if (strchr(line, '\n') == NULL) 111 errx(1, "line too long."); 112 if (!first) 113 (void)printf("...\n"); 114 convert(line); 115 } 116 else 117 for (first = 1; *argv != NULL; first = 0, ++argv) { 118 if (!first) 119 (void)printf("...\n"); 120 convert(*argv); 121 } 122 exit(0); 123} 124 125void 126convert(line) 127 char *line; 128{ 129 register flen, len, rval; 130 register char *p, *fraction; 131 132 fraction = NULL; 133 for (p = line; *p != '\0' && *p != '\n'; ++p) { 134 if (isblank(*p)) { 135 if (p == line) { 136 ++line; 137 continue; 138 } 139 goto badnum; 140 } 141 if (isdigit(*p)) 142 continue; 143 switch (*p) { 144 case '.': 145 if (fraction != NULL) 146 goto badnum; 147 fraction = p + 1; 148 *p = '\0'; 149 break; 150 case '-': 151 if (p == line) 152 break; 153 /* FALLTHROUGH */ 154 default: 155badnum: errx(1, "illegal number: %s", line); 156 break; 157 } 158 } 159 *p = '\0'; 160 161 if ((len = strlen(line)) > MAXNUM || 162 fraction != NULL && (flen = strlen(fraction)) > MAXNUM) 163 errx(1, "number too large, max %d digits.", MAXNUM); 164 165 if (*line == '-') { 166 (void)printf("minus%s", lflag ? " " : "\n"); 167 ++line; 168 } 169 170 rval = len > 0 ? unit(len, line) : 0; 171 if (fraction != NULL && flen != 0) 172 for (p = fraction; *p != '\0'; ++p) 173 if (*p != '0') { 174 if (rval) 175 (void)printf("%sand%s", 176 lflag ? " " : "", 177 lflag ? " " : "\n"); 178 if (unit(flen, fraction)) { 179 if (lflag) 180 (void)printf(" "); 181 pfract(flen); 182 rval = 1; 183 } 184 break; 185 } 186 if (!rval) 187 (void)printf("zero%s", lflag ? "" : ".\n"); 188 if (lflag) 189 (void)printf("\n"); 190} 191 192int 193unit(len, p) 194 register int len; 195 register char *p; 196{ 197 register int off, rval; 198 199 rval = 0; 200 if (len > 3) { 201 if (len % 3) { 202 off = len % 3; 203 len -= off; 204 if (number(p, off)) { 205 rval = 1; 206 (void)printf(" %s%s", 207 name3[len / 3], lflag ? " " : ".\n"); 208 } 209 p += off; 210 } 211 for (; len > 3; p += 3) { 212 len -= 3; 213 if (number(p, 3)) { 214 rval = 1; 215 (void)printf(" %s%s", 216 name3[len / 3], lflag ? " " : ".\n"); 217 } 218 } 219 } 220 if (number(p, len)) { 221 if (!lflag) 222 (void)printf(".\n"); 223 rval = 1; 224 } 225 return (rval); 226} 227 228int 229number(p, len) 230 register char *p; 231 int len; 232{ 233 register int val, rval; 234 235 rval = 0; 236 switch (len) { 237 case 3: 238 if (*p != '0') { 239 rval = 1; 240 (void)printf("%s hundred", name1[*p - '0']); 241 } 242 ++p; 243 /* FALLTHROUGH */ 244 case 2: 245 val = (p[1] - '0') + (p[0] - '0') * 10; 246 if (val) { 247 if (rval) 248 (void)printf(" "); 249 if (val < 20) 250 (void)printf("%s", name1[val]); 251 else { 252 (void)printf("%s", name2[val / 10]); 253 if (val % 10) 254 (void)printf("-%s", name1[val % 10]); 255 } 256 rval = 1; 257 } 258 break; 259 case 1: 260 if (*p != '0') { 261 rval = 1; 262 (void)printf("%s", name1[*p - '0']); 263 } 264 } 265 return (rval); 266} 267 268void 269pfract(len) 270 int len; 271{ 272 static char *pref[] = { "", "ten-", "hundred-" }; 273 274 switch(len) { 275 case 1: 276 (void)printf("tenths.\n"); 277 break; 278 case 2: 279 (void)printf("hundredths.\n"); 280 break; 281 default: 282 (void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]); 283 break; 284 } 285} 286 287void 288usage() 289{ 290 (void)fprintf(stderr, "usage: number [# ...]\n"); 291 exit(1); 292} 293