Fix bit operations. Test them (a bit) better.
[u/mdw/catacomb] / factorial.c
CommitLineData
ff07f543 1/* -*-c-*-
2 *
3235b49b 3 * $Id: factorial.c,v 1.3 2002/01/13 19:51:59 mdw Exp $
ff07f543 4 *
5 * Example factorial computation
6 *
7 * (c) 2000 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: factorial.c,v $
3235b49b 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.
36 *
4afa7ff8 37 * Revision 1.2 2001/06/16 13:22:59 mdw
38 * Added command-line option to select output radix.
39 *
ff07f543 40 * Revision 1.1 2000/07/09 21:30:49 mdw
41 * Demo program to compute factorials.
42 *
43 */
44
45/*----- Header files ------------------------------------------------------*/
46
3235b49b 47#include "config.h"
48
49#include <ctype.h>
50#include <limits.h>
ff07f543 51#include <stdio.h>
52#include <stdlib.h>
53
4afa7ff8 54#include <mLib/mdwopt.h>
ff07f543 55#include <mLib/quis.h>
56#include <mLib/report.h>
57
3235b49b 58#include "mpint.h"
ff07f543 59#include "mpmul.h"
3235b49b 60#include "mptext.h"
ff07f543 61
62/*----- Main code ---------------------------------------------------------*/
63
3235b49b 64static void usage(FILE *fp)
65{
66 pquis(fp, "Usage: $ [-r radix] integer\n");
67}
68
69static void version(FILE *fp)
70{
71 pquis(fp, "$, Catacomb version " VERSION "\n");
72}
73
74static void help(FILE *fp)
75{
76 version(fp);
77 putc('\n', fp);
78 usage(fp);
79 fputs("\n\
80Prints the factorial of the given integer on its output. Input may be\n\
81in 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\
83Output may be in any base between 2 and 62; the default is base 10. For\n\
84bases between 11 and 36 inclusive, lowercase letters of either case are\n\
85used as additional digits with values 10 upwards; lowercase is always\n\
86used for output. For bases between 37 and 62 inclusive, lowercase letters\n\
87have lower value than uppercase; hence `a' has the value 10, while `A' has\n\
88the value 36.\n\
89\n\
90Options provided:\n\
91\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\
95\n\
96-r, --radix=N Write output in base N.\n\
97", fp);
98}
99
ff07f543 100int main(int argc, char *argv[])
101{
102 unsigned long x;
4afa7ff8 103 int r = 10;
ff07f543 104 char *p;
3235b49b 105 mp *f, *ulmax, *xx;
106 unsigned fl = 0;
107
108#define f_bogus 1u
ff07f543 109
110 ego(argv[0]);
111
4afa7ff8 112 for (;;) {
113 static const struct option opt[] = {
3235b49b 114 { "help", 0, 0, 'h' },
115 { "version", 0, 0, 'v' },
116 { "usage", 0, 0, 'u' },
4afa7ff8 117 { "radix", OPTF_ARGREQ, 0, 'r' },
118 { 0, 0, 0, 0 }
119 };
3235b49b 120 int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
4afa7ff8 121 if (i < 0)
122 break;
123 switch (i) {
3235b49b 124 case 'h':
125 help(stdout);
126 exit(0);
127 case 'v':
128 version(stdout);
129 exit(0);
130 case 'u':
131 usage(stdout);
132 exit(0);
4afa7ff8 133 case 'r':
134 r = atoi(optarg);
3235b49b 135 if (r < 2 || r > 62)
4afa7ff8 136 die(EXIT_FAILURE, "bad radix `%s'", optarg);
137 break;
138 default:
3235b49b 139 fl |= f_bogus;
140 break;
4afa7ff8 141 }
142 }
143
3235b49b 144 if (optind + 1 != argc || (fl & f_bogus)) {
145 usage(stderr);
ff07f543 146 exit(EXIT_FAILURE);
147 }
3235b49b 148 ulmax = mp_fromulong(MP_NEW, ULONG_MAX);
149 p = argv[optind];
150 while (isspace((unsigned char)*p))
151 p++;
152 xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
153 while (isspace((unsigned char)*p))
154 p++;
155 if (!xx || *p || MP_CMP(xx, >, ulmax))
4afa7ff8 156 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
3235b49b 157 x = mp_toulong(xx);
158 mp_drop(xx);
159 mp_drop(ulmax);
ff07f543 160 f = mp_factorial(x);
4afa7ff8 161 mp_writefile(f, stdout, r);
ff07f543 162 fputc('\n', stdout);
163 mp_drop(f);
164 return (0);
165}
166
167/*----- That's all, folks -------------------------------------------------*/