-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdixotomia.cpp
55 lines (51 loc) · 2.13 KB
/
dixotomia.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
#include <math.h>
#include <map>
#include <tuple>
#include <string>
typedef std::tuple<std::tuple<double, double>, std::map<int, std::tuple<double, double>>> FuncOutput;
double calculate(std::string &str, double chislo);
double expression(std::string &, unsigned &, double chislo);
double multiply_divide(std::string &, unsigned &, double chislo);
double sign(std::string &, unsigned &, double chislo);
double brackets(std::string &, unsigned &, double chislo);
double number(std::string &, unsigned &, double chislo);
double identificator(std::string &, unsigned &, double chislo);
double function(std::string &, std::string &, unsigned &, double chislo);
FuncOutput dixot(double left_border,double right_border,double eps,int l, std::string function) {
double center, check1, check2;
typedef std::map<int, std::tuple<double, double>> ForGraph;
ForGraph dict;
int counter = 0;
std::tuple<double, double> pre_interval;
if (l == 1){
while (fabs(right_border - left_border) > eps) {
center = (left_border + right_border) / 2;
check1= calculate(function, center + eps);
check2= calculate(function, center - eps);
if (check1 < check2) {
right_border = center;
} else {
left_border = center;
}
counter +=1;
pre_interval = std::make_tuple(left_border, right_border);
dict.insert(std::make_pair(counter, pre_interval));
}
}else{
while (fabs(right_border - left_border) > eps) {
center = (left_border + right_border) / 2;
check1= calculate(function, center + eps);
check2= calculate(function, center - eps);
if (check1 > check2) {
right_border = center;
} else {
left_border = center;
}
counter +=1;
pre_interval = std::make_tuple(left_border, right_border);
dict.insert(std::make_pair(counter, pre_interval));
}
}
std::tuple<double, double> interval = std::make_tuple(left_border, right_border);
return std::make_tuple(interval, dict);
}