/[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.4 by joko, Fri May 12 19:53:48 2006 UTC revision 1.6 by joko, Fri May 12 21:13:49 2006 UTC
# Line 1  Line 1 
1    /* $Id$ */
2    
3  #include <stdio.h>  #include <stdio.h>
4    #include <stdlib.h>
5    #include <limits.h>
6    
7    #define BOOL int
8    #define TRUE 1
9    #define FALSE 0
10    
11  #define PRINTPRIME(x) if(is_prime(x)) printf("%i\n", x)  #define PRINTPRIME(x) if (is_prime(x)) printf("%i\n", x)
12  #define PRINTERROR(message) printf("ERROR: %s\n", message)  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)
13    
14  // check for prime number  /* check for prime number */
15  int is_prime(int number)  BOOL is_prime(long int number)
16  {  {
17          int i;          int i;
18          for (i=2; i*i <= number; i++) {          for (i=2; i*i <= number; i++) {
19                  if ((number % i) == 0)                  if ((number % i) == 0)
20                          return 0;                          return FALSE;
21            }
22            return TRUE;
23    }
24    
25    /* convert from string to long, with error checking */
26    long int convert_number(const char *nptr) {
27            long int number = strtol(nptr, (char **)NULL, 10);
28            if (number == LONG_MAX) {
29                    PRINTERROR("Number '%s' is not in range of 'long int'.");
30          }          }
         return 1;  
31  }  }
32    
33  int main(int argc, char * argv[])  int main(int argc, char * argv[])
34  {  {
35                    
         // TODO: segfaults if zero arguments given  
         // switch/case???  
36          if (argc == 1) {          if (argc == 1) {
37                  PRINTERROR("No arguments given, will segfault under cygwin. :-)");                  PRINTERROR("No arguments given.");
38                    return -1;
39          }          }
40                    
41          // (3) range mode          /* (3) range mode */
42          if (argc > 2) {          if (argc > 2) {
43                  int i, j;                  int i, j;
44                  j = atoi(argv[2]);                  j = atoi(argv[2]);
# Line 31  int main(int argc, char * argv[]) Line 46  int main(int argc, char * argv[])
46                          PRINTPRIME(i);                          PRINTPRIME(i);
47                  }                  }
48                    
49          // other modes          /* other modes */
50          } else {          } else {
51    
52                  // try to open file for reading                  /* try to open file for reading */
53                  FILE * fp = fopen(argv[1], "r");                  FILE * fp = fopen(argv[1], "r");
54                                    
55                  // (1) test-single-number mode: first argument is not a filename                  /* (1) test-single-number mode: first argument is not a filename */
56                  if (fp == NULL) {                  if (fp == NULL) {
57                          PRINTPRIME(atoi(argv[1]));                          /* PRINTPRIME(atoi(argv[1])); */
58                            PRINTPRIME(convert_number(argv[1]));
59                                    
60                  // (2) file mode: read numbers from file                  /* (2) file mode: read numbers from file */
61                  } else {                  } else {
62                          char num[11];                          char num[11];
63                          while (fgets(num, 11, fp))                          while (fgets(num, 11, fp)) {
64                                    printf("raw: %s\n", num);
65                                    printf("num: %i\n", atoi(num));
66                                  PRINTPRIME(atoi(num));                                  PRINTPRIME(atoi(num));
67                            }
68                          fclose(fp);                          fclose(fp);
69                  }                  }
70          }          }

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

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