From 98901163e266b0de456aa13572efe69a72aa69b0 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 1 Feb 2020 02:40:27 +0100 Subject: chore: lab exam 28/2/19 --- I_anno/Programmazione_1/lab_28_02_19.cc | 162 ++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 I_anno/Programmazione_1/lab_28_02_19.cc (limited to 'I_anno/Programmazione_1') diff --git a/I_anno/Programmazione_1/lab_28_02_19.cc b/I_anno/Programmazione_1/lab_28_02_19.cc new file mode 100644 index 0000000..4639f66 --- /dev/null +++ b/I_anno/Programmazione_1/lab_28_02_19.cc @@ -0,0 +1,162 @@ +#include +#include +#include +#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(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 +class C : public A { +public: + C(short n, double k, char c) : A{n, c} { + if(typeid(T) == typeid(short)) + y = static_cast(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(*(vett[i])); + sumb+=temp.foo(5); + ++cb; + break; + } + case 1: + vett[i]= new C(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a'); + break; + case 2: + { + vett[i]= new C(n, (double) rand()/RAND_MAX, rand()%('z' - 'a' + 1) + 'a'); + auto temp = dynamic_cast&>(*(vett[i])); + sumc+=temp.g(5); + ++cc; + } + } + } + + for(int i=0; i