Не работает программа на нахождение корней в квадратном уравнении. Не выполняются некоторые из условий - C (СИ)
Формулировка задачи:
Хожу на курсы по обучению на С. задали написать программу, для расчета корней в квадратичном уравнении, а в случае нарушения устроить типо "Армагеддон"
Сам армагеддон работает нормально, а вот программа для корней нет. Не выдает результат при b=0, и a=0.
Вот код:
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <conio.h>
void do_armageddon ();
int calculation_of_the_equation (double a, double b, double c);
int calculation_of_the_normal_quadratic_equation (double a, double b, double c);
int calculation_of_the_not_normal_quadratic_equation (double a, double c);
int main()
{
printf ("Hello, guys! You are students, I know it!\n"
"And I also know, that you can save one universe!\n"
"Enter a, b, c for ax^2 + bx + c = 0\n"
"Or I will destroy one world in the multiverse!"
"\nEnter please 3 numbers:\n");
double a = 0, b = 0, c = 0;
int nVals = scanf ("%lg %lg %lg,", &a, &b, &c);
if (nVals != 3)
{
printf ("Learn the math, MEN! You have destroyed another one universe,"
" like other guys!\n");
do_armageddon ();
getch ();
return -666;
}
else
{
calculation_of_the_equation (a, b, c);
}
}
int calculation_of_the_equation (double a, double b, double c)
{
if (a != 0)
if (b != 0)
if (c != 0)
{
calculation_of_the_normal_quadratic_equation (a, b, c);
}
else if (b == 0)
{
calculation_of_the_not_normal_quadratic_equation (a, c);
}
else if (a == 0 && c != 0)
{
double x = -c / b;
printf ("It's linear equation and answer is:\n"
"x = %lg", x);
}
else if (c == 0 && a != 0)
{
double x = -a / b;
printf ("It's linear equation and answer is:\n"
"x = %lg", x);
}
getch ();
return 0;
}
int calculation_of_the_not_normal_quadratic_equation (double a, double c)
{
double d = 0 - 4*a*c;
double x1 = (0 + sqrt(d))/(2*a);
double x2 = (0 - sqrt(d))/(2*a);
if (d > 0)
{
printf ("Mission complete! Results are:\n");
printf ("x1 = %lg and ", x1);
printf ("x2 = %lg\n", x2);
printf ("Thanks, man!");
printf ("\nPress any key to exit.");
}
else if (d == 0)
{
printf ("You entrered the worst set of numbers in the world, "
"but we have 1 result...\n");
printf ("This result shows us that:\n");
printf ("x = %lg", x1);
printf ("\nThanks, man!");
printf ("\nPress any key to exit.");
}
else if (d < 0)
{
printf ("There is nothing... Bad numbers!");
printf ("\nPress any key to exit.");
}
}
int calculation_of_the_normal_quadratic_equation (double a, double b, double c)
{
double d = b*b - 4*a*c;
double x1 = (-b + sqrt(d))/(2*a);
double x2 = (-b - sqrt(d))/(2*a);
if (d > 0)
{
printf ("Mission complete! Results are:\n");
printf ("x1 = %lg and ", x1);
printf ("x2 = %lg\n", x2);
printf ("Thanks, man!");
printf ("\nPress any key to exit.");
}c5BpMyAp
else if (d == 0)
{
printf ("You entrered the worst set of numbers in the world, "
"but we have 1 result...\n");
printf ("This result shows us that:\n");
printf ("x = %lg", x1);
printf ("\nThanks, man!");
printf ("\nPress any key to exit.");
}
else if (d < 0)
{
printf ("There is nothing... Bad numbers!");
printf ("\nPress any key to exit.");
}
}
void do_armageddon ()
{
int i = 0;
while (i <= 100)
{
printf ("Destroying the universe ... %d%%\r", i);
Sleep (100);
i += 1;
}
printf ("\nArmagedon done! Thanks!\n");
printf ("Press any key to leaving the world...\n");
}
Я проверил еще отдельно каждую функцию. Они все по отдельности работают нормально и выдают требуемый результат. Почему они не работают нормально вместе?
Решение задачи: «Не работает программа на нахождение корней в квадратном уравнении. Не выполняются некоторые из условий»
textual
Листинг программы
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <conio.h>
void do_armageddon();
void calculation_of_the_equation(double a, double b, double c);
void calculation_of_the_normal_quadratic_equation(double a, double b, double c);
void calculation_of_the_not_normal_quadratic_equation(double a, double c);
void main()
{
printf("Hello, guys! You are students, I know it!\n"
"And I also know, that you can save one universe!\n"
"Enter a, b, c for ax^2 + bx + c = 0\n"
"Or I will destroy one world in the multiverse!"
"\nEnter please 3 numbers:\n");
double a = 0, b = 0, c = 0;
int nVals = scanf("%lg %lg %lg,", &a, &b, &c);
if (nVals != 3)
{
printf("Learn the math, MEN! You have destroyed another one universe,"
" like other guys!\n");
do_armageddon();
}
else
{
calculation_of_the_equation(a, b, c);
}
getch();
}
void calculation_of_the_equation(double a, double b, double c)
{
if (a != 0 && b != 0 && c != 0)
{
calculation_of_the_normal_quadratic_equation(a, b, c);
}
else if (b == 0)
{
calculation_of_the_not_normal_quadratic_equation(a, c);
}
else if (a == 0 && c != 0)
{
double x = -c / b;
printf("It's linear equation and answer is:\n"
"x = %lg", x);
}
else if (c == 0 && a != 0)
{
double x = -a / b;
printf("It's linear equation and answer is:\n"
"x = %lg", x);
}
getch();
}
void calculation_of_the_not_normal_quadratic_equation(double a, double c)
{
double d = 0 - 4 * a*c;
double x1 = (0 + sqrt(d)) / (2 * a);
double x2 = (0 - sqrt(d)) / (2 * a);
if (d > 0)
{
printf("Mission complete! Results are:\n");
printf("x1 = %lg and ", x1);
printf("x2 = %lg\n", x2);
printf("Thanks, man!");
printf("\nPress any key to exit.");
}
else if (d == 0)
{
printf("You entrered the worst set of numbers in the world, "
"but we have 1 result...\n");
printf("This result shows us that:\n");
printf("x = %lg", x1);
printf("\nThanks, man!");
printf("\nPress any key to exit.");
}
else if (d < 0)
{
printf("There is nothing... Bad numbers!");
printf("\nPress any key to exit.");
}
}
void calculation_of_the_normal_quadratic_equation(double a, double b, double c)
{
double d = b*b - 4 * a*c;
double x1 = (-b + sqrt(d)) / (2 * a);
double x2 = (-b - sqrt(d)) / (2 * a);
if (d > 0)
{
printf("Mission complete! Results are:\n");
printf("x1 = %lg and ", x1);
printf("x2 = %lg\n", x2);
printf("Thanks, man!");
printf("\nPress any key to exit.");
}
else if (d == 0)
{
printf("You entrered the worst set of numbers in the world, "
"but we have 1 result...\n");
printf("This result shows us that:\n");
printf("x = %lg", x1);
printf("\nThanks, man!");
printf("\nPress any key to exit.");
}
else if (d < 0)
{
printf("There is nothing... Bad numbers!");
printf("\nPress any key to exit.");
}
}
void do_armageddon()
{
int i = 0;
while (i <= 100)
{
printf("Destroying the universe ... %d%%\r", i);
Sleep(100);
i += 1;
}
printf("\nArmagedon done! Thanks!\n");
printf("Press any key to leaving the world...\n");
}
Объяснение кода листинга программы
- Программа предназначена для решения квадратных уравнений.
- Пользователю предлагается ввести значения a, b и c.
- Если пользователь вводит некорректные значения, программа выдает сообщение об ошибке и вызывает функцию do_armageddon().
- Если пользователь вводит корректные значения, программа вызывает функцию calculation_of_the_equation(a, b, c).
- Внутри функции calculation_of_the_equation() происходит проверка значений a, b и c.
- Если b равно нулю, программа вызывает функцию calculation_of_the_not_normal_quadratic_equation(a, c).
- Если a равно нулю и c не равно нулю, программа вычисляет значение x как -c/b и выводит его.
- Если c равно нулю и a не равно нулю, программа вычисляет значение x как -a/b и выводит его.
- Если a, b и c не равны нулю, программа вызывает функцию calculation_of_the_normal_quadratic_equation(a, b, c).
- Внутри функции calculation_of_the_normal_quadratic_equation() происходит вычисление дискриминанта по формуле d = bb - 4a*c.
- Затем программа вычисляет значения x1 и x2 по формулам x1 = (-b + sqrt(d)) / (2a) и x2 = (-b - sqrt(d)) / (2a).
- Если d больше нуля, программа выводит значения x1 и x2.
- Если d равно нулю, программа выводит только x1.
- Если d меньше нуля, программа выводит сообщение о том, что корней нет.
- Если пользователь вводит некорректные значения, программа вызывает функцию do_armageddon().
- Функция do_armageddon() выводит сообщение о том, что Армагеддон завершен.
- Функция do_armageddon() использует цикл while для отображения процесса уничтожения вселенной.
- В конце программы пользователю предлагается нажать любую клавишу для выхода из программы.