Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Image {
  8.     double quality;
  9.     double freshness;
  10.     double rating;
  11. };
  12.  
  13. struct Params {
  14.     double a;
  15.     double b;
  16.     double c;
  17. };
  18.  
  19.  
  20. class FunctionPart{
  21. public:
  22.     FunctionPart(char new_operation, double new_value){
  23.         operation = new_operation;
  24.         value = new_value;
  25.     }
  26.  
  27.     double Apply(double source_value) const {
  28.        if (operation == '+') {
  29.            return source_value + value;
  30.        } else if (operation == '-'){
  31.            return source_value - value;
  32.        } else if (operation == '*'){
  33.            return source_value * value;
  34.        } else if (operation == '/'){
  35.            return source_value / value;
  36.        }
  37.     }
  38.  
  39.     void Invert(){
  40.         if (operation == '+') {
  41.             operation = '-';
  42.         } else if (operation == '-'){
  43.             operation = '+';
  44.         } else if (operation == '*'){
  45.             operation = '/';
  46.         } else if (operation == '/'){
  47.             operation = '*';
  48.         }
  49.     }
  50.  
  51. private:
  52.     char operation;
  53.     double value;
  54. };
  55.  
  56. class Function{
  57. public:
  58.     void AddPart(char operation, double value){
  59.         parts.push_back({operation, value});
  60.     }
  61.     double Apply(double value) const{
  62.         for (const FunctionPart& x : parts){
  63.             value = x.Apply(value);
  64.         }
  65.         return value;
  66.     }
  67.     void Invert(){
  68.         for(FunctionPart& x : parts){
  69.             x.Invert();
  70.         }
  71.         reverse(parts.begin(), parts.end());
  72.     }
  73.  
  74. private:
  75.     vector<FunctionPart> parts;
  76. };
  77.  
  78. Function MakeWeightFunction(const Params& params,
  79.                             const Image& image) {
  80.     Function function;
  81.     function.AddPart('*', params.a);
  82.     function.AddPart('-', image.freshness * params.b);
  83.     function.AddPart('+', image.rating * params.c);
  84.     return function;
  85. }
  86.  
  87. double ComputeImageWeight(const Params& params, const Image& image) {
  88.     Function function = MakeWeightFunction(params, image);
  89.     return function.Apply(image.quality);
  90. }
  91.  
  92. double ComputeQualityByWeight(const Params& params,
  93.                               const Image& image,
  94.                               double weight) {
  95.     Function function = MakeWeightFunction(params, image);
  96.     function.Invert();
  97.     return function.Apply(weight);
  98. }
  99.  
  100. int main() {
  101.     Image image = {10, 2, 6};
  102.     Params params = {4, 2, 6};
  103.     cout << ComputeImageWeight(params, image) << endl;
  104.     cout << ComputeQualityByWeight(params, image, 52) << endl;
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement