From d7e54927ac774b0fdb7d5b87f05738c600b2c0fe Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 1 Feb 2020 11:51:56 +0100 Subject: chore: lab exam A 28/2/19 --- I_anno/Programmazione_1/lab_28_02_19_A.cc | 166 ++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 I_anno/Programmazione_1/lab_28_02_19_A.cc diff --git a/I_anno/Programmazione_1/lab_28_02_19_A.cc b/I_anno/Programmazione_1/lab_28_02_19_A.cc new file mode 100644 index 0000000..ff2eaae --- /dev/null +++ b/I_anno/Programmazione_1/lab_28_02_19_A.cc @@ -0,0 +1,166 @@ +#include +#include +#include +#include +#define DIM 50 + +using namespace std; + +class A { +public: + A(short m) : len{m} { + arr = new short[m]; + srand(time(NULL)); + for(int i = 0; i < m; ++i) { + arr[i] = rand()%10+1; + } + } + + ~A() { + delete[] arr; + } + + virtual double f(short a) = 0; + virtual void print(ostream& os) = 0; + + short getLen() { + return len; + } + +protected: + short get(short i) { + if(i < 0 || i >= getLen()) + throw out_of_range{"out of arr' range"}; + + return arr[i]; + } + +private: + short* arr; + short len; +}; + +template +class B : public A { +public: + B(short m, char c) : A{m} { + if(typeid(T) == typeid(char)) { + x = c; + } else if(typeid(T) == typeid(string)) { + for(int i = 0; i < m; ++i) + x+=c; + + } else { + // tipo non gestito + throw bad_typeid{}; + } + } + + double f(short a) override { + double n = static_cast(getLen() - a); + double sum{}; + for(int i = a; i < getLen(); ++i) + sum+=get(i); + + return sum/n; + } + + void print(ostream& os) override { + os << "B<" << (typeid(T) == typeid(char) ? "char" : "string") << "> arr=["; + for(int i = 0; i < getLen(); ++i) + os << get(i) << ' '; + os << "], x=" << x; + } + + double foo(short s) { + return log(s) + sin(f(s)); + } +private: + T x; +}; + +class C : public A { +public: + C(short n, int k) : A{n}, y{k} {} + + double f(short a) override { + srand(time(NULL)); + short rand_elem = get(rand()%getLen()); + + return static_cast(a+y)/static_cast(rand_elem); + } + + double g(short w) { + // sicuri che non debba convertire a double? qui sarĂ  sempre 0, forse 1 + return static_cast(sin(w+y)); + } + + void print(ostream& os) override { + os << "C arr=["; + for(int i = 0; i < getLen(); ++i) + os << get(i) << ' '; + os << "], y=" << y; + } + + C& operator++() { + ++y; + return *this; + } +private: + int y; +}; + +ostream& operator<<(ostream& os, A& a) { + a.print(os); + + return os; +} + +int main() { + srand(111222333); + + A *vett[DIM]; + + double max_f3{-100000000.0}; + short c_count{}; + double gsum{}; + short i_to_p{-1}; + + for(int i=0; i(*(vett[i])); + gsum+=cref.g(5); + ++c_count; + break; + } + case 1: + vett[i]= new B(n, rand()%('z' - 'a' + 1) + 'a'); + break; + case 2: + vett[i]= new B(n, rand()%('z' - 'a' + 1) + 'a'); + } + + auto f3 = vett[i]->f(3); + if(f3 > max_f3) + max_f3 = f3; + } + + for(int i = 0; i < DIM; ++i) + cout << i+1 << ')' << *(vett[i]) << ", f(3)=" << vett[i]->f(3) << endl; + + cout << "max f(3)=" << max_f3; + cout << " avg g(5)=" << (static_cast(gsum/c_count)) << endl; + if(i_to_p >= 0) { + cout << *(vett[i_to_p]) << endl; + auto cref = dynamic_cast(vett[i_to_p]); + ++(*cref); + cout << *(vett[i_to_p]) << endl; + } + + return 0; +} -- cgit v1.2.3-18-g5258