Advertisement
metalni

OOP Labs 4 Datotecen Sistem

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