3 * Example Fibonacci number computation
5 * (c) 2013 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/mdwopt.h>
38 #include <mLib/quis.h>
39 #include <mLib/report.h>
44 #include "mp-fibonacci.h"
46 /*----- Main code ---------------------------------------------------------*/
48 static void usage(FILE *fp)
49 { pquis(fp, "Usage: $ [-r RADIX] INTEGER\n"); }
51 static void version(FILE *fp)
52 { pquis(fp, "$, Catacomb version " VERSION "\n"); }
54 static void help(FILE *fp)
60 Prints the Nth Fibonacci number for a given inteer N. Input may be in\n\
61 decimal (the default), octal with preceding zero, hex with preceding `0x',\n\
62 or any base N between 2 and 62 inclusive with preceding `N_'. Output\n\
63 may be in any base between 2 and 62; the default is base 10. For bases\n\
64 between 11 and 36 inclusive, lowercase letters of either case are used as\n\
65 additional digits with values 10 upwards; lowercase is always used for\n\
66 output. For bases between 37 and 62 inclusive, lowercase letters have\n\
67 lower value than uppercase; hence `a' has the value 10, while `A' has the\n\
72 -h, --help Display this help message.\n\
73 -v, --version Display the version number.\n\
74 -u, --usage Display a usage message.\n\
76 -r, --radix=N Write output in base N.\n\
80 int main(int argc, char *argv[])
85 mp *f, *lmin, *lmax, *nn;
93 static const struct option opt[] = {
94 { "help", 0, 0, 'h' },
95 { "version", 0, 0, 'v' },
96 { "usage", 0, 0, 'u' },
97 { "radix", OPTF_ARGREQ, 0, 'r' },
100 int i = mdwopt(argc, argv, "hvur:", opt, 0, 0, 0);
116 die(EXIT_FAILURE, "bad radix `%s'", optarg);
124 if (optind + 1 != argc || (fl & f_bogus)) {
128 lmin = mp_fromlong(MP_NEW, LONG_MIN);
129 lmax = mp_fromlong(MP_NEW, LONG_MAX);
131 while (isspace((unsigned char)*p)) p++;
132 nn = mp_readstring(MP_NEW, argv[optind], &p, 0);
133 while (isspace((unsigned char)*p)) p++;
134 if (!nn || *p || MP_CMP(lmin, >, nn) || MP_CMP(nn, >, lmax))
135 die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
137 mp_drop(nn); mp_drop(lmin); mp_drop(lmax);
139 mp_writefile(f, stdout, r);
145 /*----- That's all, folks -------------------------------------------------*/