Advertisement
pVinc

Untitled

Jul 10th, 2021
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <complex>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.   double w1, w2, w3, w4, delta, q, p; // współczynniki
  10.   const double pi = 3.14;
  11.   printf("Pierwszy wspolczynnik: ");
  12.   scanf("%lf", &w1);
  13.   if (w1 != 0)
  14.   {
  15.  
  16.     printf("Drugi wspolczynnik: ");
  17.     scanf("%lf", &w2);
  18.     printf("Trzeci wspolczynnik: ");
  19.     scanf("%lf", &w3);
  20.     printf("Czwarty wspolczynnik: ");
  21.     scanf("%lf", &w4);
  22.  
  23.     complex<double> wynik1, wynik2, wynik3;
  24.     p = (w3 / w1) - (pow(w2, 2.0) / (3 * pow(w1, 2.0)));
  25.     q = ((2 * pow(w2, 3.0)) / (27 * pow(w1, 3.0))) - ((w2 * w3) / (3 * pow(w1, 2.0))) + (w4 / w1);
  26.     delta = pow(q / 2.0, 2.0) + pow(p / 3.0, 3.0);
  27.  
  28.     if (delta >= 0)
  29.     {
  30.       double u, v;
  31.       u = pow(-(q / 2.0) + sqrt(abs(delta)), 1.0 / 3.0);
  32.       v = pow(abs(-(q / 2.0) - sqrt(abs(delta))), 1.0 / 3.0);
  33.  
  34.       wynik2 = (-(1.0 / 2.0) * (u - v) - (w2 / (3 * w1))) + (1i * sqrt(3)/2.0 * (u + v));
  35.       wynik3 = (-(1.0 / 2.0) * (u - v) - (w2 / (3 * w1))) - (1i * sqrt(3)/2.0 * (u + v));
  36.       wynik1 = u - v - (w2 / (3 * w1));
  37.  
  38.     }
  39.     else
  40.     {
  41.  
  42.       double g;
  43.       g = acos((-(q / 2.0)) / sqrt(-(pow(p, 3.0) / 27.0)));
  44.       wynik2 = 2 * sqrt(-p / 3.0) * cos((g + (2 * pi)) / 3.0) - (w2 / (3.0 * w1));
  45.       wynik3 = 2 * sqrt(-p / 3.0) * cos((g + (4 * pi)) / 3.0) - (w2 / (3.0 * w1));
  46.       wynik1 = 2 * sqrt(-p / 3.0) * cos(g / 3.0) - (w2 / (3 * w1));
  47.  
  48.     }
  49.     printf("wynik1 = %lf + (%lf)i\n", real(wynik1), imag(wynik1));
  50.     printf("wynik2 = %lf + (%lf)i\n", real(wynik2), imag(wynik2));
  51.     printf("wynik3 = %lf + (%lf)i\n", real(wynik3), imag(wynik3));
  52.  
  53.     printf("Potwierdzenie metoda hornera\n");
  54.     complex<double> horn1, horn2, horn3;
  55.     horn1 = (((w1 * wynik1 + w2) * wynik1 + w3) * wynik1 + w4);
  56.     horn2 = (((w1 * wynik2 + w2) * wynik2 + w3) * wynik2 + w4);
  57.     horn3 = (((w1 * wynik3 + w2) * wynik3 + w3) * wynik3 + w4);
  58.    
  59.     if(real(horn1) == 0 && imag(horn1) == 0){
  60.         printf("Wynik1 poprawny: %lf + (%lf)i\n", real(horn1), imag(horn1));
  61.     }else{
  62.         printf("Wynik1 niepoprawny: %lf + (%lf)i\n", real(horn1), imag(horn1));
  63.     }
  64.  
  65.     if(real(horn1) == 0 && imag(horn1) == 0){
  66.         printf("Wynik2 poprawny: %lf + (%lf)i\n", real(horn2), imag(horn2));
  67.     }else{
  68.         printf("Wynik2 niepoprawny: %lf + (%lf)i\n", real(horn2), imag(horn2));
  69.     }
  70.  
  71.     if(real(horn1) == 0 && imag(horn1) == 0){
  72.         printf("Wynik3 poprawny: %lf + (%lf)i\n", real(horn3), imag(horn3));
  73.     }else{
  74.         printf("Wynik3 niepoprawny: %lf + (%lf)i\n", real(horn3), imag(horn3));
  75.     }
  76.     return 0;
  77.  
  78.   }
  79.  
  80.  
  81.  
  82.   printf("Pierwszy wspolczynnik nie moze byc 0\n");
  83.   return 0;
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement