| 3 |
#include <stdio.h> |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
#include <stdlib.h> |
| 5 |
#include <limits.h> |
#include <limits.h> |
| 6 |
|
#include <errno.h> |
| 7 |
|
|
| 8 |
#define BOOL int |
#define BOOL int |
| 9 |
#define TRUE 1 |
#define TRUE 1 |
| 16 |
BOOL is_prime(long int number) |
BOOL is_prime(long int number) |
| 17 |
{ |
{ |
| 18 |
int i; |
int i; |
| 19 |
|
|
| 20 |
|
/* negative values, 0 and 1 are never prime numbers */ |
| 21 |
|
if (number < 2) return FALSE; |
| 22 |
|
|
| 23 |
|
/* check all numbers 2..sqrt(number) for being a prime number */ |
| 24 |
for (i=2; i*i <= number; i++) { |
for (i=2; i*i <= number; i++) { |
| 25 |
if ((number % i) == 0) |
if ((number % i) == 0) |
| 26 |
return FALSE; |
return FALSE; |
| 28 |
return TRUE; |
return TRUE; |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
/* convert from string to long, with error checking */ |
/* convert from string to long int, with error checking */ |
| 32 |
long int convert_number(const char *nptr) { |
long int convert_number(const char *nptr) { |
| 33 |
long int number = strtol(nptr, (char **)NULL, 10); |
|
| 34 |
if (number == LONG_MAX) { |
errno = 0; |
| 35 |
PRINTERROR("Number '%s' is not in range of 'long int'."); |
char * endptr; |
| 36 |
|
long int number = strtol(nptr, &endptr, 10); |
| 37 |
|
|
| 38 |
|
/* invalid characters? */ |
| 39 |
|
if (*endptr != '\0') { |
| 40 |
|
char message[254]; |
| 41 |
|
snprintf(message, 256, "Could not convert '%s' to a valid number.", nptr); |
| 42 |
|
PRINTERROR(message); |
| 43 |
|
exit(EXIT_FAILURE); |
| 44 |
} |
} |
| 45 |
|
|
| 46 |
|
/* invalid range? */ |
| 47 |
|
/* if (number == LONG_MAX || number == LONG_MIN) { */ |
| 48 |
|
if (errno == ERANGE) { |
| 49 |
|
char message[254]; |
| 50 |
|
snprintf(message, 256, "Number is not in range of 'long int': %s", nptr); |
| 51 |
|
PRINTERROR(message); |
| 52 |
|
exit(EXIT_FAILURE); |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
return number; |
| 56 |
|
|
| 57 |
} |
} |
| 58 |
|
|
| 59 |
int main(int argc, char * argv[]) |
int main(int argc, char * argv[]) |
| 61 |
|
|
| 62 |
if (argc == 1) { |
if (argc == 1) { |
| 63 |
PRINTERROR("No arguments given."); |
PRINTERROR("No arguments given."); |
| 64 |
return -1; |
exit(EXIT_FAILURE); |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
/* (3) range mode */ |
/* (3) range mode */ |
| 68 |
if (argc > 2) { |
if (argc > 2) { |
| 69 |
int i, j; |
long int i, j; |
| 70 |
j = atoi(argv[2]); |
i = convert_number(argv[1]); |
| 71 |
for (i = atoi(argv[1]); i< j; i++) { |
j = convert_number(argv[2]); |
| 72 |
|
for (i; i< j; i++) { |
| 73 |
PRINTPRIME(i); |
PRINTPRIME(i); |
| 74 |
} |
} |
| 75 |
|
|
| 81 |
|
|
| 82 |
/* (1) test-single-number mode: first argument is not a filename */ |
/* (1) test-single-number mode: first argument is not a filename */ |
| 83 |
if (fp == NULL) { |
if (fp == NULL) { |
| 84 |
/* PRINTPRIME(atoi(argv[1])); */ |
long int number = convert_number(argv[1]); |
| 85 |
PRINTPRIME(convert_number(argv[1])); |
PRINTPRIME(number); |
| 86 |
|
|
| 87 |
/* (2) file mode: read numbers from file */ |
/* (2) file mode: read numbers from file */ |
| 88 |
} else { |
} else { |
| 89 |
char num[11]; |
char entry[11]; |
| 90 |
while (fgets(num, 11, fp)) { |
while (fgets(entry, 11, fp)) { |
| 91 |
printf("raw: %s\n", num); |
long int number = convert_number(entry); |
| 92 |
printf("num: %i\n", atoi(num)); |
PRINTPRIME(number); |
|
PRINTPRIME(atoi(num)); |
|
| 93 |
} |
} |
| 94 |
fclose(fp); |
fclose(fp); |
| 95 |
} |
} |