summaryrefslogtreecommitdiff
path: root/I_anno/Programmazione_1/lab_28_02_19_C.cc
blob: ac53815c92728c9910c320baade984e6746f93f4 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include<iostream>
#include<cmath>
#include<set>
#define DIM 10

using namespace std;

class A {
public:
	A(short m) : len{m} {
		srand(time(NULL));
		base = new char[len];
		for(int i = 0; i < len; ++i) {
			base[i] = rand()%('z'-'a'+1)+'a';
		}
	}

	~A() {
		delete base;
	}

	short getLen() {
		return len;
	}

	virtual string extract(short x) = 0;
	virtual void print(ostream& os) = 0;

protected:
	char get(short i) {
		return base[i];
	}

private:
	char* base;
	short len;
};

class B : public A {
public:
	B(short m, double p) : A{m} {
		for(int i = 0; i < m; ++i) {
			switch(get(i)) {
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				str+=get(i);
			}
		}
	}

	double foo(short s) {
		return sin(k+s)/log(s+1);
	}

	string extract(short x) override {
		string x2{""};
		short inserted{};
		set<char> chars_a;
		for(int i = 0; i < getLen(); ++i)
			chars_a.insert(get(i));

		while(inserted < x) {
			char r_char = rand()%('z'-'a'+1)+'a';
			if(chars_a.find(r_char) != chars_a.end()) {
				x2+=r_char;
				++inserted;
			}			
		}

		return x2;
	}

	void print(ostream& os) override {
		os << "B: base=[";
		for(int i = 0; i < getLen(); ++i) {
			os << get(i) << ' ';
		}
		os << "], str=" << str << ", k=" << k;
	}
	
private:
	string str;
	double k;
};

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

	double g(short w) {
		return sin(w+y);
	}

	string extract(short x) override {
		string x2{""};
		char* narr = new char[x];
		short counter_narr{};

		for(int i = 0; i < getLen(); ++i) {
			switch(get(i)) {
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				break;
			default:
				narr[counter_narr++] = get(i);
			}
		}
		for(int i = 0; i < x; ++i)
			x2+=narr[i];

		delete[] narr;

		return x2;
	}	

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

	C operator++(int) {
		C temp{*this};
		
		y++;

		return temp;
	}
private:
	int y;
};

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

	return os;
}

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

	double sum{};
	short i_c{-1};
	for(int i=0; i<DIM; i++) {
		short n=10+rand()%10;
		switch(rand()%2){
	  	case 0: {
	    	vett[i]= new C(n, rand()%20 + 1);
			auto cref = dynamic_cast<C*>(vett[i]);
			sum+=cref->g(5);
			i_c = i;
	    	break;
	  	}
		case 1: {
	   	 	vett[i]= new B(n, rand()/(double) RAND_MAX);
			auto bref = dynamic_cast<B*>(vett[i]);
			sum+=bref->foo(5);
	    	break;
		}
		}
    }

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

	cout << sum/DIM << endl;

	if(i_c >= 0) {
		cout << *vett[i_c] << endl;
		auto cref = dynamic_cast<C*>(vett[i_c]);
		(*cref)++;
		cout << *vett[i_c] << endl;
		

	}
	return 0;
}