Tobiahao

S01_PLIKI_09

Jan 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. /*
  2.  Napisz program, który otworzy do odczytu wskazany plik tekstowy, a następnie wywoła funkcję fork(). Jeden z powstałych procesów policzy sumę liczb parzystych znalezionych w otwartym pliku, a drugi liczb nieparzystych.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/fcntl.h>
  10. #include <unistd.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13.  
  14. #define BUFFER_SIZE 128
  15.  
  16. int calculate_odd(int fd)
  17. {
  18.     ssize_t status;
  19.     char buffer[BUFFER_SIZE];
  20.     int result = 0;
  21.  
  22.     for(;;) {
  23.         status = read(fd, buffer, BUFFER_SIZE-1);
  24.         if(status == -1) {
  25.             perror("read");
  26.             exit(EXIT_FAILURE);
  27.         }
  28.         else if(status == 0) {
  29.             break;
  30.         }
  31.         else {
  32.             for(int i = 0; i < strlen(buffer); i++) {
  33.                 if(isdigit(buffer[i])) {
  34.                     int number_buffer = buffer[i] - '0';
  35.                     if(number_buffer & 1)
  36.                         result += number_buffer;
  37.                 }
  38.             }
  39.         }
  40.     }
  41.     lseek(fd, 0, SEEK_SET);
  42.     return result;
  43. }
  44.  
  45. int calculate_even(int fd)
  46. {
  47.     ssize_t status;
  48.     char buffer[BUFFER_SIZE];
  49.     int result = 0;
  50.  
  51.     for(;;) {
  52.         status = read(fd, buffer, BUFFER_SIZE-1);
  53.         if(status == -1) {
  54.             perror("read");
  55.             exit(EXIT_FAILURE);
  56.         }
  57.         else if(status == 0) {
  58.             break;
  59.         }
  60.         else {
  61.             for(int i = 0; i < strlen(buffer); i++) {
  62.                 if(isdigit(buffer[i])) {
  63.                     int number_buffer = buffer[i] - '0';
  64.                     if(!(number_buffer & 1))
  65.                         result += number_buffer;
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     lseek(fd, 0, SEEK_SET);
  71.     return result;
  72. }
  73.  
  74. int main(int argc, char *argv[])
  75. {
  76.     if(argc != 2) {
  77.         fprintf(stderr, "Niepoprawna liczba argumentow!\n");
  78.         return EXIT_FAILURE;
  79.     }
  80.  
  81.     pid_t pid;
  82.     int fd;
  83.  
  84.     if((fd = open(argv[1], O_RDONLY, 0600)) == -1) {
  85.         perror("open");
  86.         return EXIT_FAILURE;
  87.     }
  88.  
  89.     pid = fork();
  90.     if(pid == -1) {
  91.         perror("fork");
  92.         return EXIT_FAILURE;
  93.     }
  94.     else if(pid == 0)
  95.         printf("Suma cyfr parzystych: %d\n", calculate_even(fd));
  96.     else
  97.         printf("Suma cyfr nieparzystych: %d\n", calculate_odd(fd));
  98.  
  99.     return EXIT_SUCCESS;
  100. }
Add Comment
Please, Sign In to add comment