Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. enum Extension{txt, pdf, exe};
  5. using namespace std;
  6. class File{
  7. private:
  8. char *ime;
  9. Extension ekstenzija;
  10. char *sopstvenik;
  11. int golemina;
  12. public:
  13. File(){
  14. ime=NULL;
  15. sopstvenik=NULL;
  16. ekstenzija=Extension (0);
  17. golemina=0;
  18. }
  19. File(char *ime, char *sopstvenik, int golemina, Extension ekstenzija){
  20. this->ekstenzija=ekstenzija;
  21. this->golemina=golemina;
  22. this->ime=new char[strlen(ime)+1];
  23. strcpy(this->ime, ime);
  24. this->sopstvenik=new char[strlen(sopstvenik)+1];
  25. strcpy(this->sopstvenik, sopstvenik);
  26. }
  27. File(const File &f){
  28. this->ekstenzija=f.ekstenzija;
  29. this->golemina=f.golemina;
  30. this->ime=new char[strlen(f.ime)+1];
  31. strcpy(this->ime, f.ime);
  32. this->sopstvenik=new char[strlen(f.sopstvenik)+1];
  33. strcpy(this->sopstvenik, f.sopstvenik);
  34. }
  35. File &operator=(const File &f){
  36. if(this!=&f) {
  37. this->ekstenzija = f.ekstenzija;
  38. this->golemina = f.golemina;
  39. delete [] ime;
  40. this->ime = new char[strlen(f.ime) + 1];
  41. strcpy(this->ime, f.ime);
  42. delete [] sopstvenik;
  43. this->sopstvenik = new char[strlen(f.sopstvenik) + 1];
  44. strcpy(this->sopstvenik, f.sopstvenik);
  45. }
  46. return *this;
  47. }
  48. ~File(){
  49. delete ime;
  50. delete sopstvenik;
  51. }
  52. void print(){
  53. cout<<"File name: "<<ime<<".";
  54. if(ekstenzija==0) cout<<"pdf"<<endl;
  55. if(ekstenzija==1) cout<<"txt"<<endl;
  56. if(ekstenzija==2) cout<<"exe"<<endl;
  57. cout<<"File owner: "<<sopstvenik<<endl;
  58. cout<<"File size: "<<golemina<<endl;
  59. }
  60. bool equals(const File & that){
  61. if((!strcmp(that.ime, ime))&&(!strcmp(sopstvenik, that.sopstvenik))&&(that.ekstenzija==ekstenzija)){
  62. return true;
  63. }
  64. else return false;
  65. }
  66. bool equalsType(const File & that){
  67. if((!strcmp(that.ime, ime)) && (that.ekstenzija==ekstenzija)){
  68. return true;
  69. }
  70. else return false;
  71. }
  72. };
  73. class Folder{
  74. private:
  75. char *ime;
  76. int n;
  77. File *fajlovi;
  78. public:
  79. Folder(){
  80. fajlovi=NULL;
  81. n=0;
  82. }
  83. Folder(const char *ime){
  84. this->ime=new char[strlen(ime)+1];
  85. strcpy(this->ime, ime);
  86. fajlovi=NULL;
  87. n=0;
  88. }
  89. Folder(const Folder &f){
  90. this->ime=new char[strlen(f.ime)+1];
  91. strcpy(this->ime, f.ime);
  92. this->n=f.n;
  93. this->fajlovi=new File[n];
  94. for(int i=0; i<n; i++){
  95. fajlovi[i]=f.fajlovi[i];
  96. }
  97. }
  98. Folder &operator=(const Folder &f){
  99. if(this!=&f) {
  100. delete [] ime;
  101. delete [] fajlovi;
  102. this->ime = new char[strlen(f.ime) + 1];
  103. strcpy(this->ime, f.ime);
  104. this->n = f.n;
  105. this->fajlovi = new File[n];
  106. for (int i = 0; i < n; i++) {
  107. fajlovi[i] = f.fajlovi[i];
  108. }
  109. }
  110. return *this;
  111. }
  112. ~Folder(){
  113. delete [] fajlovi;
  114. delete [] ime;
  115. }
  116. void print(){
  117. cout<<"Folder name: results"<<endl;
  118. for(int i=0; i<n; i++){
  119. fajlovi[i].print();
  120. }
  121. }
  122. void remove(const File & file){
  123. int razlicni=0;
  124. for(int i=0; i<n; i++){
  125. if(fajlovi[i].equals(file)==false){
  126. razlicni++;
  127. }
  128. }
  129. File *tmp=new File[razlicni];
  130. int j=0;
  131. for(int i=0; i<n; i++){
  132. if(fajlovi[i].equals(file)==false){
  133. tmp[j]=fajlovi[i];
  134. j++;
  135. }
  136. }
  137. delete [] fajlovi;
  138. fajlovi=tmp;
  139. n=razlicni;
  140. }
  141. void add(const File & file){
  142. File *tmp=new File[n+1];
  143. for(int i=0; i<n; i++){
  144. tmp[i]=fajlovi[i];
  145. }
  146. tmp[n]=file;
  147. n++;
  148. delete [] fajlovi;
  149. fajlovi=tmp;
  150. }
  151. };
  152. int main() {
  153. char fileName[20];
  154. char fileOwner[20];
  155. int ext;
  156. int fileSize;
  157.  
  158. int testCase;
  159. cin >> testCase;
  160. if (testCase == 1) {
  161. cout << "======= FILE CONSTRUCTORS AND = OPERATOR =======" << endl;
  162. cin >> fileName;
  163. cin >> fileOwner;
  164. cin >> fileSize;
  165. cin >> ext;
  166.  
  167. File created = File(fileName, fileOwner, fileSize, (Extension) ext);
  168. File copied = File(created);
  169. File assigned = created;
  170.  
  171. cout << "======= CREATED =======" << endl;
  172. created.print();
  173. cout << endl;
  174. cout << "======= COPIED =======" << endl;
  175. copied.print();
  176. cout << endl;
  177. cout << "======= ASSIGNED =======" << endl;
  178. assigned.print();
  179. }
  180. else if (testCase == 2) {
  181. cout << "======= FILE EQUALS & EQUALS TYPE =======" << endl;
  182. cin >> fileName;
  183. cin >> fileOwner;
  184. cin >> fileSize;
  185. cin >> ext;
  186.  
  187. File first(fileName, fileOwner, fileSize, (Extension) ext);
  188. first.print();
  189.  
  190. cin >> fileName;
  191. cin >> fileOwner;
  192. cin >> fileSize;
  193. cin >> ext;
  194.  
  195. File second(fileName, fileOwner, fileSize, (Extension) ext);
  196. second.print();
  197.  
  198. cin >> fileName;
  199. cin >> fileOwner;
  200. cin >> fileSize;
  201. cin >> ext;
  202.  
  203. File third(fileName, fileOwner, fileSize, (Extension) ext);
  204. third.print();
  205.  
  206. bool equals = first.equals(second);
  207. cout << "FIRST EQUALS SECOND: ";
  208. if (equals)
  209. cout << "TRUE" << endl;
  210. else
  211. cout << "FALSE" << endl;
  212.  
  213. equals = first.equals(third);
  214. cout << "FIRST EQUALS THIRD: ";
  215. if (equals)
  216. cout << "TRUE" << endl;
  217. else
  218. cout << "FALSE" << endl;
  219.  
  220. bool equalsType = first.equalsType(second);
  221. cout << "FIRST EQUALS TYPE SECOND: ";
  222. if (equalsType)
  223. cout << "TRUE" << endl;
  224. else
  225. cout << "FALSE" << endl;
  226.  
  227. equalsType = second.equals(third);
  228. cout << "SECOND EQUALS TYPE THIRD: ";
  229. if (equalsType)
  230. cout << "TRUE" << endl;
  231. else
  232. cout << "FALSE" << endl;
  233.  
  234. }
  235. else if (testCase == 3) {
  236. cout << "======= FOLDER CONSTRUCTOR =======" << endl;
  237. cin >> fileName;
  238. Folder folder(fileName);
  239. folder.print();
  240.  
  241. }
  242. else if (testCase == 4) {
  243. cout << "======= ADD FILE IN FOLDER =======" << endl;
  244. char name[20];
  245. cin >> name;
  246. Folder folder(name);
  247.  
  248. int iter;
  249. cin >> iter;
  250.  
  251. while (iter > 0) {
  252. cin >> fileName;
  253. cin >> fileOwner;
  254. cin >> fileSize;
  255. cin >> ext;
  256.  
  257. File file(fileName, fileOwner, fileSize, (Extension) ext);
  258. folder.add(file);
  259. iter--;
  260. }
  261. folder.print();
  262. }
  263. else {
  264. cout << "======= REMOVE FILE FROM FOLDER =======" << endl;
  265. char name[20];
  266. cin >> name;
  267. Folder folder(name);
  268.  
  269. int iter;
  270. cin >> iter;
  271.  
  272. while (iter > 0) {
  273. cin >> fileName;
  274. cin >> fileOwner;
  275. cin >> fileSize;
  276. cin >> ext;
  277.  
  278. File file(fileName, fileOwner, fileSize, (Extension) ext);
  279. folder.add(file);
  280. iter--;
  281. }
  282. cin >> fileName;
  283. cin >> fileOwner;
  284. cin >> fileSize;
  285. cin >> ext;
  286.  
  287. File file(fileName, fileOwner, fileSize, (Extension) ext);
  288. folder.remove(file);
  289. folder.print();
  290. }
  291. return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement