Advertisement
1abinot

Untitled

Jun 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cmath>
  4.  
  5. using namespace std;
  6.  
  7. #include<iostream>
  8. #include<cstring>
  9. #include<cmath>
  10.  
  11. using namespace std;
  12.  
  13. class List
  14. {
  15. private:
  16. int *niza;
  17. int n;
  18. public:
  19. List(){
  20. this->niza=new int[0];
  21. this->n=0;
  22. }
  23. List(int n)
  24. {
  25. this->niza=new int[50];
  26. for(int i=0;i<n;i++)
  27. {
  28. this->niza[i]=niza[i];
  29. }
  30. this->n=n;
  31. }
  32. List(const List &l)
  33. {
  34. this->niza=new int[l.n];
  35. for(int i=0;i<n;i++)
  36. {
  37. this->niza[i]=l.niza[i];
  38. }
  39. this->n=l.n;
  40. }
  41. List& operator=(const List &l)
  42. {
  43. delete []niza;
  44. this->niza=new int[l.n];
  45. for(int i=0;i<l.n;i++)
  46. {
  47. this->niza[i]=l.niza[i];
  48. }
  49. this->n=l.n;
  50. return *this;
  51. }
  52. int sum()
  53. {
  54. int suma=0;
  55. for(int i=0;i<n;i++)
  56. {
  57. suma+=niza[i];
  58. }
  59. return suma;
  60. }
  61. double average()
  62. {
  63. double avg=0.0;
  64. avg=(double)sum()/n;
  65. return avg;
  66. }
  67. bool operator==(List &l)
  68. {
  69. if(sum()==l.sum())
  70. return true;
  71. else return false;
  72. }
  73. friend ostream& operator<<(ostream& out,List& l)
  74. {
  75. out<<l.n<<": ";
  76. for(int i=0;i<l.n;i++)
  77. {
  78. out<<l.niza[i]<<" ";
  79. }
  80. out<<"sum: "<<l.sum()<<" average: "<<l.average()<<endl;
  81. return out;
  82. }
  83. friend istream& operator>>(istream &inp,List& l)
  84. {
  85. inp>>l.n;
  86. for(int i=0;i<l.n;i++)
  87. {
  88. inp>>l.niza[i];
  89. }
  90. return inp;
  91. }
  92. friend List operator+ (List &l1, List &l2) {
  93. List x;
  94. List res(l1);
  95. if(l1.n==l2.n) {
  96. for(int i=0; i<l1.n; ++i)
  97. res.niza[i]+=l2.niza[i];
  98. return res;
  99. } else
  100. cout<<"The list is empty"<<endl;
  101. }
  102. List& operator-(const List &l)
  103. {
  104. if(n==l.n)
  105. {
  106. List tmp = List(n-l.n);
  107. for(int i=0;i<n;i++)
  108. {
  109.  
  110. tmp.niza[i]=niza[i]-l.niza[i];
  111.  
  112. }
  113. return tmp;
  114. }
  115. else return *this;
  116. }
  117. List& operator++()
  118. {
  119. for(int i=0;i<n;++i)
  120. {
  121. ++niza[i];
  122. }
  123. return *this;
  124. }
  125. List operator++(int)
  126. {
  127. List l(*this);
  128. for(int i=0;i<n;++i)
  129. {
  130. niza[i]++;
  131. }
  132. return l;
  133. }
  134. List& operator--(int)
  135. {
  136. for(int i=0;i<n;i++)
  137. {
  138. niza[i]--;
  139. }
  140. return *this;
  141. }
  142. List& operator+=(int l)
  143. {
  144. int *tmp=new int[n+1];
  145. for(int i=0;i<n;i++)
  146. {
  147. tmp[i]=niza[i];
  148. }
  149. tmp[n++]=l;
  150. delete []niza;
  151. niza=tmp;
  152. return *this;
  153. }
  154. int& operator[](int index) {
  155. return niza[index];
  156. }
  157. List& addList(List l)
  158. {
  159. List tmp=List(n+l.n);
  160. for(int i=0;i<n;i++)
  161. {
  162. tmp.niza[i]=niza[i]+l.niza[i];
  163. }
  164. return tmp;
  165. }
  166. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement