Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <complex>
- #include <cmath>
- using namespace std;
- int main()
- {
- double w1, w2, w3, w4, delta, q, p; // współczynniki
- const double pi = 3.14;
- printf("Pierwszy wspolczynnik: ");
- scanf("%lf", &w1);
- if (w1 != 0)
- {
- printf("Drugi wspolczynnik: ");
- scanf("%lf", &w2);
- printf("Trzeci wspolczynnik: ");
- scanf("%lf", &w3);
- printf("Czwarty wspolczynnik: ");
- scanf("%lf", &w4);
- complex<double> wynik1, wynik2, wynik3;
- p = (w3 / w1) - (pow(w2, 2.0) / (3 * pow(w1, 2.0)));
- q = ((2 * pow(w2, 3.0)) / (27 * pow(w1, 3.0))) - ((w2 * w3) / (3 * pow(w1, 2.0))) + (w4 / w1);
- delta = pow(q / 2.0, 2.0) + pow(p / 3.0, 3.0);
- if (delta >= 0)
- {
- double u, v;
- u = pow(-(q / 2.0) + sqrt(abs(delta)), 1.0 / 3.0);
- v = pow(abs(-(q / 2.0) - sqrt(abs(delta))), 1.0 / 3.0);
- wynik2 = (-(1.0 / 2.0) * (u - v) - (w2 / (3 * w1))) + (1i * sqrt(3)/2.0 * (u + v));
- wynik3 = (-(1.0 / 2.0) * (u - v) - (w2 / (3 * w1))) - (1i * sqrt(3)/2.0 * (u + v));
- wynik1 = u - v - (w2 / (3 * w1));
- }
- else
- {
- double g;
- g = acos((-(q / 2.0)) / sqrt(-(pow(p, 3.0) / 27.0)));
- wynik2 = 2 * sqrt(-p / 3.0) * cos((g + (2 * pi)) / 3.0) - (w2 / (3.0 * w1));
- wynik3 = 2 * sqrt(-p / 3.0) * cos((g + (4 * pi)) / 3.0) - (w2 / (3.0 * w1));
- wynik1 = 2 * sqrt(-p / 3.0) * cos(g / 3.0) - (w2 / (3 * w1));
- }
- printf("wynik1 = %lf + (%lf)i\n", real(wynik1), imag(wynik1));
- printf("wynik2 = %lf + (%lf)i\n", real(wynik2), imag(wynik2));
- printf("wynik3 = %lf + (%lf)i\n", real(wynik3), imag(wynik3));
- printf("Potwierdzenie metoda hornera\n");
- complex<double> horn1, horn2, horn3;
- horn1 = (((w1 * wynik1 + w2) * wynik1 + w3) * wynik1 + w4);
- horn2 = (((w1 * wynik2 + w2) * wynik2 + w3) * wynik2 + w4);
- horn3 = (((w1 * wynik3 + w2) * wynik3 + w3) * wynik3 + w4);
- if(real(horn1) == 0 && imag(horn1) == 0){
- printf("Wynik1 poprawny: %lf + (%lf)i\n", real(horn1), imag(horn1));
- }else{
- printf("Wynik1 niepoprawny: %lf + (%lf)i\n", real(horn1), imag(horn1));
- }
- if(real(horn1) == 0 && imag(horn1) == 0){
- printf("Wynik2 poprawny: %lf + (%lf)i\n", real(horn2), imag(horn2));
- }else{
- printf("Wynik2 niepoprawny: %lf + (%lf)i\n", real(horn2), imag(horn2));
- }
- if(real(horn1) == 0 && imag(horn1) == 0){
- printf("Wynik3 poprawny: %lf + (%lf)i\n", real(horn3), imag(horn3));
- }else{
- printf("Wynik3 niepoprawny: %lf + (%lf)i\n", real(horn3), imag(horn3));
- }
- return 0;
- }
- printf("Pierwszy wspolczynnik nie moze byc 0\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement