# Untitled

May 19th, 2021
851
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <complex>
2. #include <cstdlib>
3. #include <iostream>
4. #include <locale.h>
5. #include <math.h>
6.
7. using namespace std;
8. const double Pi = 3.14159;
9. const double zero = 0.00000001;
10. double A, B, C, D, p, q, Delta;
11. int wybor;
12.
13. int main() {
14.   setlocale(LC_CTYPE, "Polish");
15.   cout
16.       << "Witam w programie do obliczania\npierwiastkow wielomianu 3-stopnia\n";
17.
18.   for (;;) {
19.     cout << "-------------------------------" << endl << "\n";
20.     cout << "1 - zacznij liczyć pierwiastki wielomianu\n";
21.     cout << "2 - wyjdź z programu\n";
22.     cin >> wybor;
23.
24.     switch (wybor) {
25.     case 1:
26.       system("cls");
27.       cout << "\nPodaj A (A!=0): ";
28.       cin >> A;
29.       while (A == 0) {
30.         system("cls");
31.         cout << "\nBŁĄD !!!! Podaj A (A!=0): ";
32.         cin >> A;
33.       }
34.       cout << "Podaj B: ";
35.       cin >> B;
36.       cout << "Podaj C: ";
37.       cin >> C;
38.       cout << "Podaj D: ";
39.       cin >> D;
40.
41.       p = (C / A) - (B * B) / (3.0 * A * A);
42.
43.       cout << "\np = " << p;
44.
45.       q = ((2.0 * B * B * B) / (27.0 * A * A * A)) + (D / A) -
46.           ((B * C) / (3.0 * A * A));
47.
48.       cout << "\nq = " << q;
49.
50.       // Delta = ((q * q) + (4.0 * p * p * p)) / 27.0;
51.       Delta = pow(q / 2.0, 2.0) + pow(p / 3.0, 3.0);
52.
53.       cout << "\nDelta = " << Delta << endl;
54.       cout << "-------------------------------" << endl;
55.
56.       if (Delta > 0) {
57.         complex<double> x2;
58.         complex<double> x3;
59.         complex<double> wynik;
60.         double x1;
61.         double u, v;
62.
63.         u = pow(-(q / 2.0) + sqrt(abs(Delta)), 1.0 / 3.0);
64.         v = pow(abs(-(q / 2.0) - sqrt(abs(Delta))), 1.0 / 3.0);
65.
66.         x1 = u - v - (B / (3 * A));
67.
68.         x2 = (-(1.0 / 2.0) * (u - v) - (B / (3 * A))) +
69.              (1i * (sqrt(3.0) / 2.0) * (u + v));
70.         x3 = (-(1.0 / 2.0) * (u - v) - (B / (3 * A))) -
71.              (1i * (sqrt(3.0) / 2.0) * (u + v));
72.
73.         cout << "x1 = " << x1 << endl;
74.         cout << "x2 = " << real(x2) << " +j " << imag(x2) << endl;
75.         cout << "x3 = " << real(x3) << " -j " << abs(imag(x3)) << endl;
76.
77.         // sprawdzenie
78.         cout << x2 << endl;
79.         cout << x3 << endl;
80.
81.         wynik = A * pow(x2, 3) + B * pow(x2, 2) + C * pow(x2, 1) + D;
82.         cout << wynik << endl;
83.
84.       }
85.
86.       else if (Delta < 0) {
87.         double y1, y2, y3, x1, x2, x3;
88.         double u, v;
89.         u = 2 * sqrt(abs(p / 3.0));
90.         v = acos(((-1.0 * q) / 2.0) * pow(abs(3 / p), 3.0 / 2.0));
91.
92.         y1 = u * cos(v / 3.0);
93.         y2 = u * cos((v + 2 * Pi) / 3.0);
94.         y3 = u * cos((v + 4 * Pi) / 3.0);
95.
96.         x1 = y1 - (B / (3.0 * A));
97.         x2 = y2 - (B / (3.0 * A));
98.         x3 = y3 - (B / (3.0 * A));
99.
100.         cout << "x1 = " << x1 << endl;
101.         cout << "x2 = " << x2 << endl;
102.         cout << "x3 = " << x3 << endl;
103.
104.         /*  double F = (((3.0 * C) / A) - ((B * 😎 / (A * A))) / 3.0;
105.           double G = (((2.0 * B * B * 😎 / (A * A * A)) - ((9.0 * B * C) / (A *
106.           A)) + ((27.0 * D) / A)) /27.0; double H = ((G * G) / 4.0) + ((F * F *
107.           F) / 27.0); double I = sqrt(((G * G) / 4.0) - H); double J = cbrt(F);
108.           double K = acos(-(G / (2.0 * I)));
109.           double L = -J;
110.           double M = cos(K / 3.0);
111.           double N = sqrt(3.0) * sin(K / 3.0);
112.           double P = -(B / (3.0 * A));
113.           double x1 = (2.0 * J * M) + P;
114.           double x2 = L * (M + N) + P;
115.           double x3 = L * (M - N) + P;
116.           cout << "x[1] = " << x1 << endl;
117.           cout << "x[2] = " << x2 << endl;
118.           cout << "x[3] = " << x3 << endl; */
119.       } else if (Delta == 0) {
120.         double y1, y2, x1, x2;
121.         double u;
122.         u = -1 * pow(q / 2.0, 1.0 / 3.0);
123.         y1 = 2 * u;
124.         y2 = -u;
125.
126.         x1 = y1 - B / 3.0;
127.         x2 = y2 - B / 3.0;
128.
129.         cout << "x1 = " << x1 << endl;
130.         cout << "x2 = " << x2 << endl;
131.       }
132.
133.       else {
134.         cout << "BłĄD" << endl;
135.       }
136.
137.       cout << "\n";
138.       break;
139.
140.     case 2:
141.       exit(0);
142.       break;
143.     default:
144.       system("cls");
145.
146.       break;
147.     }
148.   }
149. }
150.