summaryrefslogtreecommitdiff
path: root/Year_1/Programming_1/lab_14_12_18.cc
blob: 02bbd6995c361eab04d7b19b4c95577949089558 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<iostream>
#include<cmath>
#define DIM 30

using namespace std;

class A {
protected:
	double* ptr;
	short len;

	void getPtr(ostream& os) {
		os << "ptr=[";
		for(int i = 0; i < len; ++i) { 
			os << ptr[i] << ' ';
		}

		os << "]";
	}
public:
	A(short n) : len{n} {
		srand(time(NULL));
		ptr = new double[n];
		for(int i = 0; i < n; ++i) {
			ptr[i] = (double) rand() / RAND_MAX;
		}
	}

	~A() {
		delete ptr;
	}
	virtual double foo(short a) const = 0;
	virtual void print(ostream& os) = 0;

	double operator[](int i) {
		return ptr[i];
	}
};

class B : public A {
public:
	B(short m, double s) : A{m}, alpha{s} {}
	double foo(short b) const {
		return static_cast<double>(log(1+extract(b)));
	}
	void print(ostream& os) {
		os << "B, ";
		getPtr(os);
		os << ", alpha=" << alpha;
	}

private:
	double alpha;
	double extract(short s) const {
		return (ptr[s%len]+alpha)/2;
	}

};

template<typename T>
class C;

template<>
class C<double> : public A {
public:
	C(short n) : A{n} {
		x = static_cast<double>(log(1+n));
	}

	double foo(short r) const {
		return g(r*x);
	}

	double g(double k) const {
		return 3*k;
	}

	void print(ostream& os) {
		os << "C<double> ";
		getPtr(os);
		os << ", x=" << x;
	}
private:
	short x;
};

template<>
class C<int> : public A {
public:
	C(short n) : A{n} {
		x = g(n);
	}

	double foo(short r) const {
		return g(r*x);
	}

	short g(short k) const {
		return 3*k;
	}

	void print(ostream& os) {
		os << "C<short> ";
		getPtr(os);
		os << ", x=" << x;
	}
private:
	short x;
};

ostream& operator<<(ostream& os, A& a) {
	a.print(os);

	return os;
}

int main() {
	srand(328832748);
	A* vett[DIM];
	double maxfoo{-11111111.0};
	double g_sum{};
	for(int i=0; i<DIM; i++) {
		short n=1+rand()%5;
		switch(rand()%3) {
			case 0: vett[i]= new B(n, n/100.0); break;
			case 1: {
				vett[i]= new C<double>(n);
				auto refC = dynamic_cast<C<double>*>(vett[i]);
				g_sum+=refC->g(5);
				break;
			}
			case 2: vett[i]= new C<int>(n);

		}

		double foo5 = vett[i]->foo(5);
		if(foo5 > maxfoo)
			maxfoo = foo5;
	}

	for(int i = 0; i < DIM; ++i) {
		cout << i+1 << ")" << *(vett[i]) << ", foo(5)=" << vett[i]->foo(5) << endl;
	}

	cout << "max(foo(5))=" << maxfoo << " sum(g(5))=" << g_sum << endl;
	cout << (*vett[2])[0] << endl;

	return 0;
}