fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. void rownanie(double a, double b, double c)
  7. {
  8. cout << "Rownanie: " << a << "x^2 + " << b << "x + " << c << " = 0" << endl;
  9.  
  10. double delta = b * b - 4 * a * c;
  11. cout << "Delta = " << delta << endl;
  12.  
  13. if (delta >= 0)
  14. {
  15. double x1 = (-b - sqrt(delta)) / (2 * a);
  16. double x2 = (-b + sqrt(delta)) / (2 * a);
  17.  
  18. cout << "Pierwiastki (z delty):" << endl;
  19. cout << "x1 = " << x1 << endl;
  20. cout << "x2 = " << x2 << endl;
  21.  
  22. cout << "Sprawdzenie wzorami Viety:" << endl;
  23. cout << "x1 + x2 = " << x1 + x2
  24. << " (powinno byc: " << -b / a << ")" << endl;
  25. cout << "x1 * x2 = " << x1 * x2
  26. << " (powinno byc: " << c / a << ")" << endl;
  27. }
  28. else
  29. {
  30. cout << "Brak pierwiastkow rzeczywistych." << endl;
  31. }
  32.  
  33. cout << "-----------------------------" << endl;
  34. }
  35.  
  36. int main()
  37. {
  38. // 1) a = 5, b = 4, c = 2
  39. rownanie(5, 4, 2);
  40.  
  41. // 2) x^2 + 10000x + 1 = 0
  42. rownanie(1, 10000, 1);
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Rownanie: 5x^2 + 4x + 2 = 0
Delta = -24
Brak pierwiastkow rzeczywistych.
-----------------------------
Rownanie: 1x^2 + 10000x + 1 = 0
Delta = 1e+08
Pierwiastki (z delty):
x1 = -10000
x2 = -0.0001
Sprawdzenie wzorami Viety:
x1 + x2 = -10000   (powinno byc: -10000)
x1 * x2 = 1   (powinno byc: 1)
-----------------------------