Advertisement
Derga

Untitled

Feb 27th, 2023
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. /*
  2. Задание 1.
  3. */
  4.  
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <functional>
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12.     double x_first;
  13.     double x_last;
  14.     double dx;
  15.     double a;
  16.     double b;
  17.     double c;  
  18.     cin >> x_first >> x_last >> dx >> a >> b >> c;
  19.  
  20.     double result = 0;
  21.     bool is_ok = true;
  22.  
  23.     auto function1 = [&a, &b, &c, &result, &is_ok](const double& x) {
  24.         if (b < 0) {
  25.             cout << "Cant get sqrt froom negative number\n";
  26.             is_ok = false;
  27.             return;
  28.         }
  29.         result = -1 * (a * x * x) - sqrt(b);
  30.     };
  31.  
  32.     auto function2 = [&a, &b, &c, &result, &is_ok](const double& x) {
  33.         if (c == 0 || x == 0) {
  34.             cout << "Cant devide by 0\n";
  35.             is_ok = false;
  36.             return;
  37.         }
  38.         result = (x - a) / (c * x);
  39.     };
  40.  
  41.     auto function3 = [&a, &b, &c, &result, &is_ok](const double& x) {
  42.         if (b == 0) {
  43.             cout << "Cant divide by 0\n";
  44.             is_ok = false;
  45.             return;
  46.         }
  47.         result = -1 * x / b;
  48.     };
  49.  
  50.  
  51.     vector<function<void(double)>> functions = { function1, function2, function3 };
  52.    
  53.     size_t foo_idx = 2;
  54.     if (c < 0 && a) {
  55.         foo_idx = 0;
  56.     }
  57.     if (c > 5 && !a) {
  58.         foo_idx = 1;
  59.     }
  60.    
  61.     auto foo = functions[foo_idx];
  62.     const int PRECISION = 15;
  63.  
  64.     size_t iterations_count = (x_last - x_first) / dx + 1;
  65.     for (size_t i = 0; i < iterations_count; ++i) {
  66.         result = 0;
  67.         is_ok = true;
  68.         foo(x_first + i * dx);
  69.         if (is_ok) {
  70.             cout << fixed << setprecision(PRECISION) << result << endl;
  71.         }
  72.     }
  73.    
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement