Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
af79d206d3 | |||
4b034ce5e3 | |||
f146dbf11c | |||
656fee720e | |||
5e45656e1a |
@ -23,3 +23,7 @@ Change Log
|
|||||||
- Use `size_t' instead of `unsigned long long int'.
|
- Use `size_t' instead of `unsigned long long int'.
|
||||||
- Minor optimizations to the algorithm.
|
- Minor optimizations to the algorithm.
|
||||||
- Added commandline argument parsing.
|
- Added commandline argument parsing.
|
||||||
|
- v0.6: User Control
|
||||||
|
- Allow user to choose base in which the prime numbers are printed.
|
||||||
|
- Give option for primes to be saved to a file upon exit.
|
||||||
|
- Free memory and leave instead of emergency exit.
|
||||||
|
@ -24,10 +24,10 @@ set(SRCS
|
|||||||
|
|
||||||
# Define the C flags.
|
# Define the C flags.
|
||||||
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
|
||||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} -g -O0")
|
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
|
||||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} -O3")
|
set(CMAKE_C_FLAGS_RELEASE "-O3")
|
||||||
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS} -g -O3")
|
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3")
|
||||||
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS} -Os")
|
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
|
||||||
|
|
||||||
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
|
if(NOT CMAKE_BUILD_TYPE MATCHES Debug AND NOT CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
|
||||||
add_definitions(-DNDEBUG)
|
add_definitions(-DNDEBUG)
|
||||||
|
66
src/main.c
66
src/main.c
@ -2,12 +2,12 @@
|
|||||||
#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"
|
||||||
|
|
||||||
#define VERSION "v0.5"
|
#define VERSION "v0.6"
|
||||||
|
|
||||||
static bool run;
|
static bool run;
|
||||||
|
|
||||||
@ -15,10 +15,14 @@ void printUsage(char *progName);
|
|||||||
void leave();
|
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;
|
||||||
|
char *file = NULL;
|
||||||
|
|
||||||
int c;
|
int c;
|
||||||
while((c = getopt(argc, argv, "hvq")) != -1) {
|
while((c = getopt(argc, argv, "hvqb:f:")) != -1) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
f_help = true;
|
f_help = true;
|
||||||
@ -29,6 +33,18 @@ 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;
|
||||||
|
case 'f':
|
||||||
|
file = optarg;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
printUsage(argv[0]);
|
printUsage(argv[0]);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -40,6 +56,8 @@ 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");
|
||||||
|
puts(" -f <file> file to save primes to");
|
||||||
return 0;
|
return 0;
|
||||||
} else if(f_version) {
|
} else if(f_version) {
|
||||||
printf("Indivisible %s\n", VERSION);
|
printf("Indivisible %s\n", VERSION);
|
||||||
@ -66,9 +84,9 @@ 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);
|
goto releaseMemory;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
@ -94,9 +112,9 @@ 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);
|
goto releaseMemory;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
@ -107,10 +125,38 @@ nextPrime:
|
|||||||
} while(run);
|
} while(run);
|
||||||
|
|
||||||
printf("Found %zu primes.\n", primes.end);
|
printf("Found %zu primes.\n", primes.end);
|
||||||
puts("Clearing memory...");
|
|
||||||
// Clear GMP variables
|
// Clear GMP variables
|
||||||
mpz_clear(halfNum);
|
mpz_clear(halfNum);
|
||||||
mpz_clear(num);
|
mpz_clear(num);
|
||||||
|
|
||||||
|
if(file != NULL) {
|
||||||
|
FILE *outFile = fopen(file, "w+");
|
||||||
|
if(outFile == NULL) {
|
||||||
|
fprintf(stderr, "Failed create file `%s'.\n", file);
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
printf("Writing primes to `%s'...\n", file);
|
||||||
|
puts("0%");
|
||||||
|
for(size_t i = 0; i < primes.end; ++i) {
|
||||||
|
if(mpz_out_str(outFile, base, primes.list[i]) == 0) {
|
||||||
|
fprintf(stderr, "Error occurred while writing to file `%s'.\n", file);
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
fprintf(outFile, "\n");
|
||||||
|
if(i == primes.end / 4) puts("25%");
|
||||||
|
else if(i == primes.end / 2) puts("50%");
|
||||||
|
else if(i == primes.end * 3 / 4) puts("75%");
|
||||||
|
}
|
||||||
|
if(fclose(outFile) != 0) {
|
||||||
|
fprintf(stderr, "Failed to close file `%s'.\n", file);
|
||||||
|
goto releaseMemory;
|
||||||
|
}
|
||||||
|
puts("100%");
|
||||||
|
puts("Finished writing primes.");
|
||||||
|
}
|
||||||
|
|
||||||
|
releaseMemory:
|
||||||
|
puts("Clearing memory...");
|
||||||
// Deinitialize the list
|
// Deinitialize the list
|
||||||
deInitList(&primes);
|
deInitList(&primes);
|
||||||
|
|
||||||
@ -119,7 +165,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; }
|
||||||
|
Reference in New Issue
Block a user