Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <sstream>
- #include <typeinfo>
- #define DIM 50
- using namespace std;
- class A{
- private:
- int *ptr;
- short len;
- public:
- A(short m, short k){
- ptr=new int[m];
- for(short i=0; i<m; i++)
- ptr[i]=rand()%k+1;
- this->len=m;
- }
- virtual double f()=0;
- int get(short i){return this->ptr[i%len];}
- short getLen(){return this->len;}
- virtual ostream &put(ostream &s){
- stringstream ss;
- ss<<", ptr=[ ";
- for(short i=0; i<getLen(); i++)
- ss<<get(i)<<" ";
- ss<<"]";
- return s<<ss.str();
- }
- int &operator[](short k){return ptr[k%len];}
- };
- class B: public A{
- private:
- double p;
- public:
- B(short m, short k, double y): A(m,k), p(y){}
- double f(){
- int somma=0;
- for(short i=0; i<getLen(); i++)
- if(get(i)%2==0)
- somma+=get(i);
- return somma/p;
- }
- ostream &put(ostream &s){
- A::put(s);
- return s<<", p="<<p;
- }
- };
- class C: public A{
- private:
- char x;
- public:
- C(short n, short k, char c): A(n,k), x(c){}
- double f(){
- double avg=0;
- short counter=0;
- for(short i=0; i<getLen(); i++)
- if(get(i)%2!=0){
- avg+=get(i);
- counter++;
- }
- if(counter==0)
- return 0;
- else
- return avg/counter;
- }
- string g(char c){
- stringstream ss;
- ss<<x<<c;
- return ss.str();
- }
- ostream &put(ostream &s){
- A::put(s);
- return s<<", x="<<x;
- }
- };
- ostream &operator<<(ostream &s, A &a){
- if(typeid(a)==typeid(B))
- s<<"1B";
- else if(typeid(a)==typeid(C))
- s<<"1C";
- return a.put(s);
- }
- int main()
- {
- srand(111222333);
- A *vett[DIM];
- for(int i=0; i<DIM; i++){
- short n=1+rand()%10;
- short m = 1+rand()%8;
- 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'));
- }
- cout<<"PUNTO 1:"<<endl;
- for(int i=0; i<DIM; i++)
- cout<<i<<")"<<*vett[i]<<", f()="<<vett[i]->f()<<endl;
- cout<<endl<<"PUNTO 2:"<<endl;
- double avg=0;
- stringstream conc;
- C *ptr;
- for(int i=0; i<DIM; i++){
- avg+=vett[i]->f();
- if((ptr=dynamic_cast<C*>(vett[i]))!=NULL)
- conc<<ptr->g('h');
- }
- cout<<"AVG="<<avg/DIM<<", CONC="<<conc.str()<<endl;
- cout<<endl<<"PUNTO 3:"<<endl;
- cout<<*vett[0]<<endl;
- (*vett[0])[1]=100;
- cout<<"(*vett[0])[1]=100"<<endl;
- cout<<*vett[0]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement