Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<vector>
- #include<unistd.h>
- #include<string.h>
- using namespace std;
- void help(){
- 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";
- }
- int enter_n() {
- int n;
- cin >> n;
- cin.clear();
- while (cin.get() != '\n'){
- cout << "Error. Try again" << endl;
- enter_n();
- }
- return n;
- }
- double enter_x() {
- double x;
- cin >> x;
- cin.clear();
- while (cin.get() != '\n'){
- cout << "Error. Try again" << endl;
- cin.clear();
- enter_x();
- }
- return x;
- }
- double chebeshev(int n, double x)
- {
- if (n == 0)
- {
- return 1;
- }
- else if (n == 1)
- {
- return x;
- }
- else
- {
- double polinom;
- polinom = 2 * x * chebeshev(n - 1, x) - chebeshev(n - 2, x);
- return polinom;
- }
- }
- int main(int argc, char ** argv){
- if ((argc == 2) && (!strcmp("help", argv[1])))
- {
- help();
- exit(0);
- }
- int pid;
- int c[2];
- int s[2];
- pipe(c);
- pipe(s);
- if((pid = fork()) == -1)
- {
- perror("fork");
- exit(1);
- }
- if((pid = fork()) == 0)
- {
- int n = enter_n();
- write(c[1], &n, sizeof(n));
- int x = enter_x();
- write(c[1], &x, sizeof(x));
- close(c[1]);
- double result1;
- read(s[0], &result1, sizeof(result1));
- close(s[1]);
- cout << "Result = " << result1 << endl;
- }
- else
- {
- int n1;
- read(c[0], &n1, sizeof(n1));
- int x1;
- read(c[0], &x1, sizeof(x1));
- close(c[0]);
- double result = chebeshev(n1, x1);
- write(s[1], &result, sizeof(result));
- close(s[1]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement