summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_1/lab_26_07_19.cc
blob: 91822d93e47bf7c8fd07f5d41e9c83e9b70d0184 (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
#include<iostream>
#include<cmath>
#include<typeinfo>

#define DIM 30

using namespace std;

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

    ~A() {
        delete vec;
    }

    virtual double foo(short a) = 0;
    short getLen() {
        return len;
    }

    virtual void print(ostream& o) = 0;

    int operator()(int i1, int i2) {
        if(i1 < 0) i1 = 0;
        if(i2 > len) i2 = len;

        int sum = 0;
        
        for(int i = i1; i < i2; ++i) {
            sum+=vec[i];
        }

        return sum;
    }
    
protected:
    int get(short i) {
        return vec[i];
    }
private:
    int* vec;
    short len;
};

class B : public A {
public:
    B(short m, int x, int y) : A(m, x, y) {
        srand(time(NULL));
        short rand_num = rand()%m;
        p = get(rand_num);
    }
    
    double foo(short a) {
        return static_cast<double>(prod(a)) / static_cast<double>(p);
    }

    void print(ostream& os) {
        os << "vec=[";
        for(int i = 0; i < getLen(); ++i) {
            os << get(i) << ' ';
        }
        os << "], p=" << p;
    }
protected:
    int prod(short s) {
        int index = s%getLen();
        int _prod = 1;
        for(int i = index; i < getLen(); ++i) {
            _prod *= get(i);
        }

        return _prod;
    }
private:
    int p;
};


template<class T> class C : public A {
public:
    C(short n, int a, int b) : A(n, a, b) {
        if(typeid(T) == typeid(short)) {
            x = n;
        } else {
            x = log(1+pow(sin(n), 2));
        }

    }
    
    double foo(short r) {
        return g(r);
    }

    T g(T k) {
        return 2*x*(k+1);
    }

    void print(ostream& os) {
        os << "vec=[";
        for(int i = 0; i < getLen(); ++i) {
            os << get(i) << ' ';
        }
        os << "], x=" << x;
    }
private:
    T x;
};

ostream& operator<<(ostream& os, A& a) {
    os << typeid(a).name() << ", ";
    a.print(os);
    return os;
}

int main() {
    srand (time(0)); 
    A* vett[DIM];
    double sum_cd = 0;
    short num_cd = 0;
    for(int i=0; i < DIM; ++i) {
        short n = 1+rand()%10;
        switch ( rand ()%3) {
        case 0:
            vett[i]= new B(n, rand()%5 + 1, rand()%10 + 5);
            break ;
        case 1: {
            vett[i]= new C<double>(n, rand()%5 + 1, rand()%10 + 5); 
            ++num_cd;
            C<double>& cc = dynamic_cast<C<double>& >(*(vett[i]));
            sum_cd += cc.g(5);
            
            break ;
        }
        case 2:
            vett[i]= new C<short>(n, rand()%5 + 1, rand()%10 + 5);
        }
    }

    double max_foo = -100000;

    for(int i = 0; i < DIM; ++i) {
        auto foo3 = vett[i]->foo(3);

        if(foo3 > max_foo) {
            max_foo = foo3;
        }
        cout << (*(vett[i])) << ", foo(3)=" << foo3 <<endl;
    }
    
    // max_foo => Valore maggiore calcolato per foo(3)
    // sum_cd / num_cd => media valori g(5) per C<Double> 
    
    cout << (*vett[0])(0, 2) << endl;

    return 0;
}