3 * $Id: factorial.c,v 1.3 2002/01/13 19:51:59 mdw Exp $
5 * Example factorial computation
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: factorial.c,v $
33 * Revision 1.3 2002/01/13 19:51:59 mdw
34 * Provide proper help and options parsing. Allow more bases. Use
35 * @mptext@ to read integers for the better base support.
37 * Revision 1.2 2001/06/16 13:22:59 mdw
38 * Added command-line option to select output radix.
40 * Revision 1.1 2000/07/09 21:30:49 mdw
41 * Demo program to compute factorials.
45 /*----- Header files ------------------------------------------------------*/
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
62 /*----- Main code ---------------------------------------------------------*/
64 static void usage(FILE *fp)
66 pquis(fp, "Usage: $ [-r radix] integer\n");
69 static void version(FILE *fp)
71 pquis(fp, "$, Catacomb version " VERSION "\n");
74 static void help(FILE *fp)
80 Prints the factorial of the given integer on its output. Input may be\n\
81 in decimal (the default), octal with preceding zero, hex with preceding\n\
82 `0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\
83 Output may be in any base between 2 and 62; the default is base 10. For\n\
84 bases between 11 and 36 inclusive, lowercase letters of either case are\n\
85 used as additional digits with values 10 upwards; lowercase is always\n\
86 used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
87 have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
92 -h, --help Display this help message.\n\
93 -v, --version Display the version number.\n\
94 -u, --usage Display a usage message.\n\
96 -r, --radix=N Write output in base N.\n\
100 int main(int argc, char *argv[])
113 static const struct option opt[] = {
114 { "help", 0, 0, 'h' },
115 { "version", 0, 0, 'v' },
116 { "usage", 0, 0, 'u' },
117 { "radix", OPTF_ARGREQ, 0, 'r' },
120 int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
136 die(EXIT_FAILURE, "bad radix `%s'", optarg);
144 if (optind + 1 != argc || (fl & f_bogus)) {
148 ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
150 while (isspace((unsigned char)*p))
152 xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
153 while (isspace((unsigned char)*p))
155 if (!xx || *p || MP_CMP(xx, >, ulmax))
156 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
161 mp_writefile(f, stdout, r);
167 /*----- That's all, folks -------------------------------------------------*/