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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by joko, Fri May 12 21:13:49 2006 UTC revision 1.12 by joko, Sat May 13 12:35:32 2006 UTC
# Line 3  Line 3 
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
# Line 10  Line 11 
11    
12  #define PRINTPRIME(x) if (is_prime(x)) printf("%i\n", x)  #define PRINTPRIME(x) if (is_prime(x)) printf("%i\n", x)
13  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)  #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 */  /* check for prime number */
20  BOOL is_prime(long int number)  BOOL is_prime(long int number)
21  {  {
22          int i;          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++) {          for (i=2; i*i <= number; i++) {
29                  if ((number % i) == 0)                  if ((number % i) == 0)
30                          return FALSE;                          return FALSE;
# Line 22  BOOL is_prime(long int number) Line 32  BOOL is_prime(long int number)
32          return TRUE;          return TRUE;
33  }  }
34    
35  /* convert from string to long, with error checking */  /* convert from string to long int, with error checking */
36  long int convert_number(const char *nptr) {  long int convert_number(const char *nptr) {
37          long int number = strtol(nptr, (char **)NULL, 10);          
38          if (number == LONG_MAX) {          char * endptr;
39                  PRINTERROR("Number '%s' is not in range of 'long int'.");          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 (integer) 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[])  int main(int argc, char * argv[])
# Line 35  int main(int argc, char * argv[]) Line 66  int main(int argc, char * argv[])
66                    
67          if (argc == 1) {          if (argc == 1) {
68                  PRINTERROR("No arguments given.");                  PRINTERROR("No arguments given.");
69                  return -1;                  exit(EXIT_FAILURE);
70          }          }
71                    
72          /* (3) range mode */          /* (3) range mode */
73          if (argc > 2) {          if (argc > 2) {
74                  int i, j;                  long int i, j;
75                  j = atoi(argv[2]);                  i = convert_number(argv[1]);
76                  for (i = atoi(argv[1]); i< j; i++) {                  j = convert_number(argv[2]);
77                    for (i; i< j; i++) {
78                          PRINTPRIME(i);                          PRINTPRIME(i);
79                  }                  }
80                    
# Line 54  int main(int argc, char * argv[]) Line 86  int main(int argc, char * argv[])
86                                    
87                  /* (1) test-single-number mode: first argument is not a filename */                  /* (1) test-single-number mode: first argument is not a filename */
88                  if (fp == NULL) {                  if (fp == NULL) {
89                          /* PRINTPRIME(atoi(argv[1])); */                          long int number = convert_number(argv[1]);
90                          PRINTPRIME(convert_number(argv[1]));                          PRINTPRIME(number);
91                                    
92                  /* (2) file mode: read numbers from file */                  /* (2) file mode: read numbers from file */
93                  } else {                  } else {
94                          char num[11];                          char line[MAX_LINE_LENGTH + 1];
95                          while (fgets(num, 11, fp)) {                          long int number;
96                                  printf("raw: %s\n", num);                          int line_length;
97                                  printf("num: %i\n", atoi(num));                          long int line_no = 0;
98                                  PRINTPRIME(atoi(num));                          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);                          fclose(fp);
137                  }                  }

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.12

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