summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_1/lab_28_02_19_A.cc
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-02-06 19:56:36 +0100
committerSanto Cariotti <santo@dcariotti.me>2021-02-06 19:56:36 +0100
commitd2edbc38cac8da52f58c5cd3da6c0c625fa05736 (patch)
treea51e9a4e56fc9d4c7c9e37576dceedca3a0c72b4 /1_anno/Programmazione_1/lab_28_02_19_A.cc
parent98f34040820dc3a964b7be59a698323e8cc6c8a3 (diff)
conf: rename
Diffstat (limited to '1_anno/Programmazione_1/lab_28_02_19_A.cc')
-rw-r--r--1_anno/Programmazione_1/lab_28_02_19_A.cc166
1 files changed, 0 insertions, 166 deletions
diff --git a/1_anno/Programmazione_1/lab_28_02_19_A.cc b/1_anno/Programmazione_1/lab_28_02_19_A.cc
deleted file mode 100644
index ff2eaae..0000000
--- a/1_anno/Programmazione_1/lab_28_02_19_A.cc
+++ /dev/null
@@ -1,166 +0,0 @@
-#include<iostream>
-#include<stdexcept>
-#include<cstdlib>
-#include<cmath>
-#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<typename T>
-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<double>(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<double>(a+y)/static_cast<double>(rand_elem);
- }
-
- double g(short w) {
- // sicuri che non debba convertire a double? qui sarĂ  sempre 0, forse 1
- return static_cast<double>(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<DIM; i++) {
- short n=1+rand()%10;
- switch(rand()%3) {
- case 0:
- {
- vett[i]= new C(n, rand()%10 + 1);
- i_to_p = i;
- auto cref = dynamic_cast<C&>(*(vett[i]));
- gsum+=cref.g(5);
- ++c_count;
- break;
- }
- case 1:
- vett[i]= new B<string>(n, rand()%('z' - 'a' + 1) + 'a');
- break;
- case 2:
- vett[i]= new B<char>(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<double>(gsum/c_count)) << endl;
- if(i_to_p >= 0) {
- cout << *(vett[i_to_p]) << endl;
- auto cref = dynamic_cast<C*>(vett[i_to_p]);
- ++(*cref);
- cout << *(vett[i_to_p]) << endl;
- }
-
- return 0;
-}