Advertisement
Guest User

Untitled

a guest
May 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #include <sstream>
  5. #include <typeinfo>
  6. #define DIM 30
  7.  
  8. using namespace std;
  9.  
  10. class MyArray {
  11.  
  12. private:
  13. int dim;
  14. short *data;
  15. public:
  16. MyArray(int dim) {
  17. this->dim = dim;
  18. data = new short[this->dim];
  19.  
  20. for(int i = 0; i < this->dim;i++){
  21. data[i] = static_cast<short>((this->dim)*sin(this->dim + i));
  22. // cout << "[ "<<data[i] << "]";
  23. }
  24. // cout << endl;
  25. }
  26.  
  27. float avg() {
  28. float average = 0.0;
  29. for(int i = 0; i < this->dim;i++){
  30. average += data[i];
  31. }
  32.  
  33. return average/this->dim;
  34. }
  35.  
  36. int greater(int q) {
  37. int counter = 0;
  38. for(int i = 0; i < (this->dim);i++){
  39. if(data[i] > q){
  40. counter++;
  41. }
  42. }
  43. return counter;
  44. }
  45.  
  46. virtual ostream &put(ostream &os){
  47. stringstream temp;
  48. for(int i = 0; i < this->dim ; i++){
  49. temp << data[i] << " ";
  50. }
  51. string str = temp.str();
  52. return os << str;
  53. }
  54. };
  55.  
  56. ostream & operator << (ostream & left, MyArray & obj)
  57. {
  58. return obj.put(left);
  59. }
  60.  
  61. class A {
  62.  
  63. private: int dim;
  64.  
  65. protected: MyArray *x;
  66.  
  67. public:
  68. A(int dim ) {
  69. this->dim = dim;
  70. x = new MyArray(dim);
  71. }
  72. virtual float f() const = 0;
  73.  
  74. virtual ostream &put(ostream &os){
  75.  
  76. return os << "dim = " << dim << " " << "arr: " << "["<< *x<<"]";
  77. }
  78. };
  79.  
  80. ostream &operator<< (ostream &left, A &obj){
  81. return obj.put(left);
  82. }
  83. class B: public A {
  84.  
  85. public:
  86. B(int dim) : A(dim) {}
  87.  
  88. float f() const { return x->avg();} // nel caso non dovesse funzionare riguardare avg();
  89.  
  90. ostream &put(ostream &os) {
  91. return A::put(os) << " Class B " << "f() = " << f() << " ";
  92. }
  93. };
  94.  
  95. class C : public A {
  96.  
  97. private: double w;
  98.  
  99. public:
  100. C(int dim, double w) : A(dim) { this->w = w;}
  101.  
  102. float f() const {
  103. int intera = (int)this->w;
  104. return x->greater(intera);
  105.  
  106. }
  107.  
  108. float f(int p) const {return (this->w)*p;}
  109.  
  110. ostream &put(ostream &os) {
  111. return A::put(os) << " Class C "<< "f() = " << f() << " " << "w = "<< w << " " << "f(4) = " << f(4);
  112. }
  113. };
  114. int main()
  115. {
  116. A* vett[DIM];
  117. srand(833274768);
  118.  
  119. float sommaF = 0.0;
  120. float sommaF4 = 0.0;
  121. int counter = 0;
  122. int iC = 0;
  123.  
  124. for(int i = 0; i < DIM; i++){
  125. int x = 1 + rand()%5;
  126.  
  127. switch(x%2) {
  128.  
  129. case 0 : vett[i] = new B(x);
  130. break;
  131. case 1 : vett[i] = new C(x,x/(double)RAND_MAX*10000);
  132. }
  133. }
  134.  
  135. for(int i = 0; i < DIM; i++){
  136. cout << i << ")"<< *vett[i] << endl << endl;
  137. }
  138.  
  139. for(int i = 0; i < DIM; i++){
  140. sommaF += vett[i]->f();
  141.  
  142. if(typeid(*vett[i]) == typeid(C)){
  143. counter++;
  144. if(counter == 5){
  145. iC = i;
  146. sommaF4 = ((C*)vett[i])->f(4);
  147. }
  148. }
  149. }
  150.  
  151. cout << "sommaF = " << sommaF << " " << "SommaF(4) = " << sommaF4 << " di indice " << iC << endl << endl;
  152.  
  153. system("PAUSE");
  154. return EXIT_SUCCESS;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement