Allow user to choose base.

This commit is contained in:
Nicolás A. Ortega 2016-12-14 19:06:33 +01:00
parent 5e45656e1a
commit 656fee720e
No known key found for this signature in database
GPG Key ID: 614272579C2070D1

View File

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <stdbool.h> #include <stdbool.h>
#include <getopt.h> #include <unistd.h>
#include <gmp.h> #include <gmp.h>
#include "list.h" #include "list.h"
@ -16,9 +16,10 @@ void leave();
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
bool f_help = false, f_version = false, f_quiet = false; bool f_help = false, f_version = false, f_quiet = false;
int base = 10;
int c; int c;
while((c = getopt(argc, argv, "hvq")) != -1) { while((c = getopt(argc, argv, "hvqb:")) != -1) {
switch(c) { switch(c) {
case 'h': case 'h':
f_help = true; f_help = true;
@ -29,6 +30,15 @@ int main(int argc, char *argv[]) {
case 'q': case 'q':
f_quiet = true; f_quiet = true;
break; break;
case 'b':
base = atoi(optarg);
if(base < 2 || base > 62) {
fprintf(stderr,
"Invalid base `%d'. Base must be between 2 and 62\n",
base);
exit(1);
}
break;
default: default:
printUsage(argv[0]); printUsage(argv[0]);
exit(1); exit(1);
@ -40,6 +50,7 @@ int main(int argc, char *argv[]) {
puts(" -h print this help information"); puts(" -h print this help information");
puts(" -v print version number of program"); puts(" -v print version number of program");
puts(" -q quiet mode"); puts(" -q quiet mode");
puts(" -b <base> base in which to print primes between 2 and 62");
return 0; return 0;
} else if(f_version) { } else if(f_version) {
printf("Indivisible %s\n", VERSION); printf("Indivisible %s\n", VERSION);
@ -66,7 +77,7 @@ int main(int argc, char *argv[]) {
mpz_set_ui(num, 2); mpz_set_ui(num, 2);
addToList(&primes, num); addToList(&primes, num);
if(!f_quiet) { if(!f_quiet) {
if(mpz_out_str(stdout, 10, num) == 0) { if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n"); fprintf(stderr, "Could not print to `stdout'!\n");
exit(1); exit(1);
} }
@ -94,7 +105,7 @@ int main(int argc, char *argv[]) {
// `num' is a prime so we add it to the list and print it // `num' is a prime so we add it to the list and print it
addToList(&primes, num); addToList(&primes, num);
if(!f_quiet) { if(!f_quiet) {
if(mpz_out_str(stdout, 10, num) == 0) { if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n"); fprintf(stderr, "Could not print to `stdout'!\n");
exit(1); exit(1);
} }
@ -119,7 +130,7 @@ nextPrime:
} }
void printUsage(char *progName) { void printUsage(char *progName) {
printf("%s [-v | -h | -q]\n", progName); printf("%s [options...]\n", progName);
} }
void leave() { run = false; } void leave() { run = false; }