-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cpp
112 lines (103 loc) · 2.13 KB
/
Function.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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Image
{
double quality;
double freshness;
double rating;
};
struct Params
{
double a;
double b;
double c;
};
class FunctionPart
{
public:
FunctionPart(char new_operation, double new_value)
{
operation = new_operation;
value = new_value;
}
double Apply(double source_value) const
{
if (operation == '+')
{
return source_value + value;
}
else if (operation == '-')
{
return source_value - value;
}
}
void Invert()
{
if (operation == '+')
{
operation = '-';
}
else if (operation == '-')
{
operation = '+';
}
}
private:
char operation;
double value;
};
class Function
{
public:
void AddPart(char operation, double value)
{
parts.push_back({operation, value});
}
double Apply(double value) const
{
for (const auto& part : parts)
{
value = part.Apply(value);
}
return value;
}
void Invert()
{
for (auto& part : parts)
{
part.Invert();
}
reverse(begin(parts), end(parts));
}
private:
vector<FunctionPart> parts;
};
Function MakeWeightFunction(const Params& params, const Image& image)
{
Function function;
function.AddPart('-', image.freshness * params.a + params.b);
function.AddPart('+', image.rating * params.c);
return function;
}
double ComputeImageWeight(const Params& params, const Image& image)
{
Function function = MakeWeightFunction(params, image);
return function.Apply(image.quality);
}
double ComputeQualityByWeight(const Params& params, const Image& image,
double weight)
{
Function function = MakeWeightFunction(params, image);
function.Invert();
return function.Apply(weight);
}
int main(void)
{
Image image = {10, 2, 6};
Params params = {4, 2, 6};
cout << ComputeImageWeight(params, image) << endl;
cout << ComputeQualityByWeight(params, image, 46) << endl;
return 0;
}