Advertisement
RobertDeMilo

WB4.2-3 Классы Function и FunctionPart

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