Advertisement
Leeen

Linux_cmd_lab #3

Apr 15th, 2020
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<unistd.h>
  4. #include<string.h>
  5.  
  6. using namespace std;
  7.  
  8. void help(){
  9.   cout << "Chebeshev function\nn - number of iteration\nx - argument of function\nPlease, enter the value of n and x.\n Use Enter for it\n";
  10. }
  11.  
  12. int enter_n() {
  13.   int n;
  14.   cin >> n;
  15.   cin.clear();
  16.   while (cin.get() != '\n'){
  17.     cout << "Error. Try again" << endl;
  18.     enter_n();
  19.   }
  20. return n;
  21. }
  22.  
  23. double enter_x() {
  24.  
  25.   double x;
  26.   cin >> x;
  27.   cin.clear();
  28.  while (cin.get() != '\n'){
  29.     cout << "Error. Try again" << endl;
  30.     cin.clear();
  31.     enter_x();
  32.   }
  33.  
  34. return x;
  35. }
  36.  
  37.  
  38. double chebeshev(int n, double x)
  39. {
  40.    if (n == 0)
  41.            {
  42.                  return 1;
  43.            }
  44.            else if (n == 1)
  45.            {
  46.                  return x;
  47.            }
  48.            else
  49.            {
  50.                  double polinom;
  51.                  polinom = 2 * x * chebeshev(n - 1, x) - chebeshev(n - 2, x);
  52.                  return polinom;
  53.            }
  54.     }
  55. int main(int argc, char ** argv){
  56. if ((argc == 2) && (!strcmp("help", argv[1])))
  57.     {
  58.         help();
  59.         exit(0);
  60.     }
  61. int pid;
  62. int c[2];
  63. int s[2];
  64. pipe(c);
  65. pipe(s);
  66. if((pid = fork()) == -1)
  67.            {
  68.                    perror("fork");
  69.                    exit(1);
  70.            }
  71.         if((pid = fork()) == 0)
  72.            {
  73.    
  74.                 int n = enter_n();
  75.  
  76.                 write(c[1], &n, sizeof(n));
  77.                 int x = enter_x();
  78.                 write(c[1], &x, sizeof(x));
  79.                 close(c[1]);
  80.                
  81.         double result1;
  82.                 read(s[0], &result1, sizeof(result1));
  83.                 close(s[1]);
  84.                 cout << "Result = " << result1 << endl;
  85.  
  86.            }
  87.            else
  88.            {
  89.                 int n1;
  90.  
  91.                 read(c[0], &n1, sizeof(n1));
  92.                 int x1;
  93.                 read(c[0], &x1, sizeof(x1));
  94.                 close(c[0]);
  95.  
  96.                 double result =  chebeshev(n1, x1);
  97.                 write(s[1], &result, sizeof(result));
  98.                 close(s[1]);
  99.  
  100.            }
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement