forked from danylozemlianyi/GL1-squareRoots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
74 lines (65 loc) · 1.68 KB
/
main.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cmath>
#include <iostream>
const short con_4 = 4;
const short con_2 = 2;
int main(int argc, char *argv[]) {
// Input a, b, c
std::cout<<"This program will solve equation like ax^2 + bx + c = 0"<<"\n";
std::cout<<"Please, enter a, b and c form your equation:"<<"\n";
float a;
while (1) {
std::cout<<"Enter a: ";
std::cin>>a;
if (a != 0) {
break;
}
std::cout<<"A cannot be 0. Please try again:"<<"\n";
}
std::cout<<"Enter b: ";
float b;
std::cin>>b;
std::cout<<"Enter c: ";
float c;
std::cin>>c;
// Checking for discriminant lover than 0
float d;
if ((pow(b, con_2) - con_4*a*c) < 0) {
std::cout<<"Discriminant is lower than 0. Exiting..."<<"\n";
std::cin.get();
return 1;
}
else {
try
{
d = sqrt(pow(b, con_2) - con_4*a*c);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cout<<"Ooops. Something failed. Exiting..."<<"\n";
std::cin.get();
return 1;
}
}
// Finding solution
float x1;
float x2;
try
{
x1 =(-b + d)/(con_2*a);
x2 =(-b - d)/(con_2*a);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
std::cout<<"Ooops. Something failed. Exiting..."<<"\n";
std::cin.get();
return 1;
}
// Printing solution
std::cout<<"Everything is Okay. Here is your result:"<<"\n";
std::cout<<"Xq1: "<<x1<<"\n";
std::cout<<"Xq1: "<<x2<<"\n";
std::cin.get();
return 0;
}