partly explained
unknown
c_cpp
2 years ago
4.0 kB
12
Indexable
#define _CRT_SECURE_NO_WARNINGS // нужно затем чтобы MVS не выебывался на scanf
//можешь тексты написать по другому, так безопаснее будет
#include <math.h>
#include <stdio.h>
int main(void) {
int a, b, c, delta, absdelta;
float sdelta, x1, x2, re1, re2, im1, im2; // creating a variables это понятно
printf("This program will solve the quadratic equation of the form: "); // intro это необязательно
printf("ax^2+bx+c=0\n");
printf("\nEnter the value of a here: "); // asking for the value of a запрашиваем ввод переменных
scanf("%d", &a);
printf("\nEnter the value of b here: "); // asking for the value of b
scanf("%d", &b);
printf("\nEnter the value of c here: "); // asking for the value of c
scanf("%d", &c);
if (a == 0) { //здесь мы решаем линейное уравнение если а = 0 (частный случай)
if (b == 0) { // linear equation
if (c == 0) {
printf("\nx can be any number");
}
else {
printf("\nthere is no solution");
}
}
else {
printf("\nx = %f\n", -(float)c / b);
}
}
else { //здесь мы начинаем решать другие частные случаи квадратного уравнения
if (b == 0) {
if (a * c > 0) {
printf("\nThere are no real solutions\n");
}
else {
float sq;
sq = sqrt(-(float)c / a);
printf("\n x1 = %f\n", sq);
printf("\n x2 = %f", -1*sq);
}
}
else {
if (c == 0) {
printf("\nx1 = 0\n");
printf("x2 = %f", -(float)b / a);
} //здесь мы заканчиваем решать частные случаи и начинается полноценное решение квадратного уравнения
else {
delta = b * b - 4 * a * c; // computing the discriminant
sdelta = sqrt((float)delta); // computing square root of discriminant
if (delta > 0) { // checking the value of discriminant (>0)
x1 = ((-(float)b + sdelta) / (2 * (float)a)); // computing the first root
x2 = ((-(float)b - sdelta) / (2 * (float)a)); // computing the second root
printf("The roots are: %f and %f\n", x1, x2); // printing the roots
}
if (delta == 0) { // checking the value of discriminant (=0)
x1 = ((-(float)b + sqrt((float)delta)) / (2 * (float)a)); // computing the root
printf("The root is: %f\n", x1); // printing the root
}
if (delta < 0) { // checking the value of discriminant (<0)
printf("\nThere are no real solutions\n");
//absdelta = delta * (-1); // computing the absolute value of diccriminant ЭТО УДАЛИ, ТУТ Я КОМПЛЕКСНЫЕ КОРНИ НАХОДИЛ
//re1 = (-(float)b / (2 * (float)a)); // computing the real parts of the roots
//re2 = (-(float)b / (2 * (float)a));
//im1 = (sdelta / (2 * (float)a)); // computing the imaginary parts of the roots
//im2 = (-sdelta / (2 * (float)a));
//printf("\nThe roots are: %f + %fi and %f + %fi", re1, re2, im1, im2); // printing the roots
}
}
}
}
}Editor is loading...
Leave a Comment