/* -*-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
*
/*----- 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.
*
#include <stdio.h>
#include <stdlib.h>
+#include <mLib/mdwopt.h>
#include <mLib/quis.h>
#include <mLib/report.h>
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);