Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 KB | None | 0 0
  1. ListContainer
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class List{
  7. private:
  8. int *broevi;
  9. int brojBroevi;
  10. public:
  11. List()
  12. {
  13. brojBroevi=0;
  14. broevi=new int[0];
  15. }
  16. List(int *broevi,int brojBroevi)
  17. {
  18. this->brojBroevi=brojBroevi;
  19. this->broevi=new int[brojBroevi];
  20. for(int i=0;i<brojBroevi;i++)
  21. {
  22. this->broevi[i]=broevi[i];
  23. }
  24. }
  25. List(const List &l)
  26. {
  27. brojBroevi=l.brojBroevi;
  28. broevi=new int[brojBroevi];
  29. for(int i=0;i<brojBroevi;i++)
  30. {
  31. broevi[i]=l.broevi[i];
  32. }
  33. }
  34. List &operator=(const List &l)
  35. {
  36. if(this!=&l)
  37. {
  38. delete [] broevi;
  39. brojBroevi=l.brojBroevi;
  40. broevi=new int[brojBroevi];
  41. for(int i=0;i<brojBroevi;i++)
  42. {
  43. broevi[i]=l.broevi[i];
  44. }
  45. }
  46. return *this;
  47. }
  48. int sum()
  49. {
  50. int suma=0;
  51. for(int i=0;i<brojBroevi;i++)
  52. {
  53. suma+=broevi[i];
  54. }
  55. return suma;
  56. }
  57. double average()
  58. {
  59. return sum()*1.0/brojBroevi;
  60. }
  61. int brojElementiLista()
  62. {
  63. int br=0;
  64. for(int i=0;i<brojBroevi;i++)
  65. {
  66. br++;
  67. }
  68. return br;
  69. }
  70. void pecati()
  71. {
  72. cout<<brojBroevi<<": ";
  73. for(int i=0;i<brojBroevi;i++)
  74. {
  75. cout<<" "<<broevi[i];
  76. }
  77. cout<<" sum: "<<sum()<<" average: "<<average()<<endl;
  78. }
  79. ~List()
  80. {
  81. delete [] broevi;
  82. }
  83. };
  84. class ListContainer{
  85. private:
  86. List *listi;
  87. int brojListi;
  88. int brojObidi;
  89. public:
  90. ListContainer()
  91. {
  92. listi=new List[0];
  93. brojListi=0;
  94. brojObidi=0;
  95. }
  96. ListContainer(const ListContainer &lc)
  97. {
  98. brojListi=lc.brojListi;
  99. listi=new List[brojListi];
  100. for(int i=0;i<brojListi;i++)
  101. {
  102. listi[i]=lc.listi[i];
  103. }
  104. brojObidi=lc.brojObidi;
  105. }
  106. ListContainer &operator=(const ListContainer &lc)
  107. {
  108. if(this!=&lc)
  109. {
  110. delete [] listi;
  111. brojListi=lc.brojListi;
  112. listi=new List[brojListi];
  113. for(int i=0;i<brojListi;i++)
  114. {
  115. listi[i]=lc.listi[i];
  116. }
  117. brojObidi=lc.brojObidi;
  118. }
  119. return *this;
  120. }
  121. int sum()
  122. {
  123. int suma=0;
  124. for(int i=0;i<brojListi;i++)
  125. {
  126. suma+=listi[i].sum();
  127. }
  128. return suma;
  129. }
  130. double average()
  131. {
  132. int suma=0;
  133. for(int i=0;i<brojListi;i++)
  134. {
  135. suma+=listi[i].brojElementiLista();
  136. }
  137. return sum()*1.0/suma;
  138. }
  139. void print()
  140. {
  141. if(brojListi==0)
  142. {
  143. cout<<"The list is empty"<<endl;
  144. }
  145. for(int i=0;i<brojListi;i++)
  146. {
  147. cout<<"List number: "<<i+1<<" List info: ";
  148. listi[i].pecati();
  149. }
  150. cout<<"Sum: "<<sum()<<" Average: "<<average()<<endl;
  151.  
  152. }
  153. void addNewList(List l)
  154. {
  155. int flag=0;
  156. for(int i=0;i<brojListi;i++)
  157. {
  158. if(listi[i].sum()==l.sum())
  159. {
  160. flag++;
  161. }
  162. }
  163. if(flag==0)
  164. {
  165. List *temp=new List[brojListi+1];
  166. for(int i=0;i<brojListi;i++)
  167. {
  168. temp[i]=listi[i];
  169. }
  170. temp[brojListi]=l;
  171. delete [] listi;
  172. brojListi++;
  173. listi=temp;
  174. }
  175.  
  176. }
  177. ~ListContainer()
  178. {
  179. delete [] listi;
  180. }
  181.  
  182. };
  183.  
  184. int main()
  185. {
  186. ListContainer lc;
  187. int N;
  188. cin>>N;
  189.  
  190. for (int i=0;i<N;i++) {
  191. int n;
  192. int niza[100];
  193.  
  194. cin>>n;
  195.  
  196. for (int j=0;j<n;j++){
  197. cin>>niza[j];
  198.  
  199. }
  200.  
  201. List l=List(niza,n);
  202.  
  203. lc.addNewList(l);
  204. }
  205.  
  206.  
  207. int testCase;
  208. cin>>testCase;
  209.  
  210. if (testCase==1) {
  211. cout<<"Test case for operator ="<<endl;
  212. ListContainer lc1;
  213. lc1.print();
  214. cout<<lc1.sum()<<" "<<lc.sum()<<endl;
  215. lc1=lc;
  216. lc1.print();
  217. cout<<lc1.sum()<<" "<<lc.sum()<<endl;
  218. lc1.sum();
  219. lc1.average();
  220.  
  221. }
  222. else {
  223. lc.print();
  224. }
  225. return 0;
  226. }
  227. Datotecen sistem
  228. #include <iostream>
  229. #include <string.h>
  230. using namespace std;
  231.  
  232. enum Extension{
  233. TXT,
  234. PDF,
  235. EXE
  236.  
  237. };
  238.  
  239. class File{
  240. private:
  241. char *ime;
  242. Extension tip;
  243. char *imeSopstvenik;
  244. int golemina;
  245. public:
  246. File()
  247. {
  248. ime=new char[0];
  249. imeSopstvenik=new char[0];
  250. }
  251. File(char *ime,char *imeSopstvenik,int golemina,Extension tip)
  252. {
  253. this->ime=new char[strlen(ime)+1];
  254. strcpy(this->ime,ime);
  255. this->imeSopstvenik=new char[strlen(imeSopstvenik)+1];
  256. strcpy(this->imeSopstvenik,imeSopstvenik);
  257. this->golemina=golemina;
  258. this->tip=tip;
  259. }
  260. File(const File &f)
  261. {
  262. ime=new char[strlen(f.ime)+1];
  263. strcpy(ime,f.ime);
  264. imeSopstvenik=new char[strlen(f.imeSopstvenik)+1];
  265. strcpy(imeSopstvenik,f.imeSopstvenik);
  266. golemina=f.golemina;
  267. tip=f.tip;
  268. }
  269. File &operator=(const File &f)
  270. {
  271. if(this!=&f)
  272. {
  273. delete [] ime;
  274. delete [] imeSopstvenik;
  275. ime=new char[strlen(f.ime)+1];
  276. strcpy(ime,f.ime);
  277. imeSopstvenik=new char[strlen(f.imeSopstvenik)+1];
  278. strcpy(imeSopstvenik,f.imeSopstvenik);
  279. golemina=f.golemina;
  280. tip=f.tip;
  281. }
  282. return *this;
  283. }
  284. void print()
  285. {
  286. cout<<"File name: "<<ime<<endl;
  287. cout<<"File owner: "<<imeSopstvenik<<endl;
  288. cout<<"File size: "<<golemina<<endl;
  289. }
  290. bool equals(const File &that)
  291. {
  292. if((strcmp(ime,that.ime))==0 && (tip=that.tip) && (strcmp(imeSopstvenik,that.imeSopstvenik))==0)
  293. {
  294. return true;
  295. }
  296. else
  297. {
  298. return false;
  299. }
  300.  
  301. }
  302. bool equalsType(const File &that)
  303. {
  304. if((strcmp(ime,that.ime)==0) && (tip==that.tip))
  305. {
  306. return true;
  307. }
  308. else
  309. {
  310. return false;
  311. }
  312. }
  313. ~File()
  314. {
  315. delete [] ime;
  316. delete [] imeSopstvenik;
  317. }
  318.  
  319. };
  320. class Folder{
  321. private:
  322. char *imeFolder;
  323. int brojDatoteki;
  324. File *files;
  325. public:
  326. Folder()
  327. {
  328. brojDatoteki=0;
  329. files=new File[0];
  330. }
  331. Folder(char *imeFolder)
  332. {
  333. this->imeFolder=new char[strlen(imeFolder)+1];
  334. strcpy(this->imeFolder,imeFolder);
  335. brojDatoteki=0;
  336. files=new File[0];
  337. }
  338.  
  339. void print()
  340. {
  341. for(int i=0;i<brojDatoteki;i++)
  342. {
  343. files[i].print();
  344. }
  345. }
  346. void remove(const File &file)
  347. {
  348. File *temp = new File[brojDatoteki-1];
  349. for(int i=0;i<brojDatoteki;i++)
  350. {
  351. temp[i]=files[i];
  352. }
  353. temp[brojDatoteki]=file;
  354. delete [] files;
  355. files=temp;
  356. }
  357. void add(const File &file)
  358. {
  359. File *temp = new File[brojDatoteki+1];
  360. for(int i=0;i<brojDatoteki;i++)
  361. {
  362. temp[i]=files[i];
  363. }
  364. temp[brojDatoteki]=file;
  365. delete [] files;
  366. files=temp;
  367. }
  368. ~Folder()
  369. {
  370. delete imeFolder;
  371. delete [] files;
  372. }
  373.  
  374. };
  375. int main()
  376. {
  377. char fileName[20];
  378. char fileOwner[20];
  379. int ext;
  380. int fileSize;
  381.  
  382. int testCase;
  383. cin >> testCase;
  384. if (testCase == 1) {
  385. cout << "======= FILE CONSTRUCTORS AND = OPERATOR =======" << endl;
  386. cin >> fileName;
  387. cin >> fileOwner;
  388. cin >> fileSize;
  389. cin >> ext;
  390.  
  391. File created = File(fileName, fileOwner, fileSize, (Extension) ext);
  392. File copied = File(created);
  393. File assigned = created;
  394.  
  395. cout << "======= CREATED =======" << endl;
  396. created.print();
  397. cout << endl;
  398. cout << "======= COPIED =======" << endl;
  399. copied.print();
  400. cout << endl;
  401. cout << "======= ASSIGNED =======" << endl;
  402. assigned.print();
  403. }
  404. else if (testCase == 2) {
  405. cout << "======= FILE EQUALS & EQUALS TYPE =======" << endl;
  406. cin >> fileName;
  407. cin >> fileOwner;
  408. cin >> fileSize;
  409. cin >> ext;
  410.  
  411. File first(fileName, fileOwner, fileSize, (Extension) ext);
  412. first.print();
  413.  
  414. cin >> fileName;
  415. cin >> fileOwner;
  416. cin >> fileSize;
  417. cin >> ext;
  418.  
  419. File second(fileName, fileOwner, fileSize, (Extension) ext);
  420. second.print();
  421.  
  422. cin >> fileName;
  423. cin >> fileOwner;
  424. cin >> fileSize;
  425. cin >> ext;
  426.  
  427. File third(fileName, fileOwner, fileSize, (Extension) ext);
  428. third.print();
  429.  
  430. bool equals = first.equals(second);
  431. cout << "FIRST EQUALS SECOND: ";
  432. if (equals)
  433. cout << "TRUE" << endl;
  434. else
  435. cout << "FALSE" << endl;
  436.  
  437. equals = first.equals(third);
  438. cout << "FIRST EQUALS THIRD: ";
  439. if (equals)
  440. cout << "TRUE" << endl;
  441. else
  442. cout << "FALSE" << endl;
  443.  
  444. bool equalsType = first.equalsType(second);
  445. cout << "FIRST EQUALS TYPE SECOND: ";
  446. if (equalsType)
  447. cout << "TRUE" << endl;
  448. else
  449. cout << "FALSE" << endl;
  450.  
  451. equalsType = second.equals(third);
  452. cout << "SECOND EQUALS TYPE THIRD: ";
  453. if (equalsType)
  454. cout << "TRUE" << endl;
  455. else
  456. cout << "FALSE" << endl;
  457.  
  458. }
  459. else if (testCase == 3) {
  460. cout << "======= FOLDER CONSTRUCTOR =======" << endl;
  461. cin >> fileName;
  462. Folder folder(fileName);
  463. folder.print();
  464.  
  465. }
  466. else if (testCase == 4) {
  467. cout << "======= ADD FILE IN FOLDER =======" << endl;
  468. char name[20];
  469. cin >> name;
  470. Folder folder(name);
  471.  
  472. int iter;
  473. cin >> iter;
  474.  
  475. while (iter > 0) {
  476. cin >> fileName;
  477. cin >> fileOwner;
  478. cin >> fileSize;
  479. cin >> ext;
  480.  
  481. File file(fileName, fileOwner, fileSize, (Extension) ext);
  482. folder.add(file);
  483. iter--;
  484. }
  485. folder.print();
  486. }
  487. else {
  488. cout << "======= REMOVE FILE FROM FOLDER =======" << endl;
  489. char name[20];
  490. cin >> name;
  491. Folder folder(name);
  492.  
  493. int iter;
  494. cin >> iter;
  495.  
  496. while (iter > 0) {
  497. cin >> fileName;
  498. cin >> fileOwner;
  499. cin >> fileSize;
  500. cin >> ext;
  501.  
  502. File file(fileName, fileOwner, fileSize, (Extension) ext);
  503. folder.add(file);
  504. iter--;
  505. }
  506. cin >> fileName;
  507. cin >> fileOwner;
  508. cin >> fileSize;
  509. cin >> ext;
  510.  
  511. File file(fileName, fileOwner, fileSize, (Extension) ext);
  512. folder.remove(file);
  513. folder.print();
  514. }
  515. return 0;
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement