QuadEq.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <math.h>
  2. #define FILE_IN "argument.txt"
  3. #define FILE_OUT "result.txt"
  4. #define BUFFER 512
  5. #define ERROR_OPEN_FILE -3
  6. #define ERROR_READ_DATA -4
  7. #define ERROR_INPUT_DATA -5
  8. #define ERROR_WRITE_DATA -6
  9. struct QuadraticEquation
  10. {
  11. //коэффициенты
  12. double a;
  13. double b;
  14. double c;
  15. /// <summary>
  16. /// Значение дискриминанта.
  17. /// </summary>
  18. double discriminant;
  19. /// <summary>
  20. /// Кол-во корней.
  21. /// </summary>
  22. int countRoot;
  23. //значения корней
  24. double x1;
  25. double x2;
  26. };
  27. typedef struct QuadraticEquation QuadraticEquation;
  28. void solutionQuadraticEquation(QuadraticEquation* equation)
  29. {
  30. equation->discriminant = equation->b * equation->b - 4 * equation->a * equation->c;
  31. if (equation->discriminant < 0)
  32. {
  33. equation->countRoot = 0;
  34. return;
  35. }
  36. if (equation->discriminant == 0)
  37. {
  38. equation->x1 = (-(equation->b) / (2 * equation->a));
  39. equation->countRoot = 1;
  40. return;
  41. }
  42. if (equation->discriminant > 0)
  43. {
  44. equation->x1 = (-equation->b + sqrtf(equation->discriminant)) / (2 * equation->a);
  45. equation->x2 = (-equation->b - sqrtf(equation->discriminant)) / (2 * equation->a);
  46. equation->countRoot = 2;
  47. return;
  48. }
  49. }