Advertisement
Martina312

Podatocen Centar

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