summaryrefslogtreecommitdiff
path: root/Year_1/Programming_1/lab_15_02_19_D.cc
blob: b050e72cfac21d18b9eef65e20c316483a84a8b9 (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
#include<iostream>
#include<stdexcept>
#define DIM 50

using namespace std;

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

	~A() {
		delete ptr;
	}

	virtual double const f() = 0;
	void print(ostream& os) {
		os << typeid(*this).name() << ", ptr=[ ";
		for(int i = 0; i < len; ++i)
			os << ptr[i] << ' ';

		os << "]";
	}

	int get(short i) {
		if(i >= len)
			throw out_of_range{"out of range"};

	 	return ptr[i];
	}

	short getLen() {
		return len;
	}

	int& operator[](int x){ 
		return ptr[x];
	}

	const int& operator[](int x) const { 
		return ptr[x];
	}
	
private:
	int* ptr;
	short len;

};

class B : public A {
public:
	B(short m, short k, double y) : A{m, k}, p{y} {}
	double const f() {
		int sum{};
		for(int i = 0; i < getLen(); ++i) {
			int t = get(i);

			if((t%2) == 0) {
				sum+=t;
			}
		}

		if(sum == 0)
			return 0;

		return static_cast<double>(sum/p);
	}

	void print(ostream& os) {
		A::print(os);
		os << ", p=" << p;
	}
private:
	double p;
};

class C : public A {
public:
	C(short n, short k, char c) : A{n, k}, x{c} {}

	double const f() {
		int sum{};
		short c{};
		for(int i = 0; i < getLen(); ++i) {
			int t = get(i);

			if((t%2) == 1) {
				sum+=t;
				++c;
			}
		}

		if(c == 0)
			return 0;

		return static_cast<double>(sum/c);
	}

	string const g(char c) {
		string t{""};
		t+=x;
		t+=c;

		return t;
	}

	void print(ostream& os) {
		A::print(os);
		os << ", x=" << x;
	}
private:
	char x;
};

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

	return os;
}

int main() {
	srand(111222333);
    A *vett[DIM];

	string conc{""};
    for(int i=0; i<DIM; i++){
		short n=1+rand()%10;
		short m = 1+rand()%15;
		if(rand()%2==0)
	 	 	vett[i]= new B(n, m, rand()/(double) RAND_MAX+0.05);
		else {
	  		vett[i]= new C(n, m, (char) (rand()%('z' - 'a' + 1) + 'a'));
			auto cpnt = dynamic_cast<C*>(vett[i]);
			conc+=cpnt->g('h');
		}
	}

	double sum{};
    for(int i = 0; i < DIM; i++) {
		auto fval = vett[i]->f();
		cout << i << ") " << *(vett[i]) << ", f()=" << fval << endl; 
		sum+=fval;
	}
	
	cout << "PUNTO 2:\n\tavg(f())=" << sum/DIM << ", g('h')=" << conc << endl;
	cout << "PUNTO 3:\n";
	cout << (*(vett[0])).get(0) << endl;
	(*(vett[0]))[0] = 24;
	cout << (*(vett[0])).get(0) << endl;

	
	return 0;
}