Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <sys/wait.h>
- double frac(double x) {
- if(x == 0)
- return 1;
- else if (x == 1)
- return 1;
- else
- return x*frac(x-1);
- }
- int main() {
- int fd[2];
- char str[100], wynik[100];;
- pipe(fd);
- if(fork() > 0) {
- double X;
- double Eps;
- close(fd[0]);
- printf("X = ");
- scanf("%lf", &X);
- sprintf(str, "%lf", X);
- write(fd[1], str, 101);
- printf("Eps = ");
- scanf("%lf", &Eps);
- sprintf(str, "%lf", Eps);
- write(fd[1], str, 101);
- close(fd[1]);
- sleep(1);
- }
- else {
- close(fd[1]);
- double X, Eps;
- read(fd[0], wynik, 101);
- X = strtod(wynik, NULL);
- read(fd[0], wynik, 101);
- Eps = strtod(wynik, NULL);
- close(fd[0]);
- int fd1[2];
- pipe(fd1);
- if(fork() > 0) {
- double i = 0;
- double fragment;
- do {
- fragment = pow(X, i)/frac(i);
- i++;
- sprintf(str, "%lf", fragment);
- write(fd1[1], str, 101);
- printf("%lf\n ", fragment);
- }
- while(fragment >= Eps);
- return 0;
- }
- else {
- double sum=0, tmp;
- close(fd1[1]);
- while(read(fd1[0], wynik, 101)>0) {
- tmp = strtod(wynik, NULL);
- sum += tmp;
- }
- printf("Suma: %f\n", sum);
- printf("Wynik = %f\n", exp(X));
- return 0;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment