Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3.  
  4. using namespace std;
  5.  
  6. static const int ELEMS = 10; // Это же не считается?) Сортировщик все равно темплейтовый от size.
  7.  
  8. class Function
  9. {
  10.     virtual double f(double) = 0;
  11. public:
  12.     double operator() (const double point)
  13.     {
  14.         return f(point);
  15.     }
  16. };
  17.  
  18. typedef Function *FunctionPtr;
  19.  
  20. class Metric
  21. {
  22. protected:
  23.     double point;
  24.     Metric(double p) : point(p)
  25.     {
  26.     }
  27. public:
  28.     virtual int compare(FunctionPtr farr[], int i, int j) = 0;
  29. };
  30.  
  31. typedef Metric *MetricPtr;
  32.  
  33. class Function1: public Function
  34. {
  35.     double f(double x) {return sin(x);}
  36. };
  37.  
  38. class Function2: public Function
  39. {
  40.     double f(double x) {return cos(x);}
  41. };
  42.  
  43. class Function3: public Function
  44. {
  45.     double f(double x) {return x * x;}
  46. };
  47.  
  48. class Function4: public Function
  49. {
  50.     double f(double x) {return fabs(x);}
  51. };
  52.  
  53. class Function5: public Function
  54. {
  55.     double f(double x) {return exp(x);}
  56. };
  57.  
  58. class Function6: public Function
  59. {
  60.     static const int MAGIC_CONST = 42;
  61.  
  62.     double f(double x) {return sqrt(MAGIC_CONST + x * x);}
  63. };
  64.  
  65. class Function7: public Function
  66. {
  67.     double f(double x) {return x * x * x;}
  68. };
  69.  
  70. class Function8: public Function
  71. {
  72.     #define PI 3.14159
  73.  
  74.     double f(double x) {return cos(x + PI / 3);}
  75. };
  76.  
  77. class Function9: public Function
  78. {
  79.     #define PI 3.14159
  80.  
  81.     static const int MAGIC_CONST = 42;
  82.  
  83.     double f(double x) {return sin(x - PI / MAGIC_CONST);}
  84. };
  85.  
  86. class Function10: public Function
  87. {
  88.     static const int SHIFT = 2;
  89.  
  90.     double f(double x) {return log(SHIFT + x * x);}
  91. };
  92.  
  93. class Metric1 : public Metric
  94. {
  95. public:
  96.     Metric1(double p) : Metric(p)
  97.     {
  98.     }
  99.     int compare(FunctionPtr farr[], int i, int j)
  100.     {
  101.         if ((*farr[i])(point) > (*farr[j])(point)) {
  102.             return -1;
  103.         }
  104.         return 1;
  105.     }
  106. };
  107.  
  108. class Metric2 : public Metric
  109. {
  110. public:
  111.     Metric2(double p) : Metric(p)
  112.     {
  113.     }
  114.     int compare(FunctionPtr farr[], int i, int j)
  115.     {
  116.         if (fabs((*farr[i])(point)) < fabs((*farr[j])(point))) {
  117.             return -1;
  118.         }
  119.         return 1;
  120.     }
  121. };
  122.  
  123. template <int size> class FuncSorter
  124. {
  125.     FunctionPtr *farr;
  126.     MetricPtr m;
  127.  
  128. public:
  129.     FuncSorter(FunctionPtr *farr, Metric *m) : farr(farr), m(m)
  130.     {
  131.     }
  132.  
  133.     void sort()
  134.     {
  135.         for (int i = size - 1; i > 0; i--) {
  136.             for (int j = 0; j < i; j++) {
  137.                 if (m->compare(farr, j, j + 1) == -1) {
  138.                     FunctionPtr ff = farr[j + 1];
  139.                     farr[j + 1] = farr[j];
  140.                     farr[j] = ff;
  141.                 }
  142.             }
  143.         }
  144.     }
  145. };
  146.  
  147. int main(void)
  148. {
  149.     int k; // 1
  150.     double y, z; // 0, 0.747
  151.  
  152.     FunctionPtr farr[ELEMS];
  153.  
  154.     farr[0] = new Function1;
  155.     farr[1] = new Function2;
  156.     farr[2] = new Function3;
  157.     farr[3] = new Function4;
  158.     farr[4] = new Function5;
  159.     farr[5] = new Function6;
  160.     farr[6] = new Function7;
  161.     farr[7] = new Function8;
  162.     farr[8] = new Function9;
  163.     farr[9] = new Function10;
  164.  
  165.     cin >> k >> y >> z;
  166.  
  167.     MetricPtr m;
  168.  
  169.     if (k == 1) {
  170.         m = new Metric1(y);
  171.     } else if (k == 2) {
  172.         m = new Metric2(y);
  173.     } else {
  174.         cerr << "k must equal 1 or 2" << endl;
  175.         return 1;
  176.     }
  177.  
  178.     FuncSorter<ELEMS>(farr, m).sort();
  179.  
  180.     for (int i = 0; i < ELEMS; i++) {
  181.         z = (k == 1) ? (*farr[ELEMS - 1 - i])(z): (*farr[i])(z);
  182.     }
  183.  
  184.     cout << z << endl;
  185.  
  186.     for (int i = 0; i < ELEMS; i++) {
  187.         delete farr[i];
  188.     }
  189.     delete m;
  190.  
  191.     return 0;
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement