summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_1/lab_15_02_19_D.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_15_02_19_D.cc
parent98f34040820dc3a964b7be59a698323e8cc6c8a3 (diff)
conf: rename
Diffstat (limited to '1_anno/Programmazione_1/lab_15_02_19_D.cc')
-rw-r--r--1_anno/Programmazione_1/lab_15_02_19_D.cc159
1 files changed, 0 insertions, 159 deletions
diff --git a/1_anno/Programmazione_1/lab_15_02_19_D.cc b/1_anno/Programmazione_1/lab_15_02_19_D.cc
deleted file mode 100644
index b050e72..0000000
--- a/1_anno/Programmazione_1/lab_15_02_19_D.cc
+++ /dev/null
@@ -1,159 +0,0 @@
-#include<iostream>
-#include<stdexcept>
-#define DIM 50
-
-using namespace std;
-
-class A {
-public:
- A(short m, short k) : len{m} {
- ptr = new int[len];
- srand(time(NULL));
-
- for(int i = 0; i < len; ++i) {
- ptr[i] = rand()%k + 1;
- }
- }
-
- ~A() {
- delete ptr;
- }
-
- virtual double const f() = 0;
- void print(ostream& os) {
- os << typeid(*this).name() << ", ptr=[ ";
- for(int i = 0; i < len; ++i)
- os << ptr[i] << ' ';
-
- os << "]";
- }
-
- int get(short i) {
- if(i >= len)
- throw out_of_range{"out of range"};
-
- return ptr[i];
- }
-
- short getLen() {
- return len;
- }
-
- int& operator[](int x){
- return ptr[x];
- }
-
- const int& operator[](int x) const {
- return ptr[x];
- }
-
-private:
- int* ptr;
- short len;
-
-};
-
-class B : public A {
-public:
- B(short m, short k, double y) : A{m, k}, p{y} {}
- double const f() {
- int sum{};
- for(int i = 0; i < getLen(); ++i) {
- int t = get(i);
-
- if((t%2) == 0) {
- sum+=t;
- }
- }
-
- if(sum == 0)
- return 0;
-
- return static_cast<double>(sum/p);
- }
-
- void print(ostream& os) {
- A::print(os);
- os << ", p=" << p;
- }
-private:
- double p;
-};
-
-class C : public A {
-public:
- C(short n, short k, char c) : A{n, k}, x{c} {}
-
- double const f() {
- int sum{};
- short c{};
- for(int i = 0; i < getLen(); ++i) {
- int t = get(i);
-
- if((t%2) == 1) {
- sum+=t;
- ++c;
- }
- }
-
- if(c == 0)
- return 0;
-
- return static_cast<double>(sum/c);
- }
-
- string const g(char c) {
- string t{""};
- t+=x;
- t+=c;
-
- return t;
- }
-
- void print(ostream& os) {
- A::print(os);
- os << ", x=" << x;
- }
-private:
- char x;
-};
-
-ostream& operator<<(ostream& os, A& a) {
- a.print(os);
-
- return os;
-}
-
-int main() {
- srand(111222333);
- A *vett[DIM];
-
- string conc{""};
- for(int i=0; i<DIM; i++){
- short n=1+rand()%10;
- short m = 1+rand()%15;
- if(rand()%2==0)
- vett[i]= new B(n, m, rand()/(double) RAND_MAX+0.05);
- else {
- vett[i]= new C(n, m, (char) (rand()%('z' - 'a' + 1) + 'a'));
- auto cpnt = dynamic_cast<C*>(vett[i]);
- conc+=cpnt->g('h');
- }
- }
-
- double sum{};
- for(int i = 0; i < DIM; i++) {
- auto fval = vett[i]->f();
- cout << i << ") " << *(vett[i]) << ", f()=" << fval << endl;
- sum+=fval;
- }
-
- cout << "PUNTO 2:\n\tavg(f())=" << sum/DIM << ", g('h')=" << conc << endl;
- cout << "PUNTO 3:\n";
- cout << (*(vett[0])).get(0) << endl;
- (*(vett[0]))[0] = 24;
- cout << (*(vett[0])).get(0) << endl;
-
-
- return 0;
-}