-
Notifications
You must be signed in to change notification settings - Fork 0
/
finding roots in quadratic equation.cpp
119 lines (102 loc) · 4.47 KB
/
finding roots in quadratic equation.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// finding roots in quadratic equation.cpp: Konsol uygulamasının giriş noktasını tanımlar.
//
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int a, b, c;
int delta;
string decision;
double firstRoot, secondRoot;
void firstRoot_func();
void secondRoot_func();
int main()
{
cout << "ax^2 + bx + c" << endl << endl;
findRoot:
while (1)
{
cout << "a: "; cin >> a; cout << endl;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, try again!" << endl << endl;
}
else
{
break;
}
}
while (1)
{
cout << "b: "; cin >> b; cout << endl;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, try again!" << endl << endl;
}
else
{
break;
}
}
while (1)
{
cout << "c: "; cin >> c; cout << endl;
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, try again!" << endl << endl;
}
else
{
break;
}
}
delta = (b*b) - (4 *a*c);
cout << endl<<"Delta : " << delta << endl << endl;;
if (delta > 0)
{
cout << "There are two roots because Delta > 0"<<endl;
firstRoot_func();
secondRoot_func();
}
else if (delta == 0)
{
cout << "There is only one root because Delta = 0"<<endl;
firstRoot_func();
}
else
{
cout << "There is no root because Delta < 0" << endl << endl;
}
while (1)
{
cout << "Do you want to continue? (Y/N)" << endl << endl;;
cin >> decision;
if (decision == "Y" || decision == "y")
{
goto findRoot;
}
else if (decision == "N" || decision == "n")
{
return 0;
}
}
}
void firstRoot_func()
{
double k = pow(delta, 0.5);
firstRoot = (-b + k) / (2 * a);
cout << "First Root: " << firstRoot << " or " << "(-" << b << "+" << (unsigned char)251 << delta << ")/(" << 2 * a << ")" << endl;
}
void secondRoot_func()
{
double k = pow(delta, 0.5);
secondRoot = (-b - k) / (2 * a);
cout << "Second Root: " << secondRoot << " or " << "(-" << b << "-" << (unsigned char)251 << delta << ")/(" << 2 * a << ")" << endl;
}