fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7.  
  8. /* ===== PRZYPADEK 1 =====
  9.   3x^2 + 4x + 5 = 0
  10.   */
  11. {
  12. double a = 3.0;
  13. double b = 4.0;
  14. double c = 5.0;
  15.  
  16. double delta = b * b - 4 * a * c;
  17. cout << "Rownanie: 3x^2 + 4x + 5 = 0" << endl;
  18. cout << "Delta = " << delta << endl;
  19.  
  20. if (delta < 0) {
  21. cout << "Brak pierwiastkow rzeczywistych." << endl;
  22. }
  23. cout << endl;
  24. }
  25.  
  26. /* ===== PRZYPADEK 2 =====
  27.   x^2 + 10000x + 1 = 0
  28.   float + Vieta
  29.   */
  30. {
  31. float a = 1.0f;
  32. float b = 10000.0f;
  33. float c = 1.0f;
  34.  
  35. float delta = b * b - 4 * a * c;
  36. cout << "Rownanie: x^2 + 10000x + 1 = 0" << endl;
  37. cout << "Delta = " << delta << endl;
  38.  
  39. if (delta > 0) {
  40. // liczymy tylko jeden pierwiastek klasycznie
  41. float x1 = (-b - sqrt(delta)) / (2 * a);
  42.  
  43. // drugi z wzoru Viety
  44. float x2 = (c / a) / x1;
  45.  
  46. cout << "Pierwiastki (delta + Vieta):" << endl;
  47. cout << "x1 = " << x1 << endl;
  48. cout << "x2 = " << x2 << endl;
  49. }
  50.  
  51. cout << endl;
  52. }
  53.  
  54. return 0;
  55. }
  56.  
  57.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Rownanie: 3x^2 + 4x + 5 = 0
Delta = -44
Brak pierwiastkow rzeczywistych.

Rownanie: x^2 + 10000x + 1 = 0
Delta = 1e+08
Pierwiastki (delta + Vieta):
x1 = -10000
x2 = -0.0001