-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-test-sqequ.cpp
59 lines (49 loc) · 1.83 KB
/
2-test-sqequ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// console_test.cpp
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <conio.h>
using namespace std;
int main()
{
// Input
printf("----------------------------------------------\n");
printf("---- *** Find Root(s) of the Equation *** ----\n");
printf("----------------------------------------------\n");
printf("------------- a*x^2 + b*x + c = 0 ------------\n");
printf("\n");
double a; printf("Input a (coef by x^2): "); cin >> a; // coef by X^2
double b; printf("Input b (coef by x ): "); cin >> b; // coef by X
double c; printf("Input c (free coef ): "); cin >> c; // free coef
double d; double x1; double x2;
if (a+b+c == 0) { // 0 = 0 for all values of x
printf("----------------------------------------------\n");
printf("It seems there's no equation! \n");
getch(); return 0;}
d = b*b - 4*a*c;
if (d < 0) { // D < 0, no Real roots
printf("----------------------------------------------\n");
printf("Discriminant must be greater than 0! \n");
getch(); return 0;}
if (d == 0) { // D = 0, one root
x1 = (b*(-1.0))/(a*2.0);
printf("----------------------------------------------\n");
printf("Equation : (%.1f",a); printf(")x^2 + (%.1f",b); printf(")x + (%.1f",c); printf(") = 0 \n");
printf("Discriminant : D = %.4f \n", d);
printf("Root : x = %.4f \n", x1);
}
else { // D > 0, two roots
x1 = ((b*(-1.0)) + sqrt(d)) /(a*2.0);
x2 = ((b*(-1.0)) - sqrt(d)) /(a*2.0);
printf("----------------------------------------------\n");
printf("Equation : (%.1f",a); printf(")x^2 + (%.1f",b); printf(")x + (%.1f",c); printf(") = 0 \n");
printf("Discriminant : D = %.4f \n", d);
printf("Roots : x1 = %.4f", x1);
printf(", x2 = %.4f \n", x2);
}
getch();
return 0;
}