Не работает программа на нахождение корней в квадратном уравнении. Не выполняются некоторые из условий - C (СИ)

Узнай цену своей работы

Формулировка задачи:

Хожу на курсы по обучению на С. задали написать программу, для расчета корней в квадратичном уравнении, а в случае нарушения устроить типо "Армагеддон" Сам армагеддон работает нормально, а вот программа для корней нет. Не выдает результат при b=0, и a=0. Вот код:
Листинг программы
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <windows.h>
  4. #include <conio.h>
  5. void do_armageddon ();
  6. int calculation_of_the_equation (double a, double b, double c);
  7. int calculation_of_the_normal_quadratic_equation (double a, double b, double c);
  8. int calculation_of_the_not_normal_quadratic_equation (double a, double c);
  9.  
  10. int main()
  11. {
  12. printf ("Hello, guys! You are students, I know it!\n"
  13. "And I also know, that you can save one universe!\n"
  14. "Enter a, b, c for ax^2 + bx + c = 0\n"
  15. "Or I will destroy one world in the multiverse!"
  16. "\nEnter please 3 numbers:\n");
  17. double a = 0, b = 0, c = 0;
  18. int nVals = scanf ("%lg %lg %lg,", &a, &b, &c);
  19. if (nVals != 3)
  20. {
  21. printf ("Learn the math, MEN! You have destroyed another one universe,"
  22. " like other guys!\n");
  23. do_armageddon ();
  24. getch ();
  25. return -666;
  26. }
  27. else
  28. {
  29. calculation_of_the_equation (a, b, c);
  30. }
  31. }
  32.  
  33. int calculation_of_the_equation (double a, double b, double c)
  34. {
  35. if (a != 0)
  36. if (b != 0)
  37. if (c != 0)
  38. {
  39. calculation_of_the_normal_quadratic_equation (a, b, c);
  40. }
  41. else if (b == 0)
  42. {
  43. calculation_of_the_not_normal_quadratic_equation (a, c);
  44. }
  45. else if (a == 0 && c != 0)
  46. {
  47. double x = -c / b;
  48. printf ("It's linear equation and answer is:\n"
  49. "x = %lg", x);
  50. }
  51. else if (c == 0 && a != 0)
  52. {
  53. double x = -a / b;
  54. printf ("It's linear equation and answer is:\n"
  55. "x = %lg", x);
  56. }
  57. getch ();
  58. return 0;
  59. }
  60. int calculation_of_the_not_normal_quadratic_equation (double a, double c)
  61. {
  62. double d = 0 - 4*a*c;
  63. double x1 = (0 + sqrt(d))/(2*a);
  64. double x2 = (0 - sqrt(d))/(2*a);
  65. if (d > 0)
  66. {
  67. printf ("Mission complete! Results are:\n");
  68. printf ("x1 = %lg and ", x1);
  69. printf ("x2 = %lg\n", x2);
  70. printf ("Thanks, man!");
  71. printf ("\nPress any key to exit.");
  72. }
  73. else if (d == 0)
  74. {
  75. printf ("You entrered the worst set of numbers in the world, "
  76. "but we have 1 result...\n");
  77. printf ("This result shows us that:\n");
  78. printf ("x = %lg", x1);
  79. printf ("\nThanks, man!");
  80. printf ("\nPress any key to exit.");
  81. }
  82. else if (d < 0)
  83. {
  84. printf ("There is nothing... Bad numbers!");
  85. printf ("\nPress any key to exit.");
  86. }
  87. }
  88. int calculation_of_the_normal_quadratic_equation (double a, double b, double c)
  89. {
  90. double d = b*b - 4*a*c;
  91. double x1 = (-b + sqrt(d))/(2*a);
  92. double x2 = (-b - sqrt(d))/(2*a);
  93. if (d > 0)
  94. {
  95. printf ("Mission complete! Results are:\n");
  96. printf ("x1 = %lg and ", x1);
  97. printf ("x2 = %lg\n", x2);
  98. printf ("Thanks, man!");
  99. printf ("\nPress any key to exit.");
  100. }c5BpMyAp
  101. else if (d == 0)
  102. {
  103. printf ("You entrered the worst set of numbers in the world, "
  104. "but we have 1 result...\n");
  105. printf ("This result shows us that:\n");
  106. printf ("x = %lg", x1);
  107. printf ("\nThanks, man!");
  108. printf ("\nPress any key to exit.");
  109. }
  110. else if (d < 0)
  111. {
  112. printf ("There is nothing... Bad numbers!");
  113. printf ("\nPress any key to exit.");
  114. }
  115. }
  116.  
  117. void do_armageddon ()
  118. {
  119. int i = 0;
  120. while (i <= 100)
  121. {
  122. printf ("Destroying the universe ... %d%%\r", i);
  123. Sleep (100);
  124. i += 1;
  125. }
  126. printf ("\nArmagedon done! Thanks!\n");
  127. printf ("Press any key to leaving the world...\n");
  128. }
Я проверил еще отдельно каждую функцию. Они все по отдельности работают нормально и выдают требуемый результат. Почему они не работают нормально вместе?

Решение задачи: «Не работает программа на нахождение корней в квадратном уравнении. Не выполняются некоторые из условий»

textual
Листинг программы
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <windows.h>
  4. #include <conio.h>
  5.  
  6. void do_armageddon();
  7. void calculation_of_the_equation(double a, double b, double c);
  8. void calculation_of_the_normal_quadratic_equation(double a, double b, double c);
  9. void calculation_of_the_not_normal_quadratic_equation(double a, double c);
  10.  
  11. void main()
  12. {
  13.     printf("Hello, guys! You are students, I know it!\n"
  14.         "And I also know, that you can save one universe!\n"
  15.         "Enter a, b, c for ax^2 + bx + c = 0\n"
  16.         "Or I will destroy one world in the multiverse!"
  17.         "\nEnter please 3 numbers:\n");
  18.  
  19.     double a = 0, b = 0, c = 0;
  20.     int nVals = scanf("%lg %lg %lg,", &a, &b, &c);
  21.  
  22.     if (nVals != 3)
  23.     {
  24.         printf("Learn the math, MEN! You have destroyed another one universe,"
  25.             " like other guys!\n");
  26.  
  27.         do_armageddon();
  28.     }
  29.     else
  30.     {
  31.         calculation_of_the_equation(a, b, c);
  32.     }
  33.     getch();
  34. }
  35.  
  36.  
  37. void calculation_of_the_equation(double a, double b, double c)
  38. {
  39.     if (a != 0 && b != 0 && c != 0)
  40.     {
  41.         calculation_of_the_normal_quadratic_equation(a, b, c);
  42.     }
  43.     else if (b == 0)
  44.     {
  45.         calculation_of_the_not_normal_quadratic_equation(a, c);
  46.     }
  47.     else if (a == 0 && c != 0)
  48.     {
  49.         double x = -c / b;
  50.         printf("It's linear equation and answer is:\n"
  51.             "x = %lg", x);
  52.     }
  53.     else if (c == 0 && a != 0)
  54.     {
  55.         double x = -a / b;
  56.         printf("It's linear equation and answer is:\n"
  57.             "x = %lg", x);
  58.     }
  59.  
  60.     getch();
  61. }
  62.  
  63. void calculation_of_the_not_normal_quadratic_equation(double a, double c)
  64. {
  65.     double d = 0 - 4 * a*c;
  66.     double x1 = (0 + sqrt(d)) / (2 * a);
  67.     double x2 = (0 - sqrt(d)) / (2 * a);
  68.  
  69.     if (d > 0)
  70.     {
  71.         printf("Mission complete! Results are:\n");
  72.         printf("x1 = %lg and ", x1);
  73.         printf("x2 = %lg\n", x2);
  74.         printf("Thanks, man!");
  75.         printf("\nPress any key to exit.");
  76.     }
  77.     else if (d == 0)
  78.     {
  79.         printf("You entrered the worst set of numbers in the world, "
  80.             "but we have 1 result...\n");
  81.         printf("This result shows us that:\n");
  82.         printf("x = %lg", x1);
  83.         printf("\nThanks, man!");
  84.         printf("\nPress any key to exit.");
  85.     }
  86.     else if (d < 0)
  87.     {
  88.         printf("There is nothing... Bad numbers!");
  89.         printf("\nPress any key to exit.");
  90.     }
  91. }
  92.  
  93. void calculation_of_the_normal_quadratic_equation(double a, double b, double c)
  94. {
  95.     double d = b*b - 4 * a*c;
  96.     double x1 = (-b + sqrt(d)) / (2 * a);
  97.     double x2 = (-b - sqrt(d)) / (2 * a);
  98.  
  99.     if (d > 0)
  100.     {
  101.         printf("Mission complete! Results are:\n");
  102.         printf("x1 = %lg and ", x1);
  103.         printf("x2 = %lg\n", x2);
  104.         printf("Thanks, man!");
  105.         printf("\nPress any key to exit.");
  106.     }
  107.     else if (d == 0)
  108.     {
  109.         printf("You entrered the worst set of numbers in the world, "
  110.             "but we have 1 result...\n");
  111.         printf("This result shows us that:\n");
  112.         printf("x = %lg", x1);
  113.         printf("\nThanks, man!");
  114.         printf("\nPress any key to exit.");
  115.     }
  116.     else  if (d < 0)
  117.     {
  118.         printf("There is nothing... Bad numbers!");
  119.         printf("\nPress any key to exit.");
  120.     }
  121.  
  122. }
  123.  
  124. void do_armageddon()
  125. {
  126.     int i = 0;
  127.     while (i <= 100)
  128.     {
  129.         printf("Destroying the universe ... %d%%\r", i);
  130.         Sleep(100);
  131.         i += 1;
  132.     }
  133.     printf("\nArmagedon done! Thanks!\n");
  134.     printf("Press any key to leaving the world...\n");
  135.  
  136. }

Объяснение кода листинга программы

  1. Программа предназначена для решения квадратных уравнений.
  2. Пользователю предлагается ввести значения a, b и c.
  3. Если пользователь вводит некорректные значения, программа выдает сообщение об ошибке и вызывает функцию do_armageddon().
  4. Если пользователь вводит корректные значения, программа вызывает функцию calculation_of_the_equation(a, b, c).
  5. Внутри функции calculation_of_the_equation() происходит проверка значений a, b и c.
  6. Если b равно нулю, программа вызывает функцию calculation_of_the_not_normal_quadratic_equation(a, c).
  7. Если a равно нулю и c не равно нулю, программа вычисляет значение x как -c/b и выводит его.
  8. Если c равно нулю и a не равно нулю, программа вычисляет значение x как -a/b и выводит его.
  9. Если a, b и c не равны нулю, программа вызывает функцию calculation_of_the_normal_quadratic_equation(a, b, c).
  10. Внутри функции calculation_of_the_normal_quadratic_equation() происходит вычисление дискриминанта по формуле d = bb - 4a*c.
  11. Затем программа вычисляет значения x1 и x2 по формулам x1 = (-b + sqrt(d)) / (2a) и x2 = (-b - sqrt(d)) / (2a).
  12. Если d больше нуля, программа выводит значения x1 и x2.
  13. Если d равно нулю, программа выводит только x1.
  14. Если d меньше нуля, программа выводит сообщение о том, что корней нет.
  15. Если пользователь вводит некорректные значения, программа вызывает функцию do_armageddon().
  16. Функция do_armageddon() выводит сообщение о том, что Армагеддон завершен.
  17. Функция do_armageddon() использует цикл while для отображения процесса уничтожения вселенной.
  18. В конце программы пользователю предлагается нажать любую клавишу для выхода из программы.

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.556 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы