1994-09-04 04:03:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1988, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
1999-11-16 02:45:03 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1994-09-04 04:03:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static char copyright[] =
|
|
|
|
"@(#) Copyright (c) 1988, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-03-11 17:25:32 +00:00
|
|
|
static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95";
|
1994-09-04 04:03:31 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-03-11 17:25:32 +00:00
|
|
|
#include <err.h>
|
1994-09-04 04:03:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-03-11 17:25:32 +00:00
|
|
|
#include <unistd.h>
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
#define MAXNUM 65 /* Biggest number we handle. */
|
|
|
|
|
|
|
|
static char *name1[] = {
|
|
|
|
"", "one", "two", "three",
|
|
|
|
"four", "five", "six", "seven",
|
|
|
|
"eight", "nine", "ten", "eleven",
|
|
|
|
"twelve", "thirteen", "fourteen", "fifteen",
|
|
|
|
"sixteen", "seventeen", "eighteen", "nineteen",
|
|
|
|
},
|
|
|
|
*name2[] = {
|
|
|
|
"", "ten", "twenty", "thirty",
|
|
|
|
"forty", "fifty", "sixty", "seventy",
|
|
|
|
"eighty", "ninety",
|
|
|
|
},
|
|
|
|
*name3[] = {
|
|
|
|
"hundred", "thousand", "million", "billion",
|
|
|
|
"trillion", "quadrillion", "quintillion", "sextillion",
|
|
|
|
"septillion", "octillion", "nonillion", "decillion",
|
|
|
|
"undecillion", "duodecillion", "tredecillion", "quattuordecillion",
|
1995-05-30 03:37:36 +00:00
|
|
|
"quindecillion", "sexdecillion",
|
1994-09-04 04:03:31 +00:00
|
|
|
"septendecillion", "octodecillion",
|
|
|
|
"novemdecillion", "vigintillion",
|
|
|
|
};
|
|
|
|
|
|
|
|
void convert __P((char *));
|
|
|
|
int number __P((char *, int));
|
|
|
|
void pfract __P((int));
|
|
|
|
void toobig __P((void));
|
|
|
|
int unit __P((int, char *));
|
|
|
|
void usage __P((void));
|
|
|
|
|
|
|
|
int lflag;
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
int ch, first;
|
|
|
|
char line[256];
|
|
|
|
|
|
|
|
lflag = 0;
|
1998-03-01 05:10:28 +00:00
|
|
|
while ((ch = getopt(argc, argv, "l")) != -1)
|
1994-09-04 04:03:31 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (*argv == NULL)
|
|
|
|
for (first = 1;
|
|
|
|
fgets(line, sizeof(line), stdin) != NULL; first = 0) {
|
|
|
|
if (strchr(line, '\n') == NULL)
|
|
|
|
errx(1, "line too long.");
|
|
|
|
if (!first)
|
|
|
|
(void)printf("...\n");
|
|
|
|
convert(line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (first = 1; *argv != NULL; first = 0, ++argv) {
|
|
|
|
if (!first)
|
|
|
|
(void)printf("...\n");
|
|
|
|
convert(*argv);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
convert(line)
|
|
|
|
char *line;
|
|
|
|
{
|
1999-11-16 11:49:21 +00:00
|
|
|
int flen, len, rval;
|
1999-11-16 02:58:06 +00:00
|
|
|
char *p, *fraction;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
fraction = NULL;
|
|
|
|
for (p = line; *p != '\0' && *p != '\n'; ++p) {
|
|
|
|
if (isblank(*p)) {
|
|
|
|
if (p == line) {
|
|
|
|
++line;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
goto badnum;
|
|
|
|
}
|
|
|
|
if (isdigit(*p))
|
|
|
|
continue;
|
|
|
|
switch (*p) {
|
|
|
|
case '.':
|
|
|
|
if (fraction != NULL)
|
|
|
|
goto badnum;
|
|
|
|
fraction = p + 1;
|
|
|
|
*p = '\0';
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (p == line)
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
|
|
|
badnum: errx(1, "illegal number: %s", line);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if ((len = strlen(line)) > MAXNUM ||
|
|
|
|
fraction != NULL && (flen = strlen(fraction)) > MAXNUM)
|
|
|
|
errx(1, "number too large, max %d digits.", MAXNUM);
|
|
|
|
|
|
|
|
if (*line == '-') {
|
|
|
|
(void)printf("minus%s", lflag ? " " : "\n");
|
|
|
|
++line;
|
1998-02-26 21:13:17 +00:00
|
|
|
--len;
|
1994-09-04 04:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rval = len > 0 ? unit(len, line) : 0;
|
|
|
|
if (fraction != NULL && flen != 0)
|
|
|
|
for (p = fraction; *p != '\0'; ++p)
|
|
|
|
if (*p != '0') {
|
|
|
|
if (rval)
|
|
|
|
(void)printf("%sand%s",
|
|
|
|
lflag ? " " : "",
|
|
|
|
lflag ? " " : "\n");
|
|
|
|
if (unit(flen, fraction)) {
|
|
|
|
if (lflag)
|
|
|
|
(void)printf(" ");
|
|
|
|
pfract(flen);
|
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!rval)
|
|
|
|
(void)printf("zero%s", lflag ? "" : ".\n");
|
|
|
|
if (lflag)
|
|
|
|
(void)printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
unit(len, p)
|
1999-11-16 02:58:06 +00:00
|
|
|
int len;
|
|
|
|
char *p;
|
1994-09-04 04:03:31 +00:00
|
|
|
{
|
1999-11-16 02:58:06 +00:00
|
|
|
int off, rval;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
if (len > 3) {
|
|
|
|
if (len % 3) {
|
|
|
|
off = len % 3;
|
|
|
|
len -= off;
|
|
|
|
if (number(p, off)) {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf(" %s%s",
|
|
|
|
name3[len / 3], lflag ? " " : ".\n");
|
|
|
|
}
|
|
|
|
p += off;
|
|
|
|
}
|
|
|
|
for (; len > 3; p += 3) {
|
|
|
|
len -= 3;
|
|
|
|
if (number(p, 3)) {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf(" %s%s",
|
|
|
|
name3[len / 3], lflag ? " " : ".\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (number(p, len)) {
|
|
|
|
if (!lflag)
|
|
|
|
(void)printf(".\n");
|
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
number(p, len)
|
1999-11-16 02:58:06 +00:00
|
|
|
char *p;
|
1994-09-04 04:03:31 +00:00
|
|
|
int len;
|
|
|
|
{
|
1999-11-16 02:58:06 +00:00
|
|
|
int val, rval;
|
1994-09-04 04:03:31 +00:00
|
|
|
|
|
|
|
rval = 0;
|
|
|
|
switch (len) {
|
|
|
|
case 3:
|
|
|
|
if (*p != '0') {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf("%s hundred", name1[*p - '0']);
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 2:
|
|
|
|
val = (p[1] - '0') + (p[0] - '0') * 10;
|
|
|
|
if (val) {
|
|
|
|
if (rval)
|
|
|
|
(void)printf(" ");
|
|
|
|
if (val < 20)
|
|
|
|
(void)printf("%s", name1[val]);
|
|
|
|
else {
|
|
|
|
(void)printf("%s", name2[val / 10]);
|
|
|
|
if (val % 10)
|
|
|
|
(void)printf("-%s", name1[val % 10]);
|
|
|
|
}
|
|
|
|
rval = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (*p != '0') {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf("%s", name1[*p - '0']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pfract(len)
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
static char *pref[] = { "", "ten-", "hundred-" };
|
|
|
|
|
|
|
|
switch(len) {
|
|
|
|
case 1:
|
|
|
|
(void)printf("tenths.\n");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
(void)printf("hundredths.\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr, "usage: number [# ...]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|