include iostream include math include conio using namespace std float

 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
#include <iostream>
#include <math.h>
#include <conio.h>
using namespace std;
float a, b, e;
float f(float x){
return pow(x, 2) + x - 2;
}
void dihot(){
float c;
c = (::a + ::b) / 2;
cout << "\nc = " << c;
while((fabs(::b - ::a) > ::e) && (f(c) != 0)){
if (f(a) * f(c) < 0)
::b = c;
else
::a = c;
c = (::a + ::b) / 2;
cout << "c = " << c <<endl;
}
cout << "\nx = " << c << endl;
}
void horda()
{
while(fabs(::b - ::a) > ::e)
{
::a = ::b - (::b - ::a) * f(::b)/(f(::b) - f(::a));
::b = ::a - (::a - ::b) * f(::a)/(f(::a) - f(::b));
}
cout << "x = " << ::b << endl;
}
int main(int argc, char* argv[])
{
clrscr();
while(true) {
cout << "Start a = ";
cin >> ::a;
cout << "End b = ";
cin >> ::b;
if(f(::a) * f(::b) > 0)
cout << "ERROR! Try again!" << endl;
else
break;
}
cout<<"e = ";
cin >> ::e;
cout << "--- METOD DIHOTOMII ---" << endl;
dihot();
cout << "-----------------------" << endl;
cout << "------ METOD HORD ------" << endl;
horda();
cout << "------------------------" << endl;
system("PAUSE");
return 0;
}