Advertisement
alch1337

Równania kwadratowe

May 7th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<string>
  4.  
  5. bool ZnajdzWspolczynniki(std::string& sRownanie, float& fA, float& fB, float& fC)
  6. {
  7.     int poz_A = -1, poz_B = -1;
  8.     for (unsigned i = 0; i<sRownanie.length(); i++)
  9.     {
  10.         if (sRownanie.substr(i, 3) == "x^2") { poz_A = i; }
  11.         else if (sRownanie.substr(i, 1) == "x") { poz_B = i; }
  12.     }
  13.  
  14.     if (poz_A == -1) { std::cout<<"To nie jest rownanie kwadratowe!"; return false; }
  15.     else
  16.     {
  17.         //A
  18.         if (poz_A == 0) { fA = 1; }
  19.         else
  20.         {
  21.             if ((sRownanie.substr(0,1) == "-") && poz_A -1 == 0) { fA = -1; }
  22.             else { fA = stof(sRownanie.substr(0, poz_A)); }
  23.         }
  24.         //B
  25.         if (poz_B == -1) { fB = 0; }
  26.         else
  27.         {
  28.             if (sRownanie.substr(poz_A+3, 2) == "-x") { fB = -1; }
  29.             else if (sRownanie.substr(poz_A+3, 2) == "+x") { fB = 1; }
  30.             else { fB = stof(sRownanie.substr(poz_A+3,(poz_B-1)-poz_A)); }
  31.         }
  32.         //C
  33.         if (sRownanie.substr(sRownanie.length()-3,1) != "x")
  34.         {
  35.             if (fB != 0) { fC = stof(sRownanie.substr(poz_B+1,(sRownanie.length()-3) - poz_B)); }
  36.             else { fC = stof(sRownanie.substr(poz_A +3, sRownanie.length()-poz_A)); }
  37.         }
  38.         else { fC = 0; }
  39.  
  40.         return true;
  41.     }
  42. }
  43.  
  44. bool ZnajdzPierwiastki(float& fA, float& fB, float& fC, float& fX1, float& fX2)
  45. {
  46.     int nDelta = (fB*fB) - (4*fA*fC);
  47.     std::cout<<"Delta: "<<nDelta<<std::endl;
  48.     if (nDelta < 0) { return false; }
  49.     else if (nDelta == 0) { fX1 = -fB/(2*fA); fX2 = fX1; return true; }
  50.     else { fX1 = (-fB+sqrt(nDelta))/(2*fA); fX2 = (-fB-sqrt(nDelta))/(2*fA); return true; }
  51.  
  52. }
  53.  
  54. int main()
  55. {
  56.     std::string sRownanie;
  57.     float fA, fB, fC, fX1, fX2;
  58.     std::cout<<"Podaj rownanie kwadratowe do rozwiazania! [ ax^2+bx+c=0 ]"<<std::endl;
  59.     std::cin>>sRownanie;
  60.     if (sRownanie.substr(sRownanie.length()-2, 2) != "=0") { std::cout<<"Bledna forma rownania!"; }
  61.     else
  62.     {
  63.         if(ZnajdzWspolczynniki(sRownanie, fA, fB, fC))
  64.         {
  65.             std::cout<<"A: "<<fA<<" B: "<<fB<<" C: "<<fC<<std::endl;
  66.             if (!ZnajdzPierwiastki(fA, fB, fC, fX1, fX2))
  67.             {
  68.                 std::cout<<"To rownanie nie ma pierwiastkow!";
  69.             }
  70.             else
  71.             {
  72.                 std::cout<<"x1 = "<<fX1<<"\nX2 = "<<fX2;
  73.             }
  74.         }
  75.     }
  76.  
  77.     _getch();
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement