Advertisement
Guest User

Untitled

a guest
Mar 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. //Solution
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. class List {
  6.  
  7. private:
  8.  
  9. int * numbers;
  10. int numNumbers;
  11.  
  12. void copy(const List& l) {
  13. numNumbers = l.numNumbers;
  14. numbers = new int[numNumbers];
  15. for (int i = 0; i < numNumbers; ++i) {
  16. numbers[i] = l.numbers[i];
  17. }
  18. }
  19.  
  20. public:
  21.  
  22.  
  23. List() {
  24. numbers = new int[0];
  25. numNumbers = 0;
  26. }
  27.  
  28. List(int * lista, int broj) {
  29.  
  30. numNumbers = broj;
  31. numbers = new int[numNumbers];
  32. for (int i = 0; i < numNumbers; ++i) {
  33. numbers[i] = lista[i];
  34. }
  35. }
  36.  
  37. ~List() {
  38. delete [] numbers;
  39. }
  40.  
  41. List(const List& l) {
  42. copy(l);
  43. }
  44.  
  45. List & operator = (const List& l) {
  46. if (this != &l) {
  47. delete [] numbers;
  48. copy(l);
  49. }
  50. return *this;
  51. }
  52.  
  53. int sum() {
  54. int suma = 0;
  55. for (int i=0; i < numNumbers; ++i) {
  56. suma += numbers[i];
  57. }
  58. return suma;
  59. }
  60.  
  61. double average() {
  62. return sum() * 1.0 / numNumbers;
  63. }
  64.  
  65. void pecati() {
  66. cout << numNumbers << ": ";
  67. for (int i=0; i < numNumbers; ++i) {
  68. cout << numbers[i] << " ";
  69. }
  70. cout << "sum: " << sum();
  71. cout << " average: " << average();
  72. }
  73.  
  74. int getNumber() {
  75. return numNumbers;
  76. }
  77. };
  78.  
  79. class ListContainer {
  80.  
  81. private:
  82. List * listi;
  83. int numListi;
  84. int attempts;
  85.  
  86. void copy(const ListContainer &lc) {
  87.  
  88. numListi = lc.numListi;
  89. attempts = lc.attempts;
  90. listi = new List[numListi];
  91. for (int i=0; i < numListi; ++i) {
  92. listi[i] = lc.listi[i];
  93. }
  94. }
  95.  
  96. public:
  97. ListContainer() {
  98. listi = new List[0];
  99. numListi = 0;
  100. attempts = 0;
  101. }
  102.  
  103. ~ListContainer() {
  104. delete [] listi;
  105. }
  106.  
  107. ListContainer(const ListContainer &lc) {
  108. copy(lc);
  109. }
  110.  
  111. ListContainer & operator =(const ListContainer &lc) {
  112. if (this != &lc) {
  113. delete [] listi;
  114. copy(lc);
  115. }
  116. return *this;
  117. }
  118.  
  119. int sum() {
  120. int suma = 0;
  121. for (int i=0; i < numListi; ++i){
  122. suma += listi[i].sum();
  123. }
  124. return suma;
  125. }
  126.  
  127. double average() {
  128. int suma = sum();
  129. int numElements = 0;
  130. for (int i=0; i < numListi; ++i) {
  131. numElements += listi[i].getNumber();
  132. }
  133. return suma / (numElements * 1.0);
  134. }
  135.  
  136. void addNewList(List l){
  137. ++attempts;
  138. for (int i=0; i < numListi; ++i) {
  139. if (l.sum() == listi[i].sum()){
  140. return;
  141. }
  142. }
  143.  
  144. List * tmp = new List[numListi + 1];
  145. for (int i=0; i < numListi; ++i) {
  146. tmp[i] = listi[i];
  147. }
  148.  
  149. tmp[numListi++] = l;
  150. delete [] listi;
  151. listi = tmp;
  152. }
  153.  
  154. void print() {
  155. if (!numListi) {
  156. cout << "The list is empty\n";
  157. return;
  158. }
  159. else {
  160. for (int i = 0; i < numListi; ++i) {
  161. cout << "List number: " << i + 1 << " List info: ";
  162. listi[i].pecati();
  163. cout << endl;
  164. }
  165. }
  166. cout << "Sum: " << sum();
  167. cout << " Average: " << average() << endl;
  168. cout << "Successful attempts: " << numListi << " Failed attempts: " << attempts - numListi<< endl;
  169.  
  170. }
  171.  
  172. };
  173.  
  174.  
  175. int main() {
  176.  
  177. ListContainer lc;
  178. int N;
  179. cin>>N;
  180.  
  181. for (int i=0;i<N;i++) {
  182. int n;
  183. int niza[100];
  184.  
  185. cin>>n;
  186.  
  187. for (int j=0;j<n;j++){
  188. cin>>niza[j];
  189.  
  190. }
  191.  
  192. List l=List(niza,n);
  193.  
  194. lc.addNewList(l);
  195. }
  196.  
  197.  
  198. int testCase;
  199. cin>>testCase;
  200.  
  201. if (testCase==1) {
  202. cout<<"Test case for operator ="<<endl;
  203. ListContainer lc1;
  204. lc1.print();
  205. cout<<lc1.sum()<<" "<<lc.sum()<<endl;
  206. lc1=lc;
  207. lc1.print();
  208. cout<<lc1.sum()<<" "<<lc.sum()<<endl;
  209.  
  210. }
  211. else {
  212. lc.print();
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement