/[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.10 by joko, Sat May 13 12:06:38 2006 UTC revision 1.17 by franky, Mon May 15 11:36:52 2006 UTC
# Line 2  Line 2 
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
# Line 13  Line 18 
18  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)
19  #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)  #define PRINTWARNING(message) fprintf(stderr, "WARNING: %s\n", message)
20    
21    #define MAX_LINE_LENGTH 80
22    
23    
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          }          }
# Line 30  BOOL is_prime(long int number) Line 38  BOOL is_prime(long int number)
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;
44          long int number = strtol(nptr, &endptr, 10);          long int number;
45    
46          errno = 0;          errno = 0;
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 number.", nptr);                  snprintf(message, 255, "Could not convert '%s' to a valid (integer) number.", nptr);
53                  PRINTERROR(message);                  errno=EINVAL;
54                  exit(EXIT_FAILURE);                  if(warn)
55                            PRINTWARNING(message);
56                    else{
57                            PRINTERROR(message);
58                            exit(EXIT_FAILURE);
59                    }
60          }          }
61                    
62          /* invalid range? */          /* invalid range? */
63          /* if (number == LONG_MAX || number == LONG_MIN) { */          /* if (number == LONG_MAX || number == LONG_MIN) { */
64          if (errno == ERANGE) {          if (errno == ERANGE) {
65                  char message[254];                  char message[256];
66                  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);
67                  PRINTERROR(message);                  errno=EINVAL;
68                  exit(EXIT_FAILURE);                  if(warn)
69                            PRINTWARNING(message);
70                    else{                  
71                            PRINTERROR(message);
72                            exit(EXIT_FAILURE);
73                    }
74          }          }
75                    
76          return number;          return number;
# Line 69  int main(int argc, char * argv[]) Line 88  int main(int argc, char * argv[])
88          /* (3) range mode */          /* (3) range mode */
89          if (argc > 2) {          if (argc > 2) {
90                  long int i, j;                  long int i, j;
91                  i = convert_number(argv[1]);                  i = convert_number(argv[1],FALSE);
92                  j = convert_number(argv[2]);                  j = convert_number(argv[2],FALSE);
93                  for (i; i< j; i++) {                  for (i; i< j; i++) {
94                          PRINTPRIME(i);                          PRINTPRIME(i);
95                  }                  }
96                    
97          /* other modes */          /* other modes */
98          } else {          } else {
99                    int err;
100                  /* try to open file for reading */                  /* try to open file for reading */
101                  FILE * fp = fopen(argv[1], "r");                  FILE * fp = fopen(argv[1], "r");
102                                    err=errno;
103                    errno=0;
104                  /* (1) test-single-number mode: first argument is not a filename */                  /* (1) test-single-number mode: first argument is not a filename */
105                  if (fp == NULL) {                  if (fp == NULL) {
106                          long int number = convert_number(argv[1]);                          long int number = convert_number(argv[1],TRUE);
107                            if(errno){
108                                    fprintf(stderr, "%s: %s\n", argv[1], strerror(err));
109                                    return -1;
110                            }
111                          PRINTPRIME(number);                          PRINTPRIME(number);
112                                    
113                  /* (2) file mode: read numbers from file */                  /* (2) file mode: read numbers from file */
114                  } else {                  } else {
115                          char entry[81];                          char line[MAX_LINE_LENGTH + 1];
116                          long int number;                          long int number;
117                          long int lineno = 0;                          int line_length;
118                          while (fgets(entry, 81, fp)) {                          long int line_no = 0;
119                            while (fgets(line, MAX_LINE_LENGTH + 1, fp)) {
120                                                                    
121                                  /* count line number (for warnings) */                                  /* count line number (for warnings) */
122                                  lineno++;                                  line_no++;
123    
124                                    line_length = strlen(line);
125                                    
126                                  /* skip empty lines */                                  /* skip empty lines */
127                                  if (strlen(entry) < 2) continue;                                  if (strlen(line) < 2) continue;
128    
129                                  /* line handling: policy = skip exceeding lines */                                  /* line handling: policy = skip exceeding lines */
130                                                                    
                                 /* if last char is newline, strip it */  
                                 if (entry[strlen(entry)-1] == '\n') {  
                                         entry[strlen(entry)-1] = '\0';  
                                           
131                                  /* line exceeds max length */                                  /* line exceeds max length */
132                                  } else {                                  if (line_length == MAX_LINE_LENGTH && line[line_length-1] != '\n') {
133                                          char message[254];                                          char message[254];
134                                          snprintf(message, 256, "Line too long (max 80 chars) in line number: %i", lineno);                                          snprintf(message, 256, "Line too long (max %i chars) in line number: %i", MAX_LINE_LENGTH, line_no);
135                                          PRINTWARNING(message);                                          PRINTWARNING(message);
136                                                                                    
137                                          /* eat all characters until newline */                                          /* eat all characters until newline or EOF */
138                                          while (fgetc(fp) != 10);                                          while (1) {
139                                                    int charcode = fgetc(fp);
140                                                    if (charcode == 10 || charcode == EOF) break;
141                                            }
142                                                                                    
143                                          /* skip this line from prime calculation */                                          /* skip this line from prime calculation */
144                                          continue;                                          continue;
145                                  }                                  }
146                                                                    
147                                  number = convert_number(entry);                                  /* if last char is newline, strip it */
148                                    if (line[line_length-1] == '\n') {
149                                            line[line_length-1] = '\0';
150                                    }
151                                    
152                                    /* finally: prime number calculation and output */
153                                    number = convert_number(line,FALSE);
154                                  PRINTPRIME(number);                                  PRINTPRIME(number);
155                                    
156                          }                          }
157                          fclose(fp);                          fclose(fp);
158                  }                  }

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.17

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