LegoDrifter

4/5

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