/[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.5 by joko, Fri May 12 20:38:15 2006 UTC
# Line 1  Line 1 
1    /* $Id$ */
2    
3  #include <stdio.h>  #include <stdio.h>
4    
5  #define PRINTPRIME(x) if(is_prime(x)) printf("%i\n", x)  #define PRINTPRIME(x) if(is_prime(x)) printf("%i\n", x)
6  #define PRINTERROR(message) printf("ERROR: %s\n", message)  #define PRINTERROR(message) fprintf(stderr, "ERROR: %s\n", message)
7    
8  // check for prime number  /* check for prime number */
9  int is_prime(int number)  int is_prime(int number)
10  {  {
11          int i;          int i;
# Line 17  int is_prime(int number) Line 19  int is_prime(int number)
19  int main(int argc, char * argv[])  int main(int argc, char * argv[])
20  {  {
21                    
         // TODO: segfaults if zero arguments given  
         // switch/case???  
22          if (argc == 1) {          if (argc == 1) {
23                  PRINTERROR("No arguments given, will segfault under cygwin. :-)");                  PRINTERROR("No arguments given, will segfault under cygwin. :-)");
24                    return -1;
25          }          }
26                    
27          // (3) range mode          /* (3) range mode */
28          if (argc > 2) {          if (argc > 2) {
29                  int i, j;                  int i, j;
30                  j = atoi(argv[2]);                  j = atoi(argv[2]);
# Line 31  int main(int argc, char * argv[]) Line 32  int main(int argc, char * argv[])
32                          PRINTPRIME(i);                          PRINTPRIME(i);
33                  }                  }
34                    
35          // other modes          /* other modes */
36          } else {          } else {
37    
38                  // try to open file for reading                  /* try to open file for reading */
39                  FILE * fp = fopen(argv[1], "r");                  FILE * fp = fopen(argv[1], "r");
40                                    
41                  // (1) test-single-number mode: first argument is not a filename                  /* (1) test-single-number mode: first argument is not a filename */
42                  if (fp == NULL) {                  if (fp == NULL) {
43                          PRINTPRIME(atoi(argv[1]));                          PRINTPRIME(atoi(argv[1]));
44                                    
45                  // (2) file mode: read numbers from file                  /* (2) file mode: read numbers from file */
46                  } else {                  } else {
47                          char num[11];                          char num[11];
48                          while (fgets(num, 11, fp))                          while (fgets(num, 11, fp)) {
49                                    printf("raw: %s\n", num);
50                                    printf("num: %i\n", atoi(num));
51                                  PRINTPRIME(atoi(num));                                  PRINTPRIME(atoi(num));
52                            }
53                          fclose(fp);                          fclose(fp);
54                  }                  }
55          }          }

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

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