This repository was archived by the owner on Aug 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_InvertibleFunctionWithMult.cpp
More file actions
103 lines (91 loc) · 2.04 KB
/
03_InvertibleFunctionWithMult.cpp
File metadata and controls
103 lines (91 loc) · 2.04 KB
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
#include <iostream>
#include <vector>
using namespace std;
struct Image {
double quality;
double freshness;
double rating;
};
struct Params {
double a;
double b;
double c;
};
class FunctionPart {
public:
FunctionPart(char newOperation, double newValue) {
operation = newOperation;
value = newValue;
}
double Apply(double sourceValue) const {
if (operation == '+') {
return sourceValue + value;
} else if (operation == '-') {
return sourceValue - value;
} else if (operation == '*') {
return sourceValue * value;
} else {
return sourceValue / value;
}
}
void Invert() {
if (operation == '+') {
operation = '-';
} else if (operation == '-') {
operation = '+';
} else if (operation == '*') {
operation = '/';
} else {
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 (FunctionPart const &part : parts) {
value = part.Apply(value);
}
return value;
}
void Invert() {
for (FunctionPart &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('*', params.a);
function.AddPart('-', image.freshness * 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() {
Image image = {10, 2, 6};
Params params = {4, 2, 6};
cout << ComputeImageWeight(params, image) << endl;
cout << ComputeQualityByWeight(params, image, 52) << endl;
return 0;
}