Advertisement
Guest User

zad1

a guest
Dec 6th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <sys/wait.h>
  7. #include <fcntl.h>
  8.  
  9. double silnia(double x)
  10. {
  11.     if(x==0)
  12.         return 1;
  13.     else if(x==1)
  14.         return 1;
  15.     else
  16.         return x*silnia(x-1);
  17. }
  18.  
  19. int main(){
  20.    
  21.     int fd[2];
  22.     char *myfifo1 = "Katarzyna1_2";
  23.     char *myfifo2 = "Katarzyna2_3";
  24.     mkfifo(myfifo1, 0666);
  25.     mkfifo(myfifo2, 0666);
  26.    
  27.    
  28.     char arr[100], wynik[100];
  29.    
  30.     pipe(fd);
  31.    
  32.     if(fork() > 0){
  33.         double X;
  34.         double Eps;
  35.        
  36.         close(fd[0]);
  37.        
  38.         printf("X = ");
  39.         scanf("%lf", &X);
  40.         sprintf(arr, "%lf", X);
  41.         write(fd[1], arr, 101);
  42.        
  43.         printf("Eps = ");
  44.         scanf("%lf", &Eps);
  45.         sprintf(arr, "%lf", Eps);
  46.         write(fd[1], arr, 101);
  47.        
  48.         close(fd[1]);
  49.         sleep(1);
  50.     }
  51.     else {
  52.         close(fd[1]);
  53.         double X, Eps;
  54.         read(fd[0], wynik, 101);
  55.         X=strtod(wynik, NULL);
  56.        
  57.         read(fd[0], wynik, 101);
  58.         Eps = strtod(wynik,NULL);
  59.        
  60.         close(fd[0]);
  61.        
  62.         int fd1[2];
  63.         pipe(fd1);
  64.        
  65.         if(fork() > 0){
  66.             double i=0;
  67.             double fragment;
  68.            
  69.             do{
  70.                 fragment=pow(X,i)/silnia(i);
  71.                 i++;
  72.                 sprintf(arr, "%lf", fragment);
  73.                 write(fd1[1], arr, 101);
  74.                 printf("%lf\n ", fragment);
  75.             }
  76.             while(fragment >= Eps);
  77.            
  78.             return 0;
  79.         }
  80.         else{
  81.             double sum=0, tmp;
  82.             close(fd1[1]);
  83.            
  84.             while(read(fd1[0], wynik, 101)>0){
  85.                 tmp=strtod(wynik, NULL);
  86.                 sum += tmp;
  87.             }
  88.             printf("Suma: %f\n", sum);
  89.             printf("Wynik = %f\n", exp(X));
  90.             return 0;
  91.         }
  92.     }
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement