Advertisement
pVinc

Untitled

May 3rd, 2021
727
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <complex>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <iostream>
  5.  
  6. #ifndef printf_s
  7. #define printf_s printf
  8. #endif
  9.  
  10. #ifndef scanf_s
  11. #define scanf_s scanf
  12. #endif
  13.  
  14. #if defined(_WIN32) || defined(WIN32) || defined(_WIN64) || defined(WIN64)
  15. #define clear() system("cls")
  16. #elif defined(__unix__) || defined(linux)
  17. #define clear() system("clear")
  18. #endif
  19.  
  20. #define eps (double)0.00000001
  21. #define PI (double)3.141592
  22.  
  23. using namespace std;
  24.  
  25. int main(void) {
  26.   double fa, fb, fc, fd, fnp, fq, fD;
  27.  
  28.   clear();
  29.  
  30.   printf_s("Podaj liczbe a: ");
  31.   cin >> fa;
  32.   if (abs(fa) < eps) {
  33.     printf_s("a == 0\n");
  34.     exit(-1);
  35.   }
  36.  
  37.   printf_s("Podaj liczbe b: ");
  38.   cin >> fb;
  39.  
  40.   printf_s("Podaj liczbe c: ");
  41.   cin >> fc;
  42.  
  43.   printf_s("Podaj liczbe d: ");
  44.   cin >> fd;
  45.  
  46.   /*
  47.    *hornera
  48.    */
  49.   // complex<double> w1 = ((a * z1 + b) * z1 + c) * z1 + d;
  50.  
  51.   /*
  52.    * klasyczna
  53.    */
  54.   // complex<double> w1p = a * pow(z1, 3) + b * pow(z1, 2) + c * z1 + d;
  55.  
  56.   fnp = (-(fb * fb * fb) / (27 * fa * fa * fa)) + ((fb * fc) / (6 * fa * fa)) -
  57.         (fd / (2 * fa));
  58.   fq = (fc / (3 * fa)) - ((fb * fb) / (9 * fa * fa));
  59.  
  60.   fD = (fnp * fnp) + (fq * fq * fq);
  61.  
  62.   if (fD > 0) {
  63.     /*
  64.      * jedyn rzeczywisty, dwa zespol┼Źne
  65.      * src: https://pl.wikipedia.org/wiki/R%C3%B3wnanie_sze%C5%9Bcienne
  66.      */
  67.     double fu, fv, fx, fre_z, fim_z;
  68.     double fe = sqrt(3.0) / 2.0;
  69.  
  70.     fu = cbrt(fnp + sqrt(fD));
  71.     fv = cbrt(fnp - sqrt(fD));
  72.     fx = fu + fv - (fb / (3 * fa));
  73.     fre_z = -((fu / 2.0) + (fv / 2.0) + (fb / (3.0 * fa)));
  74.  
  75.     fim_z = (fu * fe) - (fv * fe);
  76.  
  77.     complex<double> z1(fx, 0);
  78.     complex<double> z2(fre_z, fim_z);
  79.     complex<double> z3(fre_z, -fim_z);
  80.  
  81.     cout << z1 << endl << z2 << endl << z3 << endl;
  82.   } else if (fD < 0) {
  83.     /*
  84.      * trzi rzeczywiste
  85.      * src: http://www.1728.org/cubic2.htm
  86.      */
  87.     double ff = (((3 * fc) / fa) - ((fb * fb) / (fa * fa))) / 3.0;
  88.     double fg = (((2 * fb * fb * fb) / (fa * fa * fa)) -
  89.                  ((9 * fb * fc) / (fa * fa)) + ((27 * fd) / fa)) /
  90.                 27.0;
  91.     double fh = ((fg * fg) / 4.0) + ((ff * ff * ff) / 27.0);
  92.     double fi = sqrt(((fg * fg) / 4) - fh);
  93.     double fj = cbrt(fi);
  94.     double fk = acos(-(fg / (2 * fi)));
  95.     double fL = -fj;
  96.     double fM = cos(fk / 3);
  97.     double fN = sqrt(3.0) * sin(fk / 3);
  98.     double fP = -(fb / (3 * fa));
  99.     double fdelta_lt_0_x1 = (2 * fj * fM) + fP;
  100.     double fdelta_lt_0_x2 = fL * (fM + fN) + fP;
  101.     double fdelta_lt_0_x3 = fL * (fM - fN) + fP;
  102.     cout << "x_1 = " << fdelta_lt_0_x1 << endl;
  103.     cout << "x_2 = " << fdelta_lt_0_x2 << endl;
  104.     cout << "x_3 = " << fdelta_lt_0_x3 << endl;
  105.   } else if (abs(fD) < eps) {
  106.     /*
  107.      * max. dwa rzeczywiste
  108.      * src: https://pl.wikipedia.org/wiki/R%C3%B3wnanie_sze%C5%9Bcienne
  109.      */
  110.     double fdelta0_x1 = cbrt(fq / 2.0) - (fb / (3.0 * fa));
  111.     double fdelta0_x2 = ((-2) * cbrt(fq / 2.0)) - (fb / (3.0 * fa));
  112.     if (abs(abs(fdelta0_x1) - abs(fdelta0_x2)) < eps) {
  113.       cout << "x_0 = " << fdelta0_x1 << endl;
  114.     } else {
  115.       cout << "x_1 = " << fdelta0_x1 << endl;
  116.       cout << "x_2 = " << fdelta0_x2 << endl;
  117.     }
  118.   } else {
  119.     cout << "Cos je niy on" << endl;
  120.   }
  121.  
  122.   getchar();
  123.   getchar();
  124.  
  125.   exit(0);
  126. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement