Advertisement
Guest User

Untitled

a guest
May 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <vector>
  4. #include <complex>
  5.  
  6. using namespace std;
  7.  
  8. struct urojona
  9. {
  10.     float x1u, x2u, su, ru, ilu, iru;
  11.     urojona operator+(const urojona&);
  12. };
  13. struct zespolona
  14. {
  15.     float x1r, x2r, sr, rr, ilr, irr;
  16.     urojona u;
  17. };
  18. class rownanie
  19. {
  20.     vector <int> dane_we;
  21.     vector < complex <float> > zesp;
  22.     float d, pd, md;
  23.     zespolona *z;
  24. public:
  25.     rownanie();
  26.     ~rownanie();
  27.     void formatuj_rownanie();
  28.     void obliczd();
  29.     void my_sqrt();
  30.     void formatuj_l_zespol();
  31.     void oblicz_pierwiastek();
  32.     void dodaj();
  33.     void odejmij();
  34.     void pomnoz();
  35.     void podziel();
  36.     void wyswietl_wynik();
  37.     void modul();
  38.  
  39. };
  40.  
  41. urojona urojona::operator+(const urojona& r)
  42. {
  43.     urojona l;
  44.     l.x1u = x1u+r.x1u;
  45.     l.x2u = x2u+r.x2u;
  46.     l.su = su+ r.su;
  47.     l.ru = ru+r.ru;
  48.     l.ilu =ilu+r.ilu;
  49.     l.iru =iru+r.iru;
  50.     return l;
  51. }
  52.  
  53. rownanie::rownanie()
  54. {
  55.     z = new zespolona;
  56.     int zmienna;
  57.  
  58.  
  59.     cout << "Podaj a: ";
  60.     cin >> zmienna;
  61.     dane_we.push_back(zmienna);
  62.     cout << "Podaj b: ";
  63.     cin >> zmienna;
  64.     dane_we.push_back(zmienna);
  65.     cout << "Podaj c: ";
  66.     cin >> zmienna;
  67.     dane_we.push_back(zmienna);
  68.  
  69. }
  70. rownanie::~rownanie()
  71. {
  72.     // dane_we[];
  73.     delete z;
  74.     z = NULL;
  75. }
  76. void rownanie::formatuj_rownanie()
  77. {
  78.     if (dane_we.at(0) == 0);    // print a
  79.     else if (dane_we.at(0) == 1)    cout << "xx";
  80.     else if (dane_we.at(0) == -1)   cout << "-xx";
  81.     else                cout << dane_we.at(0) << "xx";
  82.  
  83.     if (dane_we.at(0) != 0) // print b
  84.     {
  85.         if ((dane_we.at(1)) == 0);
  86.         else if (dane_we.at(1) == 1)    cout << " + x";
  87.         else if (dane_we.at(1) == -1)   cout << " - x";
  88.         else if ((dane_we.at(1)) < 1)       cout << " - " << -1 * dane_we.at(1) << "x";
  89.         else                cout << " + " << dane_we.at(1) << "x";
  90.     }
  91.     else if (dane_we.at(0) == 0)
  92.     {
  93.         if (dane_we.at(1) == 0);
  94.         else if (dane_we.at(1) == 1)    cout << "x";
  95.         else if (dane_we.at(1) == -1)   cout << "-x";
  96.         else                cout << dane_we.at(1) << "x";
  97.     }
  98.  
  99.     if (dane_we.at(0) == 0 && dane_we.at(1) == 0 && dane_we.at(2) == 0) cout << "0";    // print c
  100.     else if (dane_we.at(0) == 0 && dane_we.at(1) == 0)
  101.     {
  102.         if (dane_we.at(2) == 0);
  103.         else if (dane_we.at(2) > 0)     cout << dane_we.at(2);
  104.         else                cout << dane_we.at(2);
  105.     }
  106.     else if (dane_we.at(0) != 0 || dane_we.at(1) != 0)
  107.     {
  108.         if (dane_we.at(2) == 0);
  109.         else if (dane_we.at(2) > 0)     cout << " + " << dane_we.at(2);
  110.         else                cout << " - " << -1 * dane_we.at(2);
  111.     }
  112.  
  113.     // end printing
  114. }
  115. void rownanie::obliczd()
  116. {
  117.     d = dane_we.at(1) * dane_we.at(1) - 4 * dane_we.at(0) *dane_we.at(2);
  118. }
  119.  
  120. void rownanie::my_sqrt()
  121. {
  122.     if (d < 0)
  123.     {
  124.         pd = (d*(-1));
  125.         pd = (pd + (d*(-1)) / pd) / 2;
  126.         pd = (pd + (d*(-1)) / pd) / 2;
  127.         pd = (pd + (d*(-1)) / pd) / 2;
  128.  
  129.     }
  130.     else
  131.     {
  132.         pd = d;
  133.         pd = (pd + d / pd) / 2;
  134.         pd = (pd + d / pd) / 2;
  135.         pd = (pd + d / pd) / 2;
  136.  
  137.     }
  138. }
  139.  
  140. void rownanie::formatuj_l_zespol()
  141. {
  142.     if (!zesp.empty())
  143.     {
  144.         if (d < 0)
  145.         {
  146.             if (zesp.at(0).imag() < 0) //x1u
  147.             {
  148.                 cout << "z1= ";
  149.                 cout << "" << zesp.at(0).real() << zesp.at(0).imag() << "*i";
  150.                 cout << endl;
  151.             }
  152.             else
  153.             {
  154.                 cout << "z1= ";
  155.                 cout << "" << zesp.at(0).real() << " + " << zesp.at(0).imag() << "*i";
  156.                 cout << endl;
  157.             }
  158.  
  159.             if (zesp.at(1).imag() < 0)//z2=x2r+x2u
  160.             {
  161.                 cout << "z2= ";
  162.                 cout << "" << zesp.at(1).real() << zesp.at(1).imag() << "*i";
  163.                 cout << endl;
  164.             }
  165.             else
  166.             {
  167.                 cout << "z2= ";
  168.                 cout << "" << zesp.at(1).real() << " + " << zesp.at(1).imag() << "*i";
  169.                 cout << endl;
  170.             }
  171.         }
  172.         if (d < 0)
  173.         {
  174.             if (zesp.at(2).imag() < 0) //sr+su
  175.             {
  176.                 cout << "z (sr + su)=  ";
  177.                 cout << "" << zesp.at(2).real() << zesp.at(2).imag() << "*i";
  178.                 cout << endl;
  179.             }
  180.             else
  181.             {
  182.                 cout << "z (sr + su)=  ";
  183.                 cout << "" << zesp.at(2).real() << " + " << zesp.at(2).imag() << "*i";
  184.                 cout << endl;
  185.             }
  186.  
  187.             if (zesp.at(3).imag() < 0) //rr+ru
  188.             {
  189.                 cout << "z (rr + ru)=  ";
  190.                 cout << "" << zesp.at(3).real() << zesp.at(3).imag() << "*i";
  191.                 cout << endl;
  192.             }
  193.             else
  194.             {
  195.                 cout << "z (rr + ru)=  ";
  196.                 cout << "" << zesp.at(3).real() << " + " << zesp.at(3).imag() << "*i";
  197.                 cout << endl;
  198.             }
  199.  
  200.             if (zesp.at(4).imag() < 0) //ilr+ilu
  201.             {
  202.                 cout << "z (ilr + ilu)=  ";
  203.                 cout << "" << zesp.at(4).real() << zesp.at(4).imag() << "*i";
  204.                 cout << endl;
  205.             }
  206.             else
  207.             {
  208.                 cout << "z (ilr + iluu)=  ";
  209.                 cout << "" << zesp.at(4).real() << " + " << zesp.at(4).imag() << "*i";
  210.                 cout << endl;
  211.             }
  212.  
  213.  
  214.             if (zesp.at(5).imag() < 0) //irr+iru
  215.             {
  216.                 cout << "z (irr + iru)=  ";
  217.                 cout << "" << zesp.at(5).real() << zesp.at(5).imag() << "*i";
  218.                 cout << endl;
  219.             }
  220.             else
  221.             {
  222.                 cout << "z (irr + iru)=  ";
  223.                 cout << "" << zesp.at(5).real() << " + " << zesp.at(5).imag() << "*i";
  224.                 cout << endl;
  225.             }
  226.  
  227.         }
  228.     }
  229. }
  230.  
  231. void rownanie::oblicz_pierwiastek()
  232. {
  233.  
  234.     if (dane_we.at(0) != 0)
  235.     {
  236.         if (d > 0)
  237.         {
  238.  
  239.             z->x1r = ((-1 * dane_we.at(1)) - sqrt(d)) / (2 * dane_we.at(0));
  240.             z->x2r = ((-1 * dane_we.at(1)) + sqrt(d)) / (2 * dane_we.at(0));
  241.             zesp.push_back(complex <float>(z->x1r, 0));
  242.             zesp.push_back(complex <float>(z->x2r, 0));
  243.         }
  244.         if (d == 0)
  245.         {
  246.             z->x1r = (-1 * dane_we.at(1)) / 2.0 * dane_we.at(0);
  247.         }
  248.  
  249.         if (d < 0)
  250.         {
  251.  
  252.             d = fabs(d);
  253.             z->x1r = (-1 * dane_we.at(1)) / (2.0 * dane_we.at(0));
  254.             z->x2r = (-1 * dane_we.at(1)) / (2.0 * dane_we.at(0));
  255.             z->u.x1u = (-1 * sqrt(d)) / (2.0 *dane_we.at(0));
  256.             zesp.push_back(complex <float>(z->x1r, z->u.x1u));
  257.             z->u.x2u = (zesp.at(0).imag()*-1);
  258.  
  259.             zesp.push_back(complex <float>(z->x2r, z->u.x2u));;
  260.         }
  261.  
  262.     } // wyjscie z a!=0
  263.  
  264.     if (dane_we.at(0) == 0 && dane_we.at(1) != 0)
  265.     {
  266.         z->x1r = (1.0 * (-1 * dane_we.at(2))) / (dane_we.at(1) * 1.0);
  267.     }
  268. }
  269. void rownanie::dodaj()
  270. {
  271.     if (!zesp.empty())
  272.     {
  273.         obliczd();
  274.         if (d > 0)
  275.         {
  276.             z->sr = zesp.at(0).real() + zesp.at(1).real();
  277.             zesp.push_back(complex <float>(z->sr, 0));
  278.         }
  279.         else if (d < 0)
  280.         {
  281.             z->sr = zesp.at(0).real() + zesp[1].real();
  282.             z->u.su = zesp.at(0).imag() + zesp.at(1).imag();
  283.             zesp.push_back(complex <float>(z->sr, z->u.su));
  284.         }
  285.     }
  286. }
  287.  
  288.  
  289. void rownanie::odejmij()
  290. {
  291.     if (!zesp.empty())
  292.     {
  293.         obliczd();
  294.         if (d > 0)
  295.         {
  296.             cout << endl;
  297.             z->rr = zesp.at(0).real() - zesp.at(1).real();
  298.             zesp.push_back(complex <float>(z->rr, 0));
  299.         }
  300.         else if (d < 0)
  301.         {
  302.             z->rr = zesp.at(0).real() - zesp.at(1).real();
  303.             z->u.ru = zesp.at(0).imag() - zesp.at(1).imag();
  304.             zesp.push_back(complex <float>(z->rr, z->u.ru));
  305.         }
  306.     }
  307. }
  308.  
  309. void rownanie::pomnoz()
  310. {
  311.  
  312.     if (!zesp.empty())
  313.     {
  314.         obliczd();
  315.         if (d > 0)
  316.         {
  317.             cout << endl;
  318.             z->ilr = zesp.at(0).real() * zesp.at(1).real();
  319.             zesp.push_back(complex <float>(z->ilr, 0));
  320.         }
  321.         else if (d < 0)
  322.         {
  323.             cout << endl;
  324.             z->ilr = (zesp.at(0).real() * zesp.at(1).real()) - (zesp.at(0).imag() * zesp.at(1).imag());
  325.             z->u.ilu = (zesp.at(0).real() * zesp.at(1).imag()) + (zesp.at(1).real() * zesp.at(0).imag());
  326.             zesp.push_back(complex <float>(z->ilr, z->u.ilu));
  327.         }
  328.     }
  329. }
  330.  
  331.  
  332. void rownanie::podziel()
  333. {
  334.     if (!zesp.empty())
  335.     {
  336.         obliczd();
  337.         if (d > 0)
  338.         {
  339.             if (z->x2r != 0)
  340.             {
  341.  
  342.                 z->irr = zesp.at(0).real() / zesp.at(1).real();
  343.                 zesp.push_back(complex <float>(z->irr, 0));
  344.             }
  345.         }
  346.         else if (d < 0)
  347.         {
  348.  
  349.             z->irr = ((zesp.at(0).real() * zesp.at(1).real()) + (zesp.at(0).imag() * zesp.at(1).imag())) / ((norm(zesp.at(1))));
  350.             z->u.iru = ((zesp.at(1).real() * zesp.at(0).imag()) - (zesp.at(0).real() * zesp.at(1).imag())) / ((norm(zesp.at(1))));
  351.             zesp.push_back(complex <float>(z->irr, z->u.iru));
  352.         }
  353.     }
  354. }
  355.  
  356.  
  357. void rownanie::wyswietl_wynik()
  358. {
  359.  
  360.     if (dane_we.at(0) != 0)
  361.     {
  362.         obliczd();
  363.         cout << " d= " << d << endl;
  364.         if ((d) > 0)
  365.         {
  366.             cout << "Pierwiastek wynosi= " << sqrt(d) << endl << endl;
  367.             cout << "x1r= " << z->x1r;
  368.             cout << endl;
  369.             cout << "x2r= " << z->x2r;
  370.         }
  371.  
  372.         if (d == 0)
  373.         {
  374.             cout << " x1r= " << z->x1r;
  375.             cout << endl;
  376.         }
  377.  
  378.         if (d < 0)
  379.         {
  380.             if (!zesp.empty())
  381.             {
  382.                 pd = d;
  383.                 pd = fabs(d);
  384.  
  385.                 cout << "Pierwiastek z delty= " << sqrt(pd) << endl << endl;
  386.  
  387.                 cout << "x1r= " << zesp.at(0).real();
  388.                 cout << endl;
  389.                 //z->x2r = (z->x1r);
  390.                 cout << "x2r= " << zesp.at(0).real();
  391.                 cout << endl;
  392.                 cout << endl;
  393.  
  394.                 cout << "x1u= " << zesp.at(0).imag();
  395.                 cout << endl;
  396.                 //z->u.x2u = ((zesp[0].imag())*-1);
  397.                 cout << "x2u = " << ((zesp.at(0).imag())*-1);
  398.                 cout << endl;
  399.                 cout << endl;
  400.  
  401.             }
  402.         }
  403.         pd = d;
  404.     } // wyjscie z a!=0
  405.  
  406.     if (dane_we.at(0) == 0 && dane_we.at(1) != 0)
  407.     {
  408.         cout << "x1r=== " << z->x1r;
  409.         cout << endl;
  410.         system("pause");
  411.     }
  412.  
  413.  
  414.     if (dane_we.at(0) == 0 && dane_we.at(1) == 0 && dane_we.at(2) != 0)
  415.     {
  416.         cout << "Sprzecznosc";
  417.         cout << endl;
  418.         system("pause");
  419.         exit(1);
  420.     }
  421.  
  422.     if (dane_we.at(0) == 0 && dane_we.at(1) == 0 && dane_we.at(2) == 0)
  423.     {
  424.         cout << "tozsamosc";
  425.         cout << endl;
  426.         system("pause");
  427.         exit(1);
  428.     }
  429.  
  430.  
  431.     if (dane_we.at(0) != 0)
  432.     {
  433.         if (d > 0)
  434.         {
  435.             cout << endl;
  436.             cout << "Dodawanie sr= " << zesp.at(2).real() << endl;
  437.         }
  438.         else if (d < 0)
  439.         {
  440.             cout << endl;
  441.             cout << "Dodawanie sr= " << zesp.at(2).real() << endl;
  442.             cout << "Dodawanie su= " << zesp.at(2).imag() << endl;
  443.         }
  444.  
  445.         if (d > 0)
  446.         {
  447.             cout << endl;
  448.             cout << "Odejmowanie rr= " << zesp.at(3).real() << endl;
  449.         }
  450.         else if (d < 0)
  451.         {
  452.             cout << "Odejmowanie rr= " << zesp.at(3).real() << endl;
  453.             cout << "Odejmowanie ru= " << zesp.at(3).imag() << endl;
  454.         }
  455.  
  456.         if (d > 0)
  457.         {
  458.             cout << "Mnozenie ilr= " << zesp.at(4).real() << endl;
  459.         }
  460.         else if (d < 0)
  461.         {
  462.             cout << "Mnozenie ilr= " << zesp.at(4).real() << endl;
  463.             cout << "Mnozenie ilu= " << zesp.at(4).imag() << endl;
  464.         }
  465.         if (d > 0)
  466.         {
  467.             if (z->x2r != 0)
  468.             {
  469.                 cout << "Dzielenie= " << zesp.at(5).real() << endl;
  470.             }
  471.         }
  472.         else if (d < 0)
  473.         {
  474.             cout << "Dzielenie irr =" << zesp.at(5).real() << endl;
  475.             cout << "Dzielenie iru =" << zesp.at(5).imag() << endl;
  476.         }
  477.     }
  478.  
  479.     if (d < 0)
  480.     {
  481.         if (dane_we.at(0) != 0)
  482.         {
  483.             modul();
  484.             cout << "Modul= " << md << endl;
  485.         }
  486.     }
  487.     else
  488.     {
  489.         cout << "Modul= nie mozna  a!=0 oraz d<0" << endl;
  490.     }
  491.     d = fabs(d);
  492.     if (sqrt(d) != 0)
  493.     {
  494.  
  495.  
  496.         cout << "sqrt= " << sqrt(d) << endl;
  497.         my_sqrt();
  498.         cout << "my_sqrt= " << pd << endl;
  499.         cout << "Roznica= " << sqrt(d) - pd << endl;
  500.     }
  501. }
  502.  
  503. void rownanie::modul() {
  504.     if (!zesp.empty())
  505.     {
  506.         md = abs(zesp.at(0)) * 1.0;
  507.  
  508.     }
  509. };
  510.  
  511. int main()
  512. {
  513.     rownanie *obiekt = new rownanie;
  514.  
  515.  
  516.     obiekt->formatuj_rownanie();
  517.     obiekt->obliczd();
  518.     obiekt->my_sqrt();
  519.     obiekt->oblicz_pierwiastek();
  520.     obiekt->dodaj();
  521.     obiekt->odejmij();
  522.     obiekt->pomnoz();
  523.     obiekt->podziel();
  524.     cout << endl << endl;
  525.     obiekt->formatuj_l_zespol();
  526.     cout << endl << endl;
  527.     obiekt->modul();
  528.     obiekt->wyswietl_wynik();
  529.  
  530.     delete obiekt;
  531.     system("pause");
  532.  
  533.  
  534.  
  535.     urojona a;
  536.     a.ilu = 10;
  537.     urojona b;
  538.     b.ilu = 10;
  539.     urojona c=a + b;
  540.     int dfdf = 2;
  541.     return 0;
  542. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement