From a22aab814f0cd9d0a59d2e9534036be5aef71fcd Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 16 Jan 2020 12:41:21 +0100 Subject: Lab exam 26/7/19 --- I_anno/Programmazione_1/lab_26_07_19.cc | 164 ++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 I_anno/Programmazione_1/lab_26_07_19.cc (limited to 'I_anno') diff --git a/I_anno/Programmazione_1/lab_26_07_19.cc b/I_anno/Programmazione_1/lab_26_07_19.cc new file mode 100644 index 0000000..91822d9 --- /dev/null +++ b/I_anno/Programmazione_1/lab_26_07_19.cc @@ -0,0 +1,164 @@ +#include +#include +#include + +#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(prod(a)) / static_cast(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 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(n, rand()%5 + 1, rand()%10 + 5); + ++num_cd; + C& cc = dynamic_cast& >(*(vett[i])); + sum_cd += cc.g(5); + + break ; + } + case 2: + vett[i]= new C(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 < Valore maggiore calcolato per foo(3) + // sum_cd / num_cd => media valori g(5) per C + + cout << (*vett[0])(0, 2) << endl; + + return 0; +} -- cgit v1.2.3-18-g5258