/* -*-c-*-
*
- * $Id: factorial.c,v 1.1 2000/07/09 21:30:49 mdw Exp $
+ * $Id: factorial.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
*
* Example factorial computation
*
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
- *
- * $Log: factorial.c,v $
- * Revision 1.1 2000/07/09 21:30:49 mdw
- * Demo program to compute factorials.
- *
- */
-
/*----- Header files ------------------------------------------------------*/
+#include "config.h"
+
+#include <ctype.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
+#include <mLib/mdwopt.h>
#include <mLib/quis.h>
#include <mLib/report.h>
+#include "mpint.h"
#include "mpmul.h"
+#include "mptext.h"
/*----- Main code ---------------------------------------------------------*/
+static void usage(FILE *fp)
+{
+ pquis(fp, "Usage: $ [-r radix] integer\n");
+}
+
+static void version(FILE *fp)
+{
+ pquis(fp, "$, Catacomb version " VERSION "\n");
+}
+
+static void help(FILE *fp)
+{
+ version(fp);
+ putc('\n', fp);
+ usage(fp);
+ fputs("\n\
+Prints the factorial of the given integer on its output. Input may be\n\
+in decimal (the default), octal with preceding zero, hex with preceding\n\
+`0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
+Output may be in any base between 2 and 62; the default is base 10. For\n\
+bases between 11 and 36 inclusive, lowercase letters of either case are\n\
+used as additional digits with values 10 upwards; lowercase is always\n\
+used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
+have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
+the value 36.\n\
+\n\
+Options provided:\n\
+\n\
+-h, --help Display this help message.\n\
+-v, --version Display the version number.\n\
+-u, --usage Display a usage message.\n\
+\n\
+-r, --radix=N Write output in base N.\n\
+", fp);
+}
+
int main(int argc, char *argv[])
{
unsigned long x;
+ int r = 10;
char *p;
- mp *f;
+ mp *f, *ulmax, *xx;
+ unsigned fl = 0;
+
+#define f_bogus 1u
ego(argv[0]);
- if (argc != 2) {
- pquis(stderr, "Usage: $ integer\n");
+ for (;;) {
+ static const struct option opt[] = {
+ { "help", 0, 0, 'h' },
+ { "version", 0, 0, 'v' },
+ { "usage", 0, 0, 'u' },
+ { "radix", OPTF_ARGREQ, 0, 'r' },
+ { 0, 0, 0, 0 }
+ };
+ int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
+ if (i < 0)
+ break;
+ switch (i) {
+ case 'h':
+ help(stdout);
+ exit(0);
+ case 'v':
+ version(stdout);
+ exit(0);
+ case 'u':
+ usage(stdout);
+ exit(0);
+ case 'r':
+ r = atoi(optarg);
+ if (r < 2 || r > 62)
+ die(EXIT_FAILURE, "bad radix `%s'", optarg);
+ break;
+ default:
+ fl |= f_bogus;
+ break;
+ }
+ }
+
+ if (optind + 1 != argc || (fl & f_bogus)) {
+ usage(stderr);
exit(EXIT_FAILURE);
}
- x = strtoul(argv[1], &p, 0);
- if (*p)
- die(EXIT_FAILURE, "bad integer `%s'", argv[1]);
+ ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
+ p = argv[optind];
+ while (isspace((unsigned char)*p))
+ p++;
+ xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
+ while (isspace((unsigned char)*p))
+ p++;
+ if (!xx || *p || MP_CMP(xx, >, ulmax))
+ die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
+ x = mp_toulong(xx);
+ mp_drop(xx);
+ mp_drop(ulmax);
f = mp_factorial(x);
- mp_writefile(f, stdout, 10);
+ mp_writefile(f, stdout, r);
fputc('\n', stdout);
mp_drop(f);
return (0);