2 |
|
|
3 |
#include <stdio.h> |
#include <stdio.h> |
4 |
#include <stdlib.h> |
#include <stdlib.h> |
5 |
|
#include <math.h> |
6 |
#include <limits.h> |
#include <limits.h> |
7 |
#include <errno.h> |
#include <errno.h> |
8 |
|
|
9 |
|
#ifdef _MSC_VER |
10 |
|
#define snprintf _snprintf |
11 |
|
#endif |
12 |
|
|
13 |
#define BOOL int |
#define BOOL int |
14 |
#define TRUE 1 |
#define TRUE 1 |
15 |
#define FALSE 0 |
#define FALSE 0 |
24 |
/* check for prime number */ |
/* check for prime number */ |
25 |
BOOL is_prime(long int number) |
BOOL is_prime(long int number) |
26 |
{ |
{ |
27 |
int i; |
int i,sq; |
|
|
|
28 |
/* negative values, 0 and 1 are never prime numbers */ |
/* negative values, 0 and 1 are never prime numbers */ |
29 |
if (number < 2) return FALSE; |
if (number < 2) return FALSE; |
30 |
|
sq=sqrt(number); |
31 |
|
|
32 |
/* check all numbers 2..sqrt(number) for being a prime number */ |
/* check all numbers 2..sqrt(number) for being a prime number */ |
33 |
for (i=2; i*i <= number; i++) { |
for (i=2; i <= sq; i++) { |
34 |
if ((number % i) == 0) |
if ((number % i) == 0) |
35 |
return FALSE; |
return FALSE; |
36 |
} |
} |
38 |
} |
} |
39 |
|
|
40 |
/* convert from string to long int, with error checking */ |
/* convert from string to long int, with error checking */ |
41 |
long int convert_number(const char *nptr) { |
long int convert_number(const char *nptr, BOOL warn) { |
42 |
|
|
43 |
char * endptr; |
char * endptr; |
|
long int number = strtol(nptr, &endptr, 10); |
|
|
|
|
44 |
errno = 0; |
errno = 0; |
45 |
|
long int number; |
46 |
|
|
47 |
|
number = strtol(nptr, &endptr, 10); |
48 |
|
|
49 |
/* invalid characters? */ |
/* invalid characters? */ |
50 |
if (*endptr != '\0') { |
if (*endptr != '\0') { |
51 |
char message[254]; |
char message[256]; |
52 |
snprintf(message, 256, "Could not convert '%s' to a valid (integer) number.", nptr); |
snprintf(message, 255, "Could not convert '%s' to a valid (integer) number.", nptr); |
53 |
PRINTERROR(message); |
if(warn) |
54 |
exit(EXIT_FAILURE); |
PRINTWARNING(message); |
55 |
|
else{ |
56 |
|
PRINTERROR(message); |
57 |
|
exit(EXIT_FAILURE); |
58 |
|
} |
59 |
} |
} |
60 |
|
|
61 |
/* invalid range? */ |
/* invalid range? */ |
62 |
/* if (number == LONG_MAX || number == LONG_MIN) { */ |
/* if (number == LONG_MAX || number == LONG_MIN) { */ |
63 |
if (errno == ERANGE) { |
if (errno == ERANGE) { |
64 |
char message[254]; |
char message[256]; |
65 |
snprintf(message, 256, "Number is not in range of 'long int': %s", nptr); |
snprintf(message, 255, "Number is not in range of 'long int': %s", nptr); |
66 |
PRINTERROR(message); |
if(warn) |
67 |
exit(EXIT_FAILURE); |
PRINTWARNING(message); |
68 |
|
else{ |
69 |
|
PRINTERROR(message); |
70 |
|
exit(EXIT_FAILURE); |
71 |
|
} |
72 |
} |
} |
73 |
|
|
74 |
return number; |
return number; |
86 |
/* (3) range mode */ |
/* (3) range mode */ |
87 |
if (argc > 2) { |
if (argc > 2) { |
88 |
long int i, j; |
long int i, j; |
89 |
i = convert_number(argv[1]); |
i = convert_number(argv[1],FALSE); |
90 |
j = convert_number(argv[2]); |
j = convert_number(argv[2],FALSE); |
91 |
for (i; i< j; i++) { |
for (i; i< j; i++) { |
92 |
PRINTPRIME(i); |
PRINTPRIME(i); |
93 |
} |
} |
94 |
|
|
95 |
/* other modes */ |
/* other modes */ |
96 |
} else { |
} else { |
97 |
|
int err; |
98 |
/* try to open file for reading */ |
/* try to open file for reading */ |
99 |
FILE * fp = fopen(argv[1], "r"); |
FILE * fp = fopen(argv[1], "r"); |
100 |
|
err=errno; |
101 |
/* (1) test-single-number mode: first argument is not a filename */ |
/* (1) test-single-number mode: first argument is not a filename */ |
102 |
if (fp == NULL) { |
if (fp == NULL) { |
103 |
long int number = convert_number(argv[1]); |
long int number = convert_number(argv[1],TRUE); |
104 |
|
if(err){ |
105 |
|
fprintf(stderr, "%s: %s\n", argv[1], strerror(err)); |
106 |
|
return -1; |
107 |
|
} |
108 |
PRINTPRIME(number); |
PRINTPRIME(number); |
109 |
|
|
110 |
/* (2) file mode: read numbers from file */ |
/* (2) file mode: read numbers from file */ |
147 |
} |
} |
148 |
|
|
149 |
/* finally: prime number calculation and output */ |
/* finally: prime number calculation and output */ |
150 |
number = convert_number(line); |
number = convert_number(line,FALSE); |
151 |
PRINTPRIME(number); |
PRINTPRIME(number); |
152 |
|
|
153 |
} |
} |