LazySenpai

lab8 zad1

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