Advertisement
Guest User

Untitled

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