1 |
|
/* $Id$ */ |
2 |
|
|
3 |
#include <stdio.h> |
#include <stdio.h> |
4 |
|
|
5 |
#define PRINTPRIME(x) if(is_prime(x)) printf("%i\n", x) |
#define PRINTPRIME(x) if(is_prime(x)) printf("%i\n", x) |
6 |
#define PRINTERROR(message) printf("ERROR: %s\n", message) |
#define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message) |
7 |
|
|
8 |
// check for prime number |
/* check for prime number */ |
9 |
int is_prime(int number) |
int is_prime(int number) |
10 |
{ |
{ |
11 |
int i; |
int i; |
19 |
int main(int argc, char * argv[]) |
int main(int argc, char * argv[]) |
20 |
{ |
{ |
21 |
|
|
|
// TODO: segfaults if zero arguments given |
|
|
// switch/case??? |
|
22 |
if (argc == 1) { |
if (argc == 1) { |
23 |
PRINTERROR("No arguments given, will segfault under cygwin. :-)"); |
PRINTERROR("No arguments given, will segfault under cygwin. :-)"); |
24 |
|
return -1; |
25 |
} |
} |
26 |
|
|
27 |
// (3) range mode |
/* (3) range mode */ |
28 |
if (argc > 2) { |
if (argc > 2) { |
29 |
int i, j; |
int i, j; |
30 |
j = atoi(argv[2]); |
j = atoi(argv[2]); |
32 |
PRINTPRIME(i); |
PRINTPRIME(i); |
33 |
} |
} |
34 |
|
|
35 |
// other modes |
/* other modes */ |
36 |
} else { |
} else { |
37 |
|
|
38 |
// try to open file for reading |
/* try to open file for reading */ |
39 |
FILE * fp = fopen(argv[1], "r"); |
FILE * fp = fopen(argv[1], "r"); |
40 |
|
|
41 |
// (1) test-single-number mode: first argument is not a filename |
/* (1) test-single-number mode: first argument is not a filename */ |
42 |
if (fp == NULL) { |
if (fp == NULL) { |
43 |
PRINTPRIME(atoi(argv[1])); |
PRINTPRIME(atoi(argv[1])); |
44 |
|
|
45 |
// (2) file mode: read numbers from file |
/* (2) file mode: read numbers from file */ |
46 |
} else { |
} else { |
47 |
char num[11]; |
char num[11]; |
48 |
while (fgets(num, 11, fp)) |
while (fgets(num, 11, fp)) { |
49 |
|
printf("raw: %s\n", num); |
50 |
|
printf("num: %i\n", atoi(num)); |
51 |
PRINTPRIME(atoi(num)); |
PRINTPRIME(atoi(num)); |
52 |
|
} |
53 |
fclose(fp); |
fclose(fp); |
54 |
} |
} |
55 |
} |
} |