summaryrefslogtreecommitdiffstats
path: root/arithmetic
diff options
context:
space:
mode:
authorlukem <lukem@NetBSD.org>1997-10-09 23:07:00 +0000
committerlukem <lukem@NetBSD.org>1997-10-09 23:07:00 +0000
commit3c0c7a72c2f0803e801f2330c329aff2eee680d5 (patch)
tree6d92adc9fc2a7d570c0426be556598ee7bc7a953 /arithmetic
parente70ef401ba43f3b9aee92ed87dccd079e08ce3bf (diff)
downloadbsdgames-darwin-3c0c7a72c2f0803e801f2330c329aff2eee680d5.tar.gz
bsdgames-darwin-3c0c7a72c2f0803e801f2330c329aff2eee680d5.tar.zst
bsdgames-darwin-3c0c7a72c2f0803e801f2330c329aff2eee680d5.zip
KNFify, WARNSify, use errx(), clean up .Nm in manpage
Diffstat (limited to 'arithmetic')
-rw-r--r--arithmetic/arithmetic.616
-rw-r--r--arithmetic/arithmetic.c71
2 files changed, 49 insertions, 38 deletions
diff --git a/arithmetic/arithmetic.6 b/arithmetic/arithmetic.6
index 54da170d..b24c4be5 100644
--- a/arithmetic/arithmetic.6
+++ b/arithmetic/arithmetic.6
@@ -1,4 +1,4 @@
-.\" $NetBSD: arithmetic.6,v 1.4 1995/03/21 11:59:31 cgd Exp $
+.\" $NetBSD: arithmetic.6,v 1.5 1997/10/09 23:07:00 lukem Exp $
.\"
.\" Copyright (c) 1989, 1993
.\" The Regents of the University of California. All rights reserved.
@@ -43,11 +43,11 @@
.Nm arithmetic
.Nd quiz on simple arithmetic
.Sh SYNOPSIS
-.Nm arithmetic
+.Nm
.Op Fl o Ar +\-x/
.Op Fl r Ar range
.Sh DESCRIPTION
-.Nm Arithmetic
+.Nm
asks you to solve problems in simple arithmetic.
Each question must be answered correctly before going on to the next.
After every 20 problems, it prints the score so far and the time taken.
@@ -57,7 +57,7 @@ The options are as follows:
.Bl -tag -width indent
.It Fl o
By default,
-.Nm arithmetic
+.Nm
asks questions on addition of numbers from 0 to 10, and corresponding
subtraction.
By supplying one or more of the characters
@@ -70,7 +70,7 @@ will be asked correspondingly more often.
If a
.Ar range
is supplied,
-.Nm arithmetic
+.Nm
selects the numbers in its problems in the following way.
For addition and multiplication, the numbers to be added or multiplied
are between 0 and
@@ -80,19 +80,19 @@ For subtraction and division, both the required result and the number to
divide by or subtract will be between 0 and
.Ar range .
(Of course,
-.Nm arithmetic
+.Nm
will not ask you to divide by 0.) The default
.I range
is 10.
.El
.Pp
When you get a problem wrong,
-.Nm arithmetic
+.Nm
will remember the numbers involved, and will tend to select those numbers
more often than others, in problems of the same sort.
Eventually it will forgive and forget.
.Pp
-.Nm Arithmetic
+.Nm
cannot be persuaded to tell you the right answer.
You must work it out for yourself.
.Sh DIAGNOSTICS
diff --git a/arithmetic/arithmetic.c b/arithmetic/arithmetic.c
index 6cda404f..8e943993 100644
--- a/arithmetic/arithmetic.c
+++ b/arithmetic/arithmetic.c
@@ -1,4 +1,4 @@
-/* $NetBSD: arithmetic.c,v 1.6 1996/03/21 18:30:19 jtc Exp $ */
+/* $NetBSD: arithmetic.c,v 1.7 1997/10/09 23:07:02 lukem Exp $ */
/*
* Copyright (c) 1989, 1993
@@ -36,17 +36,17 @@
* SUCH DAMAGE.
*/
+#include <sys/cdefs.h>
#ifndef lint
-static char copyright[] =
-"@(#) Copyright (c) 1989, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
+__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
+ The Regents of the University of California. All rights reserved.\n");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)arithmetic.c 8.1 (Berkeley) 5/31/93";
#else
-static char rcsid[] = "$NetBSD: arithmetic.c,v 1.6 1996/03/21 18:30:19 jtc Exp $";
+__RCSID("$NetBSD: arithmetic.c,v 1.7 1997/10/09 23:07:02 lukem Exp $");
#endif
#endif /* not lint */
@@ -79,12 +79,23 @@ static char rcsid[] = "$NetBSD: arithmetic.c,v 1.6 1996/03/21 18:30:19 jtc Exp $
*/
#include <sys/types.h>
-#include <signal.h>
+#include <err.h>
#include <ctype.h>
+#include <signal.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <time.h>
+int getrandom __P((int, int, int));
+void intr __P((int));
+int main __P((int, char *[]));
+int opnum __P((int));
+void penalise __P((int, int, int));
+int problem __P((void));
+void showstats __P((void));
+void usage __P((void));
+
char keylist[] = "+-x/";
char defaultkeys[] = "+-";
char *keys = defaultkeys;
@@ -109,28 +120,21 @@ main(argc, argv)
extern char *optarg;
extern int optind;
int ch, cnt;
- void intr();
while ((ch = getopt(argc, argv, "r:o:")) != EOF)
switch(ch) {
case 'o': {
- register char *p;
+ char *p;
for (p = keys = optarg; *p; ++p)
- if (!index(keylist, *p)) {
- (void)fprintf(stderr,
- "arithmetic: unknown key.\n");
- exit(1);
- }
+ if (!strchr(keylist, *p))
+ errx(1, "arithmetic: unknown key.");
nkeys = p - optarg;
break;
}
case 'r':
- if ((rangemax = atoi(optarg)) <= 0) {
- (void)fprintf(stderr,
- "arithmetic: invalid range.\n");
- exit(1);
- }
+ if ((rangemax = atoi(optarg)) <= 0)
+ errx(1, "arithmetic: invalid range.");
break;
case '?':
default:
@@ -156,13 +160,15 @@ main(argc, argv)
/* Handle interrupt character. Print score and exit. */
void
-intr()
+intr(dummy)
+ int dummy;
{
showstats();
exit(0);
}
/* Print score. Original `arithmetic' had a delay after printing it. */
+void
showstats()
{
if (nright + nwrong > 0) {
@@ -183,13 +189,15 @@ showstats()
* answer causes the numbers in the problem to be penalised, so that they are
* more likely to appear in subsequent problems.
*/
+int
problem()
{
- register char *p;
+ char *p;
time_t start, finish;
int left, op, right, result;
char line[80];
+ left = result = 0;
op = keys[random() % nkeys];
if (op != '/')
right = getrandom(rangemax + 1, op, 1);
@@ -294,11 +302,11 @@ struct penalty {
* operand number `operand' (0 or 1). If we run out of memory, we just
* forget about the penalty (how likely is this, anyway?).
*/
+void
penalise(value, op, operand)
int value, op, operand;
{
struct penalty *p;
- char *malloc();
op = opnum(op);
if ((p = (struct penalty *)malloc((u_int)sizeof(*p))) == NULL)
@@ -315,11 +323,12 @@ penalise(value, op, operand)
* as a value, or represents a position in the penalty list. If the latter,
* we find the corresponding value and return that, decreasing its penalty.
*/
+int
getrandom(maxval, op, operand)
int maxval, op, operand;
{
int value;
- register struct penalty **pp, *p;
+ struct penalty **pp, *p;
op = opnum(op);
value = random() % (maxval + penalty[op][operand]);
@@ -354,28 +363,30 @@ getrandom(maxval, op, operand)
* correspond to the actual sum of penalties in the list. Provide an
* obscure message.
*/
- (void)fprintf(stderr, "arithmetic: bug: inconsistent penalties\n");
- exit(1);
+ errx(1, "arithmetic: bug: inconsistent penalties.");
/* NOTREACHED */
}
/* Return an index for the character op, which is one of [+-x/]. */
+int
opnum(op)
int op;
{
char *p;
- if (op == 0 || (p = index(keylist, op)) == NULL) {
- (void)fprintf(stderr,
- "arithmetic: bug: op %c not in keylist %s\n", op, keylist);
- exit(1);
- }
+ if (op == 0 || (p = strchr(keylist, op)) == NULL)
+ errx(1, "arithmetic: bug: op %c not in keylist %s",
+ op, keylist);
return(p - keylist);
}
/* Print usage message and quit. */
+void
usage()
{
- (void)fprintf(stderr, "usage: arithmetic [-o +-x/] [-r range]\n");
+ extern char *__progname; /* from crt0.o */
+
+ (void)fprintf(stderr, "usage: %s [-o +-x/] [-r range]\n",
+ __progname);
exit(1);
}