/[cvs]/joko/Uni/BSArch/01/prime.c
ViewVC logotype

Contents of /joko/Uni/BSArch/01/prime.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (show annotations)
Sat May 13 12:30:54 2006 UTC (18 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.10: +27 -15 lines
File MIME type: text/plain
fixed file mode

1 /* $Id: prime.c,v 1.10 2006/05/13 12:06:38 joko Exp $ */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <errno.h>
7
8 #define BOOL int
9 #define TRUE 1
10 #define FALSE 0
11
12 #define PRINTPRIME(x) if (is_prime(x)) printf("%i\n", x)
13 #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)
14 #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)
15
16 #define MAX_LINE_LENGTH 80
17
18
19 /* check for prime number */
20 BOOL is_prime(long int number)
21 {
22 int i;
23
24 /* negative values, 0 and 1 are never prime numbers */
25 if (number < 2) return FALSE;
26
27 /* check all numbers 2..sqrt(number) for being a prime number */
28 for (i=2; i*i <= number; i++) {
29 if ((number % i) == 0)
30 return FALSE;
31 }
32 return TRUE;
33 }
34
35 /* convert from string to long int, with error checking */
36 long int convert_number(const char *nptr) {
37
38 char * endptr;
39 long int number = strtol(nptr, &endptr, 10);
40
41 errno = 0;
42
43 /* invalid characters? */
44 if (*endptr != '\0') {
45 char message[254];
46 snprintf(message, 256, "Could not convert '%s' to a valid number.", nptr);
47 PRINTERROR(message);
48 exit(EXIT_FAILURE);
49 }
50
51 /* invalid range? */
52 /* if (number == LONG_MAX || number == LONG_MIN) { */
53 if (errno == ERANGE) {
54 char message[254];
55 snprintf(message, 256, "Number is not in range of 'long int': %s", nptr);
56 PRINTERROR(message);
57 exit(EXIT_FAILURE);
58 }
59
60 return number;
61
62 }
63
64 int main(int argc, char * argv[])
65 {
66
67 if (argc == 1) {
68 PRINTERROR("No arguments given.");
69 exit(EXIT_FAILURE);
70 }
71
72 /* (3) range mode */
73 if (argc > 2) {
74 long int i, j;
75 i = convert_number(argv[1]);
76 j = convert_number(argv[2]);
77 for (i; i< j; i++) {
78 PRINTPRIME(i);
79 }
80
81 /* other modes */
82 } else {
83
84 /* try to open file for reading */
85 FILE * fp = fopen(argv[1], "r");
86
87 /* (1) test-single-number mode: first argument is not a filename */
88 if (fp == NULL) {
89 long int number = convert_number(argv[1]);
90 PRINTPRIME(number);
91
92 /* (2) file mode: read numbers from file */
93 } else {
94 char line[MAX_LINE_LENGTH + 1];
95 long int number;
96 int line_length;
97 long int line_no = 0;
98 while (fgets(line, MAX_LINE_LENGTH + 1, fp)) {
99
100 /* count line number (for warnings) */
101 line_no++;
102
103 line_length = strlen(line);
104
105 /* skip empty lines */
106 if (strlen(line) < 2) continue;
107
108 /* line handling: policy = skip exceeding lines */
109
110 /* line exceeds max length */
111 if (line_length == MAX_LINE_LENGTH && line[line_length-1] != '\n') {
112 char message[254];
113 snprintf(message, 256, "Line too long (max %i chars) in line number: %i", MAX_LINE_LENGTH, line_no);
114 PRINTWARNING(message);
115
116 /* eat all characters until newline or EOF */
117 while (1) {
118 int charcode = fgetc(fp);
119 if (charcode == 10 || charcode == EOF) break;
120 }
121
122 /* skip this line from prime calculation */
123 continue;
124 }
125
126 /* if last char is newline, strip it */
127 if (line[line_length-1] == '\n') {
128 line[line_length-1] = '\0';
129 }
130
131 /* finally: prime number calculation and output */
132 number = convert_number(line);
133 PRINTPRIME(number);
134
135 }
136 fclose(fp);
137 }
138 }
139 }

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed