Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <vector>
- #include <complex>
- using namespace std;
- struct urojona
- {
- float x1u, x2u, su, ru, ilu, iru;
- urojona operator+(const urojona&);
- };
- struct zespolona
- {
- float x1r, x2r, sr, rr, ilr, irr;
- urojona u;
- };
- class rownanie
- {
- vector <int> dane_we;
- vector < complex <float> > zesp;
- float d, pd, md;
- zespolona *z;
- public:
- rownanie();
- ~rownanie();
- void formatuj_rownanie();
- void obliczd();
- void my_sqrt();
- void formatuj_l_zespol();
- void oblicz_pierwiastek();
- void dodaj();
- void odejmij();
- void pomnoz();
- void podziel();
- void wyswietl_wynik();
- void modul();
- };
- urojona urojona::operator+(const urojona& r)
- {
- urojona l;
- l.x1u = x1u+r.x1u;
- l.x2u = x2u+r.x2u;
- l.su = su+ r.su;
- l.ru = ru+r.ru;
- l.ilu =ilu+r.ilu;
- l.iru =iru+r.iru;
- return l;
- }
- rownanie::rownanie()
- {
- z = new zespolona;
- int zmienna;
- cout << "Podaj a: ";
- cin >> zmienna;
- dane_we.push_back(zmienna);
- cout << "Podaj b: ";
- cin >> zmienna;
- dane_we.push_back(zmienna);
- cout << "Podaj c: ";
- cin >> zmienna;
- dane_we.push_back(zmienna);
- }
- rownanie::~rownanie()
- {
- // dane_we[];
- delete z;
- z = NULL;
- }
- void rownanie::formatuj_rownanie()
- {
- if (dane_we.at(0) == 0); // print a
- else if (dane_we.at(0) == 1) cout << "xx";
- else if (dane_we.at(0) == -1) cout << "-xx";
- else cout << dane_we.at(0) << "xx";
- if (dane_we.at(0) != 0) // print b
- {
- if ((dane_we.at(1)) == 0);
- else if (dane_we.at(1) == 1) cout << " + x";
- else if (dane_we.at(1) == -1) cout << " - x";
- else if ((dane_we.at(1)) < 1) cout << " - " << -1 * dane_we.at(1) << "x";
- else cout << " + " << dane_we.at(1) << "x";
- }
- else if (dane_we.at(0) == 0)
- {
- if (dane_we.at(1) == 0);
- else if (dane_we.at(1) == 1) cout << "x";
- else if (dane_we.at(1) == -1) cout << "-x";
- else cout << dane_we.at(1) << "x";
- }
- if (dane_we.at(0) == 0 && dane_we.at(1) == 0 && dane_we.at(2) == 0) cout << "0"; // print c
- else if (dane_we.at(0) == 0 && dane_we.at(1) == 0)
- {
- if (dane_we.at(2) == 0);
- else if (dane_we.at(2) > 0) cout << dane_we.at(2);
- else cout << dane_we.at(2);
- }
- else if (dane_we.at(0) != 0 || dane_we.at(1) != 0)
- {
- if (dane_we.at(2) == 0);
- else if (dane_we.at(2) > 0) cout << " + " << dane_we.at(2);
- else cout << " - " << -1 * dane_we.at(2);
- }
- // end printing
- }
- void rownanie::obliczd()
- {
- d = dane_we.at(1) * dane_we.at(1) - 4 * dane_we.at(0) *dane_we.at(2);
- }
- void rownanie::my_sqrt()
- {
- if (d < 0)
- {
- pd = (d*(-1));
- pd = (pd + (d*(-1)) / pd) / 2;
- pd = (pd + (d*(-1)) / pd) / 2;
- pd = (pd + (d*(-1)) / pd) / 2;
- }
- else
- {
- pd = d;
- pd = (pd + d / pd) / 2;
- pd = (pd + d / pd) / 2;
- pd = (pd + d / pd) / 2;
- }
- }
- void rownanie::formatuj_l_zespol()
- {
- if (!zesp.empty())
- {
- if (d < 0)
- {
- if (zesp.at(0).imag() < 0) //x1u
- {
- cout << "z1= ";
- cout << "" << zesp.at(0).real() << zesp.at(0).imag() << "*i";
- cout << endl;
- }
- else
- {
- cout << "z1= ";
- cout << "" << zesp.at(0).real() << " + " << zesp.at(0).imag() << "*i";
- cout << endl;
- }
- if (zesp.at(1).imag() < 0)//z2=x2r+x2u
- {
- cout << "z2= ";
- cout << "" << zesp.at(1).real() << zesp.at(1).imag() << "*i";
- cout << endl;
- }
- else
- {
- cout << "z2= ";
- cout << "" << zesp.at(1).real() << " + " << zesp.at(1).imag() << "*i";
- cout << endl;
- }
- }
- if (d < 0)
- {
- if (zesp.at(2).imag() < 0) //sr+su
- {
- cout << "z (sr + su)= ";
- cout << "" << zesp.at(2).real() << zesp.at(2).imag() << "*i";
- cout << endl;
- }
- else
- {
- cout << "z (sr + su)= ";
- cout << "" << zesp.at(2).real() << " + " << zesp.at(2).imag() << "*i";
- cout << endl;
- }
- if (zesp.at(3).imag() < 0) //rr+ru
- {
- cout << "z (rr + ru)= ";
- cout << "" << zesp.at(3).real() << zesp.at(3).imag() << "*i";
- cout << endl;
- }
- else
- {
- cout << "z (rr + ru)= ";
- cout << "" << zesp.at(3).real() << " + " << zesp.at(3).imag() << "*i";
- cout << endl;
- }
- if (zesp.at(4).imag() < 0) //ilr+ilu
- {
- cout << "z (ilr + ilu)= ";
- cout << "" << zesp.at(4).real() << zesp.at(4).imag() << "*i";
- cout << endl;
- }
- else
- {
- cout << "z (ilr + iluu)= ";
- cout << "" << zesp.at(4).real() << " + " << zesp.at(4).imag() << "*i";
- cout << endl;
- }
- if (zesp.at(5).imag() < 0) //irr+iru
- {
- cout << "z (irr + iru)= ";
- cout << "" << zesp.at(5).real() << zesp.at(5).imag() << "*i";
- cout << endl;
- }
- else
- {
- cout << "z (irr + iru)= ";
- cout << "" << zesp.at(5).real() << " + " << zesp.at(5).imag() << "*i";
- cout << endl;
- }
- }
- }
- }
- void rownanie::oblicz_pierwiastek()
- {
- if (dane_we.at(0) != 0)
- {
- if (d > 0)
- {
- z->x1r = ((-1 * dane_we.at(1)) - sqrt(d)) / (2 * dane_we.at(0));
- z->x2r = ((-1 * dane_we.at(1)) + sqrt(d)) / (2 * dane_we.at(0));
- zesp.push_back(complex <float>(z->x1r, 0));
- zesp.push_back(complex <float>(z->x2r, 0));
- }
- if (d == 0)
- {
- z->x1r = (-1 * dane_we.at(1)) / 2.0 * dane_we.at(0);
- }
- if (d < 0)
- {
- d = fabs(d);
- z->x1r = (-1 * dane_we.at(1)) / (2.0 * dane_we.at(0));
- z->x2r = (-1 * dane_we.at(1)) / (2.0 * dane_we.at(0));
- z->u.x1u = (-1 * sqrt(d)) / (2.0 *dane_we.at(0));
- zesp.push_back(complex <float>(z->x1r, z->u.x1u));
- z->u.x2u = (zesp.at(0).imag()*-1);
- zesp.push_back(complex <float>(z->x2r, z->u.x2u));;
- }
- } // wyjscie z a!=0
- if (dane_we.at(0) == 0 && dane_we.at(1) != 0)
- {
- z->x1r = (1.0 * (-1 * dane_we.at(2))) / (dane_we.at(1) * 1.0);
- }
- }
- void rownanie::dodaj()
- {
- if (!zesp.empty())
- {
- obliczd();
- if (d > 0)
- {
- z->sr = zesp.at(0).real() + zesp.at(1).real();
- zesp.push_back(complex <float>(z->sr, 0));
- }
- else if (d < 0)
- {
- z->sr = zesp.at(0).real() + zesp[1].real();
- z->u.su = zesp.at(0).imag() + zesp.at(1).imag();
- zesp.push_back(complex <float>(z->sr, z->u.su));
- }
- }
- }
- void rownanie::odejmij()
- {
- if (!zesp.empty())
- {
- obliczd();
- if (d > 0)
- {
- cout << endl;
- z->rr = zesp.at(0).real() - zesp.at(1).real();
- zesp.push_back(complex <float>(z->rr, 0));
- }
- else if (d < 0)
- {
- z->rr = zesp.at(0).real() - zesp.at(1).real();
- z->u.ru = zesp.at(0).imag() - zesp.at(1).imag();
- zesp.push_back(complex <float>(z->rr, z->u.ru));
- }
- }
- }
- void rownanie::pomnoz()
- {
- if (!zesp.empty())
- {
- obliczd();
- if (d > 0)
- {
- cout << endl;
- z->ilr = zesp.at(0).real() * zesp.at(1).real();
- zesp.push_back(complex <float>(z->ilr, 0));
- }
- else if (d < 0)
- {
- cout << endl;
- z->ilr = (zesp.at(0).real() * zesp.at(1).real()) - (zesp.at(0).imag() * zesp.at(1).imag());
- z->u.ilu = (zesp.at(0).real() * zesp.at(1).imag()) + (zesp.at(1).real() * zesp.at(0).imag());
- zesp.push_back(complex <float>(z->ilr, z->u.ilu));
- }
- }
- }
- void rownanie::podziel()
- {
- if (!zesp.empty())
- {
- obliczd();
- if (d > 0)
- {
- if (z->x2r != 0)
- {
- z->irr = zesp.at(0).real() / zesp.at(1).real();
- zesp.push_back(complex <float>(z->irr, 0));
- }
- }
- else if (d < 0)
- {
- z->irr = ((zesp.at(0).real() * zesp.at(1).real()) + (zesp.at(0).imag() * zesp.at(1).imag())) / ((norm(zesp.at(1))));
- z->u.iru = ((zesp.at(1).real() * zesp.at(0).imag()) - (zesp.at(0).real() * zesp.at(1).imag())) / ((norm(zesp.at(1))));
- zesp.push_back(complex <float>(z->irr, z->u.iru));
- }
- }
- }
- void rownanie::wyswietl_wynik()
- {
- if (dane_we.at(0) != 0)
- {
- obliczd();
- cout << " d= " << d << endl;
- if ((d) > 0)
- {
- cout << "Pierwiastek wynosi= " << sqrt(d) << endl << endl;
- cout << "x1r= " << z->x1r;
- cout << endl;
- cout << "x2r= " << z->x2r;
- }
- if (d == 0)
- {
- cout << " x1r= " << z->x1r;
- cout << endl;
- }
- if (d < 0)
- {
- if (!zesp.empty())
- {
- pd = d;
- pd = fabs(d);
- cout << "Pierwiastek z delty= " << sqrt(pd) << endl << endl;
- cout << "x1r= " << zesp.at(0).real();
- cout << endl;
- //z->x2r = (z->x1r);
- cout << "x2r= " << zesp.at(0).real();
- cout << endl;
- cout << endl;
- cout << "x1u= " << zesp.at(0).imag();
- cout << endl;
- //z->u.x2u = ((zesp[0].imag())*-1);
- cout << "x2u = " << ((zesp.at(0).imag())*-1);
- cout << endl;
- cout << endl;
- }
- }
- pd = d;
- } // wyjscie z a!=0
- if (dane_we.at(0) == 0 && dane_we.at(1) != 0)
- {
- cout << "x1r=== " << z->x1r;
- cout << endl;
- system("pause");
- }
- if (dane_we.at(0) == 0 && dane_we.at(1) == 0 && dane_we.at(2) != 0)
- {
- cout << "Sprzecznosc";
- cout << endl;
- system("pause");
- exit(1);
- }
- if (dane_we.at(0) == 0 && dane_we.at(1) == 0 && dane_we.at(2) == 0)
- {
- cout << "tozsamosc";
- cout << endl;
- system("pause");
- exit(1);
- }
- if (dane_we.at(0) != 0)
- {
- if (d > 0)
- {
- cout << endl;
- cout << "Dodawanie sr= " << zesp.at(2).real() << endl;
- }
- else if (d < 0)
- {
- cout << endl;
- cout << "Dodawanie sr= " << zesp.at(2).real() << endl;
- cout << "Dodawanie su= " << zesp.at(2).imag() << endl;
- }
- if (d > 0)
- {
- cout << endl;
- cout << "Odejmowanie rr= " << zesp.at(3).real() << endl;
- }
- else if (d < 0)
- {
- cout << "Odejmowanie rr= " << zesp.at(3).real() << endl;
- cout << "Odejmowanie ru= " << zesp.at(3).imag() << endl;
- }
- if (d > 0)
- {
- cout << "Mnozenie ilr= " << zesp.at(4).real() << endl;
- }
- else if (d < 0)
- {
- cout << "Mnozenie ilr= " << zesp.at(4).real() << endl;
- cout << "Mnozenie ilu= " << zesp.at(4).imag() << endl;
- }
- if (d > 0)
- {
- if (z->x2r != 0)
- {
- cout << "Dzielenie= " << zesp.at(5).real() << endl;
- }
- }
- else if (d < 0)
- {
- cout << "Dzielenie irr =" << zesp.at(5).real() << endl;
- cout << "Dzielenie iru =" << zesp.at(5).imag() << endl;
- }
- }
- if (d < 0)
- {
- if (dane_we.at(0) != 0)
- {
- modul();
- cout << "Modul= " << md << endl;
- }
- }
- else
- {
- cout << "Modul= nie mozna a!=0 oraz d<0" << endl;
- }
- d = fabs(d);
- if (sqrt(d) != 0)
- {
- cout << "sqrt= " << sqrt(d) << endl;
- my_sqrt();
- cout << "my_sqrt= " << pd << endl;
- cout << "Roznica= " << sqrt(d) - pd << endl;
- }
- }
- void rownanie::modul() {
- if (!zesp.empty())
- {
- md = abs(zesp.at(0)) * 1.0;
- }
- };
- int main()
- {
- rownanie *obiekt = new rownanie;
- obiekt->formatuj_rownanie();
- obiekt->obliczd();
- obiekt->my_sqrt();
- obiekt->oblicz_pierwiastek();
- obiekt->dodaj();
- obiekt->odejmij();
- obiekt->pomnoz();
- obiekt->podziel();
- cout << endl << endl;
- obiekt->formatuj_l_zespol();
- cout << endl << endl;
- obiekt->modul();
- obiekt->wyswietl_wynik();
- delete obiekt;
- system("pause");
- urojona a;
- a.ilu = 10;
- urojona b;
- b.ilu = 10;
- urojona c=a + b;
- int dfdf = 2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement