Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define NOT_FOUND 1
  5. #define FOUND 2
  6. #define FOUND_ONE 3
  7. #define INFINITE_MANY 4
  8. #define EPS 1e-20
  9. #define ERR_IO 5
  10. #define OK 0
  11.  
  12. int search_roots(float a, float b, float c, float *x1, float *x2)
  13. {
  14. int rc = FOUND;
  15. float d;
  16. if ((fabs(a) > EPS) && (abs(c) > EPS)) //??? > 0 // todo fabs
  17. {
  18. d = b*b - 4 * a * c;
  19. if (d > 0)
  20. {
  21. printf("1");
  22. *x1 = (-b + sqrt(d)) / (2 * a);
  23. *x2 = (-b - sqrt(d)) / (2 * a);
  24. return rc;
  25. }
  26. else if (d == 0)
  27. {
  28. printf("2");
  29. *x1 = (-b / (2 * a));
  30. rc = FOUND_ONE;
  31. return rc;
  32. }
  33. else
  34. {
  35. printf("3");
  36. rc = NOT_FOUND;
  37. return rc;
  38. }
  39. }
  40. else if (abs(a) < EPS) // a == 0
  41. {
  42. if (abs(b) > EPS) // b !=0
  43. {
  44. printf("4");
  45. *x1 = -c/b;
  46. rc = FOUND_ONE;
  47. return rc;
  48. }
  49. else if ((abs(b) < EPS) && (abs(c) > EPS)) // b==0 c!=0
  50. {
  51. printf("6");
  52. rc = NOT_FOUND;
  53. return rc;
  54. }
  55. else // b==0 c==0
  56. {
  57. printf("7");
  58. rc = INFINITE_MANY;
  59. return rc;
  60. }
  61. }
  62. else if (abs(c) < EPS) // c==0
  63. {
  64. printf("11");
  65. *x1 = 0;
  66. *x2 = -b / a;
  67. return rc;
  68. }
  69. return rc;
  70. }
  71. int main (void)
  72. {
  73. int rc = OK;
  74. float x1, x2;
  75. float a, b, c;
  76. printf("Input coefficients a, b, c \n");
  77. if (scanf("%f%f%f", &a, &b, &c) == 3)
  78. {
  79. rc = search_roots(a, b, c, &x1, &x2);
  80. if (rc == FOUND)
  81. {
  82. printf("Two roots were found, x1 = %.3f, x2 = %.3f", x1, x2);
  83. }
  84. else if (rc == FOUND_ONE)
  85. {
  86. printf("One root was found, x = %.3f", x1);
  87. }
  88. else if (rc == NOT_FOUND)
  89. {
  90. printf("Root were not found");
  91. }
  92. else if (rc == INFINITE_MANY)
  93. {
  94. printf("There are infinite many roots");
  95. }
  96. }
  97. else
  98. {
  99. printf("I/O error");
  100. rc = ERR_IO;
  101. }
  102. return rc;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement