Solve.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <windows.h>
  6. int main(int argc, char* argv[]) {
  7. system("chcp 1251>null");
  8. if (argc != 4) {
  9. printf("Íåâåðíîå êîëè÷åñòâî àðãóìåíòîâ.\n");
  10. return 1;
  11. }
  12. float a = atof(argv[1]);
  13. float b = atof(argv[2]);
  14. float c = atof(argv[3]);
  15. FILE* output_file = fopen("output.txt", "w");
  16. if (output_file == NULL) {
  17. printf("Ôàéë äëÿ âûâîäà íå íàéäåí.\n");
  18. return 1;
  19. }
  20. if (a == 0) {
  21. fprintf(output_file, "Ýòî íå êâàäðàòíîå óðàâíåíèå\n");
  22. fclose(output_file);
  23. return 0;
  24. }
  25. //äëÿ ïðîâåðêè äàííûõ
  26. /*printf("Ïîëó÷åíî àðãóìåíòîâ: %d\n", argc);
  27. for (int i = 0; i < argc; i++) {
  28. printf("argv[%d] = %s\n", i, argv[i]);
  29. }*/
  30. float discriminant = b * b - 4 * a * c;
  31. int rootCount = 0;
  32. if (discriminant > 0) {
  33. float x1 = (-b + sqrt(discriminant)) / (2 * a);
  34. float x2 = (-b - sqrt(discriminant)) / (2 * a);
  35. fprintf(output_file, "Äèñêðèìèíàíò:\n%.1f\n", discriminant);
  36. fprintf(output_file, "Äâà äåéñòâèòåëüíûõ êîðíÿ:\nx1 = %.1f\nx2 = %.1f\n", x1, x2);
  37. rootCount = 2;
  38. printf("Â ôàéë output.txt çàïèñàíû äâà äåéñòâèòåëüíûõ êîðíÿ");
  39. }
  40. else if (discriminant == 0) {
  41. float x = -b / (2 * a);
  42. fprintf(output_file, "Äèñêðèìèíàíò:\n%.1f\n", discriminant);
  43. fprintf(output_file, "Îäèí äåéñòâèòåëüíûé êîðåíü:\nx = %.1f\n", x);
  44. rootCount = 1;
  45. printf("Â ôàéë output.txt çàïèñàí îäèí äåéñòâèòåëüíûé êîðåíü");
  46. }
  47. else {
  48. fprintf(output_file, "Êîðíåé íåò, ïðè÷èíà -> ");
  49. fprintf(output_file, "Äèñêðèìèíàíò:\n%.1f\n", discriminant);
  50. rootCount = 0;
  51. printf("Â ôàéëå output.txt íåò êîðíåé");
  52. }
  53. fclose(output_file);
  54. return rootCount;
  55. }