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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (hide annotations)
Mon May 15 11:22:55 2006 UTC (18 years, 6 months ago) by franky
Branch: MAIN
Changes since 1.13: +33 -21 lines
File MIME type: text/plain
prime.c blubber.
test ;]

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

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