-
Notifications
You must be signed in to change notification settings - Fork 0
/
series.h
100 lines (80 loc) · 2.44 KB
/
series.h
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
#ifndef SERIES_H
#define SERIES_H
#include <function.h>
#include <ellipse.h>
#include <hyperbola.h>
//file
#include <fstream>
#include <string>
class Series{
private:
Function **value;
unsigned amount;
static unsigned length;
public:
Series(unsigned number = 3){
value = new Function*[number];
amount = number;
}
void set(Function *X, unsigned place = ++length){
value[place] = X;
}
void sortByValueIn(float X = 0, unsigned left = 0, unsigned right = length){
unsigned i = left, j = right;
Function *pivot = value[(left + right) / 2];
/* partition */
while (i <= j) {
while (value[i]->getY(X) < pivot->getY(X))
++i;
while (value[j]->getY(X) > pivot->getY(X))
--j;
if (i <= j) {
Function *tmp = value[i];
value[i] = value[j];
value[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
sortByValueIn(X, left, j);
if (i < right)
sortByValueIn(X, i, right);
}
std::string out(unsigned start = 0, unsigned end = length){
std::string result;
for (unsigned i = start; i < end; ++i){
result.append(value[i]->out());
result.append("\n");
}
return result;
}
void out(std::string file_name, unsigned start = 0, unsigned end = length){
std::ofstream output;
output.open(file_name, std::ofstream::out | std::ofstream::app);
output<<out(start, end);
output.close();
}
void in(std::string file_name, unsigned start = 0, unsigned end = length){
std::ifstream input;
input.open(file_name);
for (unsigned i = start; i < end; ++i){
char new_function;
input>>new_function;
float a, b;
input>>a>>b;
Function *tmp;
if (new_function == 'e'){
tmp = new Ellipse(a, b);
}else if (new_function == 'h'){
tmp = new Hyperbola(a, b);
}else{
tmp = new Function();
}
set(tmp, i);
}
input.close();
}
};
#endif // SERIES_H