From: mdw Date: Sat, 16 Jun 2001 13:22:59 +0000 (+0000) Subject: Added command-line option to select output radix. X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/commitdiff_plain/4afa7ff852b99306b4d82d7d9c295f73169ac208 Added command-line option to select output radix. --- diff --git a/factorial.c b/factorial.c index ac2f3fb..71f2ad2 100644 --- a/factorial.c +++ b/factorial.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: factorial.c,v 1.1 2000/07/09 21:30:49 mdw Exp $ + * $Id: factorial.c,v 1.2 2001/06/16 13:22:59 mdw Exp $ * * Example factorial computation * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: factorial.c,v $ + * Revision 1.2 2001/06/16 13:22:59 mdw + * Added command-line option to select output radix. + * * Revision 1.1 2000/07/09 21:30:49 mdw * Demo program to compute factorials. * @@ -40,6 +43,7 @@ #include #include +#include #include #include @@ -50,20 +54,40 @@ int main(int argc, char *argv[]) { unsigned long x; + int r = 10; char *p; mp *f; ego(argv[0]); - if (argc != 2) { - pquis(stderr, "Usage: $ integer\n"); + for (;;) { + static const struct option opt[] = { + { "radix", OPTF_ARGREQ, 0, 'r' }, + { 0, 0, 0, 0 } + }; + int i = mdwopt(argc, argv, "r:", opt, 0, 0, 0); + if (i < 0) + break; + switch (i) { + case 'r': + r = atoi(optarg); + if (r < 2 || r > 36) + die(EXIT_FAILURE, "bad radix `%s'", optarg); + break; + default: + exit(EXIT_FAILURE); + } + } + + if (optind + 1 != argc) { + pquis(stderr, "Usage: $ [-r radix] integer\n"); exit(EXIT_FAILURE); } - x = strtoul(argv[1], &p, 0); + x = strtoul(argv[optind], &p, 0); if (*p) - die(EXIT_FAILURE, "bad integer `%s'", argv[1]); + die(EXIT_FAILURE, "bad integer `%s'", argv[optind]); f = mp_factorial(x); - mp_writefile(f, stdout, 10); + mp_writefile(f, stdout, r); fputc('\n', stdout); mp_drop(f); return (0);