summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_1/lab_28_02_19_B.cc
blob: 4639f665255d96d706a2397388363ae135578b70 (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
#include<iostream>
#include<cstdlib>
#include<cmath>
#define DIM 50
using namespace std;

class A {
public:
	A(short m, char c) : len{m} {
		ptr = new char[len];
		srand(time(NULL));
		for(int i = 0; i < m; ++i) {
			ptr[i] = static_cast<char>(rand()%(c-'a'+1) + 'a');
		}
	}
	A(const A& a) = default;
	~A() {
		delete ptr;
	}

	virtual string elab(short a, char c) = 0;
	virtual void print(ostream& o) = 0;
	short getLen() {
		return len;
	}

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

protected:
	char get(short i) {
		if(i > len)
			return ptr[0];

		return ptr[i];
	}

private:
	char* ptr;
	short len;
};

class B : public A {
public:
	B(short m, double y, char c) : A{m, c}, x{y} {}
	double foo(short s) {
		return sin(x+s)/log(x+s);
	}

	string elab(short a, char c) {
		string st{};
		for(int i = 0; i < getLen(); ++i) {
			auto ch = get(i);
			if(abs(ch-c) <= a) {
				st+=ch;
			}
		}

		return st;
	}

	void print(ostream& os) {
		os << "B ptr=[";
		for(int i = 0; i < getLen(); ++i) 
			os << get(i) << ' ';

		os << "], x=" << x << ", elab(5, z)=" << elab(5, 'z');
	}

private:
	double x;
};

template<typename T>
class C : public A {
public:
	C(short n, double k, char c) : A{n, c} {
		if(typeid(T) == typeid(short))
			y = static_cast<int>(100*k);
		else
			y = k;
	}

	string elab(short a, char c) {
		string st{};
		if(getLen() >= a) {
			if(a >= 1) {
				for(int i = 0; i < getLen(); st+=c, ++i);
			}
		} else {
			for(int i = 0; i < getLen(); st += get(i++));
		}

		return st;
	}

	double g(short w) {
		return sin(w+y);
	}
	
	void print(ostream& os) {
		os << "C<" << typeid(T).name() << "> ptr=[";
		for(int i = 0; i < getLen(); ++i) 
			os << get(i) << ' ';

		os << "], y=" << y << ", elab(5, z)=" << elab(5, 'z');
	}
private:
	T y;
};

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

	return os;
}

int main() {
	srand(time(NULL));

	A *vett[DIM];
	short cb{}, cc{};
	double sumb{}, sumc{};

	for(int i=0; i<DIM; i++) {
  		short n=1+rand()%10;
    	switch(rand()%3) {
      	case 0: 
		{
        	vett[i]= new B(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a');
			auto temp = dynamic_cast<B&>(*(vett[i]));
			sumb+=temp.foo(5);
			++cb;
			break;
      	}
		case 1:
			vett[i]= new C<double>(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a');
			break;
      	case 2:
		{
			vett[i]= new C<short>(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a');
			auto temp = dynamic_cast<C<short>&>(*(vett[i]));
			sumc+=temp.g(5);
			++cc;
		}
      	}
	}

	for(int i=0; i<DIM; i++) {
		cout << *vett[i] << endl;
	}

	cout << "avg(foo(5))=" << sumb/cb << " "; 
	cout << "avg(g(5))=" << sumc/cc << endl; 

	cout << endl;
	cout << *vett[0] << endl;
	(*vett[0])[0] = '$';
	cout << *vett[0] << endl;
	return 0;
}