2010-05-13 21:52:11 +04:00
|
|
|
/* $NetBSD: number.c,v 1.14 2010/05/13 17:52:11 tnozaki Exp $ */
|
1995-03-23 11:28:00 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-03-23 11:28:00 +03:00
|
|
|
* Copyright (c) 1988, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-10 20:38:40 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
2008-07-20 04:52:39 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1988, 1993, 1994\
|
|
|
|
The Regents of the University of California. All rights reserved.");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-23 11:28:00 +03:00
|
|
|
#if 0
|
1997-01-07 14:56:32 +03:00
|
|
|
static char sccsid[] = "@(#)number.c 8.3 (Berkeley) 5/4/95";
|
1995-03-23 11:28:00 +03:00
|
|
|
#else
|
2010-05-13 21:52:11 +04:00
|
|
|
__RCSID("$NetBSD: number.c,v 1.14 2010/05/13 17:52:11 tnozaki Exp $");
|
1995-03-23 11:28:00 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <ctype.h>
|
1997-01-07 14:56:32 +03:00
|
|
|
#include <err.h>
|
1995-03-23 11:28:00 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-01-07 14:56:32 +03:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
#define MAXNUM 65 /* Biggest number we handle. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
static const char *const name1[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"", "one", "two", "three",
|
|
|
|
"four", "five", "six", "seven",
|
|
|
|
"eight", "nine", "ten", "eleven",
|
|
|
|
"twelve", "thirteen", "fourteen", "fifteen",
|
|
|
|
"sixteen", "seventeen", "eighteen", "nineteen",
|
|
|
|
},
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
*const name2[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"", "ten", "twenty", "thirty",
|
|
|
|
"forty", "fifty", "sixty", "seventy",
|
|
|
|
"eighty", "ninety",
|
|
|
|
},
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
*const name3[] = {
|
1993-03-21 12:45:37 +03:00
|
|
|
"hundred", "thousand", "million", "billion",
|
|
|
|
"trillion", "quadrillion", "quintillion", "sextillion",
|
|
|
|
"septillion", "octillion", "nonillion", "decillion",
|
|
|
|
"undecillion", "duodecillion", "tredecillion", "quattuordecillion",
|
|
|
|
"quindecillion", "sexdecillion",
|
|
|
|
"septendecillion", "octodecillion",
|
|
|
|
"novemdecillion", "vigintillion",
|
|
|
|
};
|
|
|
|
|
2004-01-27 23:30:28 +03:00
|
|
|
int main(int, char *[]);
|
2009-08-12 12:04:05 +04:00
|
|
|
static void convert(char *);
|
|
|
|
static int number(const char *, int);
|
|
|
|
static void pfract(int);
|
|
|
|
static int unit(int, const char *);
|
|
|
|
static void usage(void) __dead;
|
1995-03-23 11:28:00 +03:00
|
|
|
|
2009-08-12 12:04:05 +04:00
|
|
|
static int lflag;
|
1995-03-23 11:28:00 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-03-23 11:28:00 +03:00
|
|
|
int ch, first;
|
|
|
|
char line[256];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
lflag = 0;
|
1997-10-10 20:38:40 +04:00
|
|
|
while ((ch = getopt(argc, argv, "l")) != -1)
|
1995-03-23 11:28:00 +03:00
|
|
|
switch (ch) {
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
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");
|
1993-03-21 12:45:37 +03:00
|
|
|
convert(line);
|
1995-03-23 11:28:00 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
for (first = 1; *argv != NULL; first = 0, ++argv) {
|
|
|
|
if (!first)
|
|
|
|
(void)printf("...\n");
|
|
|
|
convert(*argv);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
convert(line)
|
1995-03-23 11:28:00 +03:00
|
|
|
char *line;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 20:38:40 +04:00
|
|
|
int flen, len, rval;
|
|
|
|
char *p, *fraction;
|
1995-03-23 11:28:00 +03:00
|
|
|
|
1997-10-10 20:38:40 +04:00
|
|
|
flen = 0;
|
1995-03-23 11:28:00 +03:00
|
|
|
fraction = NULL;
|
|
|
|
for (p = line; *p != '\0' && *p != '\n'; ++p) {
|
2010-05-13 21:52:11 +04:00
|
|
|
if (isblank((unsigned char)*p)) {
|
1995-03-23 11:28:00 +03:00
|
|
|
if (p == line) {
|
|
|
|
++line;
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
goto badnum;
|
|
|
|
}
|
2004-11-06 00:30:31 +03:00
|
|
|
if (isdigit((unsigned char)*p))
|
1995-03-23 11:28:00 +03:00
|
|
|
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 ||
|
1997-10-10 20:38:40 +04:00
|
|
|
(fraction != NULL && (flen = strlen(fraction)) > MAXNUM))
|
1995-03-23 11:28:00 +03:00
|
|
|
errx(1, "number too large, max %d digits.", MAXNUM);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (*line == '-') {
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("minus%s", lflag ? " " : "\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
++line;
|
1998-09-13 19:24:57 +04:00
|
|
|
--len;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03: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;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
if (!rval)
|
|
|
|
(void)printf("zero%s", lflag ? "" : ".\n");
|
|
|
|
if (lflag)
|
|
|
|
(void)printf("\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
int
|
|
|
|
unit(len, p)
|
1997-10-10 20:38:40 +04:00
|
|
|
int len;
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const char *p;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 20:38:40 +04:00
|
|
|
int off, rval;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
rval = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (len > 3) {
|
|
|
|
if (len % 3) {
|
|
|
|
off = len % 3;
|
|
|
|
len -= off;
|
1995-03-23 11:28:00 +03:00
|
|
|
if (number(p, off)) {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf(" %s%s",
|
|
|
|
name3[len / 3], lflag ? " " : ".\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
p += off;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
for (; len > 3; p += 3) {
|
1993-03-21 12:45:37 +03:00
|
|
|
len -= 3;
|
1995-03-23 11:28:00 +03:00
|
|
|
if (number(p, 3)) {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf(" %s%s",
|
|
|
|
name3[len / 3], lflag ? " " : ".\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
if (number(p, len)) {
|
|
|
|
if (!lflag)
|
|
|
|
(void)printf(".\n");
|
|
|
|
rval = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
return (rval);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
int
|
|
|
|
number(p, len)
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const char *p;
|
1995-03-23 11:28:00 +03:00
|
|
|
int len;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-10 20:38:40 +04:00
|
|
|
int val, rval;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
rval = 0;
|
|
|
|
switch (len) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case 3:
|
1995-03-23 11:28:00 +03:00
|
|
|
if (*p != '0') {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf("%s hundred", name1[*p - '0']);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
++p;
|
|
|
|
/* FALLTHROUGH */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 2:
|
1995-03-23 11:28:00 +03:00
|
|
|
val = (p[1] - '0') + (p[0] - '0') * 10;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (val) {
|
1995-03-23 11:28:00 +03:00
|
|
|
if (rval)
|
|
|
|
(void)printf(" ");
|
1993-03-21 12:45:37 +03:00
|
|
|
if (val < 20)
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("%s", name1[val]);
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("%s", name2[val / 10]);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (val % 10)
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("-%s", name1[val % 10]);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
rval = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
1995-03-23 11:28:00 +03:00
|
|
|
if (*p != '0') {
|
|
|
|
rval = 1;
|
|
|
|
(void)printf("%s", name1[*p - '0']);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
return (rval);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
void
|
1993-03-21 12:45:37 +03:00
|
|
|
pfract(len)
|
1995-03-23 11:28:00 +03:00
|
|
|
int len;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
static const char *const pref[] = { "", "ten-", "hundred-" };
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch(len) {
|
|
|
|
case 1:
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("tenths.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
case 2:
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("hundredths.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
default:
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)printf("%s%sths.\n", pref[len % 3], name3[len / 3]);
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
void
|
|
|
|
usage()
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1995-03-23 11:28:00 +03:00
|
|
|
(void)fprintf(stderr, "usage: number [# ...]\n");
|
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|