Advertisement
xSiRON

Lab Prova

Feb 5th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <typeinfo>
  4. #include <cstdlib>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. class C{
  9. private:
  10.     float x;
  11.  
  12. public:
  13.     C(float _x) : x(_x) {}
  14.     virtual double f() = 0;
  15.     float getX() { return x; }
  16.     virtual ostream &put(ostream &os){
  17.         return os << "[" << x;
  18.     }
  19. };
  20.  
  21. ostream &operator<<(ostream &stream, C &obj){
  22.     return obj.put(stream);
  23. }
  24.  
  25. class D: public C{
  26. private:
  27.     char c;
  28.  
  29. public:
  30.     D(float _x) : C(_x) {
  31.         c = (char)('a' + (int)(_x*1000000000) % 20);
  32.     }
  33.     double f(){ return sin(getX()) * sin(getX()); }
  34.     char getC() { return c; }
  35.     ostream &put(ostream &os){
  36.         return C::put(os) << " c='" << c << "'], \t";
  37.     }
  38. };
  39.  
  40. class E: public C{
  41. private:
  42.     int n;
  43.  
  44. public:
  45.     E(float _x): C(_x) {
  46.         n = (int)(_x*1000000000)%10;
  47.     }
  48.     double f() { return cos(getX()) * cos(getX()); }
  49.     int getN(){ return n; }
  50.     ostream &put(ostream &os){
  51.         return C::put(os) << " n=" << n << "]\t";
  52.     }
  53. };
  54.  
  55. class A{
  56. protected:
  57.     C* obj1;
  58.     C* obj2;
  59.  
  60. public:
  61.     A(float _x){
  62.         obj1 = new D(_x);
  63.         obj2 = new E(_x);
  64.     }
  65.     double g(){
  66.         return obj1->f() + obj2->f();
  67.     }
  68.  
  69.     virtual ostream &put(ostream &os){
  70.         os << *obj1 << *obj2 << "g()= " << g() << "\t";
  71.     }
  72. };
  73.  
  74. ostream &operator<<(ostream &stream, A &obj){
  75.     return obj.put(stream);
  76. }
  77.  
  78. class B: public A{
  79. public:
  80.     B(float _x) : A(_x) {}
  81.     string st(){
  82.         string s;
  83.         for(int i = 0; i < ((E*)obj2)->getN(); i++){
  84.             s += ((D*)obj1)->getC();
  85.         }
  86.         stringstream stream;
  87.         stream << g() << s;
  88.         return stream.str();
  89.     }
  90.  
  91.     ostream &put(ostream &os){
  92.         return A::put(os) << "st()= " << st();
  93.     }
  94. };
  95.  
  96. int main(){
  97.     srand(328832748);
  98.     const int DIM = 50;
  99.     A* vett[DIM];
  100.  
  101.     for(int i = 0; i < DIM; i++){
  102.         if(rand()%2 == 1)
  103.             vett[i] = new A((float)rand()/INT_MAX);
  104.         else
  105.             vett[i] = new B((float)rand()/INT_MAX);
  106.     }
  107.  
  108.     double sum1 = 0;
  109.     int sum2 = 0;
  110.     string s;
  111.  
  112.     for(int i = 0; i < DIM; i++){
  113.         sum1 += vett[i]->g();
  114.         cout << i << ")\t";
  115.         if(typeid(*vett[i]) == typeid(A))
  116.             cout << "class A: ";
  117.         else{
  118.             cout << "class B: ";
  119.             s = ((B*)vett[i])->st();
  120.             int k = s.length();
  121.             for(int i = 0; i < k; i++)
  122.                 sum2 += s[i];
  123.  
  124.         }
  125.         cout << *vett[i] << endl;
  126.     }
  127.  
  128.     cout << "\nPunto 1: sum1=" << sum1 << "\nPunto 2: sum2=" << sum2 << endl;
  129.  
  130.     cout << "\nPremi INVIO per continuare... ";
  131.     cin.get();
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement