Petro_zzz

lesson19_322

Sep 27th, 2023
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void sum(int a, int b) {
  6.     b = 17;
  7.     cout << a << " " << b << endl;
  8. }
  9.  
  10. int solve_eq(double coeff[], double sol[]) {
  11.     // returns number of roots
  12.     // koeff = {a, b, c}
  13.     // a * x * x + b * x + c = 0, sol = {x1, x2}
  14.     const double a = coeff[0],
  15.         b = coeff[1],
  16.         c = coeff[2];
  17.     double eps = DBL_EPSILON; // 1.0e-10;//_DOUBLE_EPS;
  18.     if (abs(a) <= eps) {
  19.         sol[0] = -c / b;
  20.         return 1;
  21.     }  
  22.     // discriminant
  23.     double d = b * b - 4 * a * c;
  24.     if (d < 0) {
  25.         return 0;
  26.     }
  27.     d = sqrt(d);
  28.     sol[0] = (-b - d) / (2 * a);
  29.     sol[1] = (-b + d) / (2 * a);
  30.     return 2;
  31. }
  32.  
  33. double check_solve_eq(double coeff[], double x) {
  34.     const double a = coeff[0],
  35.         b = coeff[1],
  36.         c = coeff[2];
  37.     return a * x * x + b * x + c;
  38. }
  39.  
  40. void analitic_task() {
  41.     double abc[]{-1, -2, -5};
  42.     double xs[2];
  43.     int num = solve_eq(abc, xs);
  44.    
  45.     for (int k = 0; k < num; k++)
  46.         cout << "root: " <<  xs[k] << " "
  47.              << "precision: "
  48.              << check_solve_eq(abc, xs[k]) << endl;
  49.     cout << endl;
  50. }
  51.  
  52. double check_function(double params[], double x) { 
  53.     return params[0] * tan(params[1] * x) - params[2];
  54. }
  55.  
  56. void numeric_task() {
  57.     // function(x)
  58.     double p[]{1.1, 0.5, 10};
  59.     double val;
  60.     double sol = -100;
  61.     double min = abs(check_function(p, sol));
  62.     for (double x = -100; x <= 100; x += 0.00001) {
  63.         val = check_function(p, x);
  64.         if (abs(val) < min) {
  65.             min = abs(val);
  66.             sol = x;
  67.         }
  68.         //if(abs(val) < 0.02)
  69.         //  cout << x << " " << val << endl;
  70.     }
  71.     cout << sol << " " << min << endl;
  72. }
  73.  
  74. void test_colors() {
  75.     cout << "\033[33mTest" << endl;
  76.     cout << "Hoba";
  77.     for (int id = 0; id <= 108; id++) {
  78.         cout << "\033[" << id << "m "
  79.             << id << "\033[0m";
  80.         if ((id + 1) % 20 == 0)
  81.             cout << endl;
  82.     }
  83.     cout << endl;
  84.     cout << "\033[41;32m GOOD DAY \033[0m" << endl;
  85. }
  86.  
  87. int main() {
  88.     //analitic_task();
  89.     //numeric_task();
  90.     /*
  91.     int a = 4;
  92.     int b = -4;
  93.     sum(a, b);
  94.     cout << a << " " << b << endl;
  95.     */
  96.     test_colors();
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment