ff07f543 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: factorial.c,v 1.4 2004/04/08 01:36:15 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 | |
ff07f543 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
3235b49b |
32 | #include "config.h" |
33 | |
34 | #include <ctype.h> |
35 | #include <limits.h> |
ff07f543 |
36 | #include <stdio.h> |
37 | #include <stdlib.h> |
38 | |
4afa7ff8 |
39 | #include <mLib/mdwopt.h> |
ff07f543 |
40 | #include <mLib/quis.h> |
41 | #include <mLib/report.h> |
42 | |
3235b49b |
43 | #include "mpint.h" |
ff07f543 |
44 | #include "mpmul.h" |
3235b49b |
45 | #include "mptext.h" |
ff07f543 |
46 | |
47 | /*----- Main code ---------------------------------------------------------*/ |
48 | |
3235b49b |
49 | static void usage(FILE *fp) |
50 | { |
51 | pquis(fp, "Usage: $ [-r radix] integer\n"); |
52 | } |
53 | |
54 | static void version(FILE *fp) |
55 | { |
56 | pquis(fp, "$, Catacomb version " VERSION "\n"); |
57 | } |
58 | |
59 | static void help(FILE *fp) |
60 | { |
61 | version(fp); |
62 | putc('\n', fp); |
63 | usage(fp); |
64 | fputs("\n\ |
65 | Prints the factorial of the given integer on its output. Input may be\n\ |
66 | in decimal (the default), octal with preceding zero, hex with preceding\n\ |
67 | `0x', or any base N between 2 and 62 inclusive with preceding `N_'.\n\ |
68 | Output may be in any base between 2 and 62; the default is base 10. For\n\ |
69 | bases between 11 and 36 inclusive, lowercase letters of either case are\n\ |
70 | used as additional digits with values 10 upwards; lowercase is always\n\ |
71 | used for output. For bases between 37 and 62 inclusive, lowercase letters\n\ |
72 | have lower value than uppercase; hence `a' has the value 10, while `A' has\n\ |
73 | the value 36.\n\ |
74 | \n\ |
75 | Options provided:\n\ |
76 | \n\ |
77 | -h, --help Display this help message.\n\ |
78 | -v, --version Display the version number.\n\ |
79 | -u, --usage Display a usage message.\n\ |
80 | \n\ |
81 | -r, --radix=N Write output in base N.\n\ |
82 | ", fp); |
83 | } |
84 | |
ff07f543 |
85 | int main(int argc, char *argv[]) |
86 | { |
87 | unsigned long x; |
4afa7ff8 |
88 | int r = 10; |
ff07f543 |
89 | char *p; |
3235b49b |
90 | mp *f, *ulmax, *xx; |
91 | unsigned fl = 0; |
92 | |
93 | #define f_bogus 1u |
ff07f543 |
94 | |
95 | ego(argv[0]); |
96 | |
4afa7ff8 |
97 | for (;;) { |
98 | static const struct option opt[] = { |
3235b49b |
99 | { "help", 0, 0, 'h' }, |
100 | { "version", 0, 0, 'v' }, |
101 | { "usage", 0, 0, 'u' }, |
4afa7ff8 |
102 | { "radix", OPTF_ARGREQ, 0, 'r' }, |
103 | { 0, 0, 0, 0 } |
104 | }; |
3235b49b |
105 | int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0); |
4afa7ff8 |
106 | if (i < 0) |
107 | break; |
108 | switch (i) { |
3235b49b |
109 | case 'h': |
110 | help(stdout); |
111 | exit(0); |
112 | case 'v': |
113 | version(stdout); |
114 | exit(0); |
115 | case 'u': |
116 | usage(stdout); |
117 | exit(0); |
4afa7ff8 |
118 | case 'r': |
119 | r = atoi(optarg); |
3235b49b |
120 | if (r < 2 || r > 62) |
4afa7ff8 |
121 | die(EXIT_FAILURE, "bad radix `%s'", optarg); |
122 | break; |
123 | default: |
3235b49b |
124 | fl |= f_bogus; |
125 | break; |
4afa7ff8 |
126 | } |
127 | } |
128 | |
3235b49b |
129 | if (optind + 1 != argc || (fl & f_bogus)) { |
130 | usage(stderr); |
ff07f543 |
131 | exit(EXIT_FAILURE); |
132 | } |
3235b49b |
133 | ulmax = mp_fromulong(MP_NEW, ULONG_MAX); |
134 | p = argv[optind]; |
135 | while (isspace((unsigned char)*p)) |
136 | p++; |
137 | xx = mp_readstring(MP_NEW, argv[optind], &p, 0); |
138 | while (isspace((unsigned char)*p)) |
139 | p++; |
140 | if (!xx || *p || MP_CMP(xx, >, ulmax)) |
4afa7ff8 |
141 | die(EXIT_FAILURE, "bad integer `%s'", argv[optind]); |
3235b49b |
142 | x = mp_toulong(xx); |
143 | mp_drop(xx); |
144 | mp_drop(ulmax); |
ff07f543 |
145 | f = mp_factorial(x); |
4afa7ff8 |
146 | mp_writefile(f, stdout, r); |
ff07f543 |
147 | fputc('\n', stdout); |
148 | mp_drop(f); |
149 | return (0); |
150 | } |
151 | |
152 | /*----- That's all, folks -------------------------------------------------*/ |