summaryrefslogtreecommitdiff
path: root/Year_1/Programming_1/lab_28_02_19_A.cc
blob: ff2eaae1a44dcc5e9281b57c3558004c3fcb1a4f (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include<iostream>
#include<stdexcept>
#include<cstdlib>
#include<cmath>
#define DIM 50

using namespace std;

class A {
public:
	A(short m) : len{m} {
		arr = new short[m];
		srand(time(NULL));
		for(int i = 0; i < m; ++i) {
			arr[i] = rand()%10+1;
		}
	}

	~A() {
		delete[] arr;
	}

	virtual double f(short a) = 0;	
	virtual void print(ostream& os) = 0;

	short getLen() {
		return len;
	}

protected:
	short get(short i) {
		if(i < 0 || i >= getLen())
			throw out_of_range{"out of arr' range"};

		return arr[i];
	}

private:
	short* arr;
	short len;
};

template<typename T>
class B : public A {
public:
	B(short m, char c) : A{m} {
		if(typeid(T) == typeid(char)) {
			x = c;
		} else if(typeid(T) == typeid(string)) {
			for(int i = 0; i < m; ++i) 
				x+=c;

		} else {
			// tipo non gestito
			throw bad_typeid{}; 
		}
	}
	
	double f(short a) override {
		double n = static_cast<double>(getLen() - a);
		double sum{};
		for(int i = a; i < getLen(); ++i) 
			sum+=get(i);
		
		return sum/n;
	}

	void print(ostream& os) override {
		os << "B<" << (typeid(T) == typeid(char) ? "char" : "string") << "> arr=[";
		for(int i = 0; i < getLen(); ++i)
			os << get(i) << ' ';
		os << "], x=" << x;
	}

	double foo(short s) {
		return log(s) + sin(f(s));
	}
private:
	T x;
};

class C : public A {
public:
	C(short n, int k) : A{n}, y{k} {}

	double f(short a) override {
		srand(time(NULL));
		short rand_elem = get(rand()%getLen());
		
		return static_cast<double>(a+y)/static_cast<double>(rand_elem);
	}

	double g(short w) {
		// sicuri che non debba convertire a double? qui sarà sempre 0, forse 1
		return static_cast<double>(sin(w+y));
	}

	void print(ostream& os) override {
		os << "C arr=[";
		for(int i = 0; i < getLen(); ++i)
			os << get(i) << ' ';
		os << "], y=" << y;
	}

	C& operator++() {
		++y;
		return *this;
	}
private:
	int y;
};

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

	return os;
} 

int main() {
	srand(111222333);

	A *vett[DIM];

	double max_f3{-100000000.0};
	short c_count{};
	double gsum{};
	short i_to_p{-1};
	
	for(int i=0; i<DIM; i++) {
		short n=1+rand()%10;
		switch(rand()%3) {
		case 0: 
		{
			vett[i]= new C(n, rand()%10 + 1);
			i_to_p = i;
			auto cref = dynamic_cast<C&>(*(vett[i]));
			gsum+=cref.g(5);
			++c_count;
	    	break;
	  	}
		case 1:
	    	vett[i]= new B<string>(n, rand()%('z' - 'a' + 1) + 'a');
	    	break;
	  	case 2: 
	    	vett[i]= new B<char>(n, rand()%('z' - 'a' + 1) + 'a');
		}

		auto f3 = vett[i]->f(3);
		if(f3 > max_f3)
			max_f3 = f3;
    }

	for(int i = 0; i < DIM; ++i)
		cout << i+1 << ')' << *(vett[i]) << ", f(3)=" << vett[i]->f(3) << endl;

	cout << "max f(3)=" << max_f3;
	cout << " avg g(5)=" << (static_cast<double>(gsum/c_count)) << endl;
	if(i_to_p >= 0) {
		cout << *(vett[i_to_p]) << endl;
		auto cref = dynamic_cast<C*>(vett[i_to_p]);
		++(*cref);
		cout << *(vett[i_to_p]) << endl;
	}
	
	return 0;
}