Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- using namespace std;
- static const int ELEMS = 10; // Это же не считается?) Сортировщик все равно темплейтовый от size.
- class Function
- {
- virtual double f(double) = 0;
- public:
- double operator() (const double point)
- {
- return f(point);
- }
- };
- typedef Function *FunctionPtr;
- class Metric
- {
- protected:
- double point;
- Metric(double p) : point(p)
- {
- }
- public:
- virtual int compare(FunctionPtr farr[], int i, int j) = 0;
- };
- typedef Metric *MetricPtr;
- class Function1: public Function
- {
- double f(double x) {return sin(x);}
- };
- class Function2: public Function
- {
- double f(double x) {return cos(x);}
- };
- class Function3: public Function
- {
- double f(double x) {return x * x;}
- };
- class Function4: public Function
- {
- double f(double x) {return fabs(x);}
- };
- class Function5: public Function
- {
- double f(double x) {return exp(x);}
- };
- class Function6: public Function
- {
- static const int MAGIC_CONST = 42;
- double f(double x) {return sqrt(MAGIC_CONST + x * x);}
- };
- class Function7: public Function
- {
- double f(double x) {return x * x * x;}
- };
- class Function8: public Function
- {
- #define PI 3.14159
- double f(double x) {return cos(x + PI / 3);}
- };
- class Function9: public Function
- {
- #define PI 3.14159
- static const int MAGIC_CONST = 42;
- double f(double x) {return sin(x - PI / MAGIC_CONST);}
- };
- class Function10: public Function
- {
- static const int SHIFT = 2;
- double f(double x) {return log(SHIFT + x * x);}
- };
- class Metric1 : public Metric
- {
- public:
- Metric1(double p) : Metric(p)
- {
- }
- int compare(FunctionPtr farr[], int i, int j)
- {
- if ((*farr[i])(point) > (*farr[j])(point)) {
- return -1;
- }
- return 1;
- }
- };
- class Metric2 : public Metric
- {
- public:
- Metric2(double p) : Metric(p)
- {
- }
- int compare(FunctionPtr farr[], int i, int j)
- {
- if (fabs((*farr[i])(point)) < fabs((*farr[j])(point))) {
- return -1;
- }
- return 1;
- }
- };
- template <int size> class FuncSorter
- {
- FunctionPtr *farr;
- MetricPtr m;
- public:
- FuncSorter(FunctionPtr *farr, Metric *m) : farr(farr), m(m)
- {
- }
- void sort()
- {
- for (int i = size - 1; i > 0; i--) {
- for (int j = 0; j < i; j++) {
- if (m->compare(farr, j, j + 1) == -1) {
- FunctionPtr ff = farr[j + 1];
- farr[j + 1] = farr[j];
- farr[j] = ff;
- }
- }
- }
- }
- };
- int main(void)
- {
- int k; // 1
- double y, z; // 0, 0.747
- FunctionPtr farr[ELEMS];
- farr[0] = new Function1;
- farr[1] = new Function2;
- farr[2] = new Function3;
- farr[3] = new Function4;
- farr[4] = new Function5;
- farr[5] = new Function6;
- farr[6] = new Function7;
- farr[7] = new Function8;
- farr[8] = new Function9;
- farr[9] = new Function10;
- cin >> k >> y >> z;
- MetricPtr m;
- if (k == 1) {
- m = new Metric1(y);
- } else if (k == 2) {
- m = new Metric2(y);
- } else {
- cerr << "k must equal 1 or 2" << endl;
- return 1;
- }
- FuncSorter<ELEMS>(farr, m).sort();
- for (int i = 0; i < ELEMS; i++) {
- z = (k == 1) ? (*farr[ELEMS - 1 - i])(z): (*farr[i])(z);
- }
- cout << z << endl;
- for (int i = 0; i < ELEMS; i++) {
- delete farr[i];
- }
- delete m;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement