Source.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <malloc.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. int is_number(char* str) {
  9. int dot = 0;
  10. while (*str) {
  11. if (*str == '.') {
  12. dot++;
  13. }
  14. else if(!isdigit(*str)&&*str != '-' && *str !='+') {
  15. return 0;
  16. }
  17. str++;
  18. }
  19. return (dot <= 1);
  20. }
  21. void read(double* arr) {
  22. FILE* file;
  23. char p[100];
  24. double d;
  25. file = fopen("1.txt", "r");
  26. FILE* file2;
  27. file2 = fopen("result.txt", "w");
  28. int n = 0;
  29. while ( fscanf(file, "%s", p) != EOF && is_number(p) && n < 3)
  30. {
  31. if (n == 0 && atof(p) == 0) {
  32. fprintf(file2,"Ýòî íå êâàäðàòíîå óðàâíåíèå");
  33. exit(1);
  34. }
  35. d = atof(p);
  36. *arr = d;
  37. arr++;
  38. n++;
  39. }
  40. if (n != 3)
  41. {
  42. fprintf(file2, "Ìàëî äàííûõ èëè îíè íåêîððåêòíûå");
  43. exit(1);
  44. }
  45. fclose(file);
  46. }
  47. void solve(double* arr) {
  48. double a = *arr++;
  49. double b = *arr++;
  50. double c = *arr;
  51. double d = b * b - 4 * a * c;
  52. double x1, x2;
  53. FILE* file;
  54. file = fopen("result.txt", "w");
  55. if (d < 0) {
  56. fprintf(file, "Äèñêðèìèíàíò ðàâåí %.3lf\n", d);
  57. fprintf(file, "Äåéñòâèòåëüíûõ êîðíåé íåò");
  58. return 0;
  59. }
  60. else if (d > 0) {
  61. x1 = (-b + sqrt(d)) / (2 * a);
  62. x2 = (-b - sqrt(d)) / (2 * a);
  63. fprintf(file, "Äèñêðèìèíàíò ðàâåí %.3lf\n", d);
  64. fprintf(file, "x1 = %.3lf\nx2 = %.3lf", x1, x2);
  65. return 0;
  66. }
  67. else {
  68. x1 = (-b) / (2 * a);
  69. fprintf(file, "Äèñêðèìèíàíò ðàâåí %.3lf\n", d);
  70. fprintf(file, "x1 = %.3lf\n", x1);
  71. return 0;
  72. }
  73. }
  74. int main()
  75. {
  76. system("chcp 1251 < nul");
  77. double* arr = malloc(3 * sizeof(double));
  78. read(arr);
  79. solve(arr);
  80. return 0;
  81. }