Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- enum Extension{txt, pdf, exe};
- class File
- {
- private:
- char* fileName;
- Extension ext;
- char* fileOwner;
- int fileSize;
- public:
- File(char* fileName=0,char* fileOwner=0, int fileSize=0, Extension ext=Extension(0))
- {
- this->ext=ext;
- this->fileSize=fileSize;
- this->fileName=new char[strlen(fileName)];
- strcpy(this->fileName,fileName);
- this->fileOwner=new char[strlen(fileOwner)];
- strcpy(this->fileOwner,fileOwner);
- }
- File(const File &f)
- {
- this->ext=f.ext;
- this->fileSize=f.fileSize;
- this->fileName=new char[strlen(f.fileName)];
- strcpy(this->fileName,f.fileName);
- this->fileOwner=new char[strlen(f.fileOwner)];
- strcpy(this->fileOwner,f.fileOwner);
- }
- ~File()
- {
- delete [] fileName;
- delete [] fileOwner;
- }
- File& operator=(const File &f)
- {
- if(this==&f)
- {
- return *this;
- }
- this->ext=f.ext;
- this->fileSize=f.fileSize;
- delete [] fileName;
- this->fileName=new char[strlen(f.fileName)];
- strcpy(this->fileName,f.fileName);
- delete [] fileOwner;
- this->fileOwner=new char[strlen(f.fileOwner)];
- strcpy(this->fileOwner,f.fileOwner);
- return *this;
- }
- void print()
- {
- cout<<"File name: "<<fileName<<".";
- if(this->ext==0)
- {
- cout<<"pdf"<<endl;
- }
- else if(this->ext==1)
- {
- cout<<"txt"<<endl;
- }
- else
- {
- cout<<"exe"<<endl;
- }
- cout<<"File owner: "<<fileOwner<<endl;
- cout<<"File size: "<<fileSize<<endl;
- }
- bool equals(const File & that)
- {
- if(strcmp(this->fileName,that.fileName)==0 &&
- this->ext==that.ext &&
- strcmp(this->fileOwner,that.fileOwner)==0)
- {
- return true;
- }
- return false;
- }
- bool equalsType(const File & that)
- {
- if(strcmp(this->fileName,that.fileName)==0 && this->ext==that.ext)
- {
- return true;
- }
- return false;
- }
- };
- class Folder
- {
- private:
- char* name;
- int fileNumber;
- File* datoteki;
- public:
- Folder(const char* name)
- {
- this->fileNumber=0;
- this->name=new char[strlen(name)];
- strcpy(this->name,name);
- this->datoteki=new File[0];
- }
- Folder(const Folder &folder)
- {
- this->fileNumber=folder.fileNumber;
- this->name=new char[strlen(folder.name)];
- strcpy(this->name,folder.name);
- this->datoteki=new File[folder.fileNumber];
- for(int i=0;i<this->fileNumber;i++)
- {
- this->datoteki[i]=folder.datoteki[i];
- }
- }
- Folder& operator=(const Folder &folder)
- {
- if(this==&folder)
- {
- return *this;
- }
- this->fileNumber=folder.fileNumber;
- delete [] name;
- this->name=new char[strlen(folder.name)];
- strcpy(this->name,folder.name);
- delete [] datoteki;
- this->datoteki=new File[folder.fileNumber];
- for(int i=0;i<this->fileNumber;i++)
- {
- this->datoteki[i]=folder.datoteki[i];
- }
- return *this;
- }
- ~Folder()
- {
- delete [] name;
- delete [] datoteki;
- }
- void print()
- {
- cout<<"Folder name: "<<name<<endl;
- for(int i=0;i<fileNumber;i++)
- {
- datoteki[i].print();
- }
- }
- void remove(const File & file)
- {
- int count=fileNumber;
- int index,j=0;
- bool f=false;
- for(int i=0;i<fileNumber;i++)
- {
- if(datoteki[i].equals(file)==true)
- {
- count--;
- index=i;
- f=true;
- }
- }
- if(f)
- {
- File* temp=new File[fileNumber-1];
- for(int i=0;i<fileNumber;i++)
- {
- if(i!=index)
- {
- temp[j]=datoteki[i];
- j++;
- }
- else
- {
- continue;
- }
- }
- delete [] datoteki;
- datoteki=temp;
- fileNumber--;
- }
- }
- void add(const File & file)
- {
- File* temp=new File[fileNumber+1];
- for(int i=0;i<fileNumber;i++)
- {
- temp[i]=datoteki[i];
- }
- temp[fileNumber]=file;
- delete [] datoteki;
- datoteki=temp;
- fileNumber++;
- }
- };
- int main() {
- char fileName[20];
- char fileOwner[20];
- int ext;
- int fileSize;
- int testCase;
- cin >> testCase;
- if (testCase == 1) {
- cout << "======= FILE CONSTRUCTORS AND = OPERATOR =======" << endl;
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File created = File(fileName, fileOwner, fileSize, (Extension) ext);
- File copied = File(created);
- File assigned = created;
- cout << "======= CREATED =======" << endl;
- created.print();
- cout << endl;
- cout << "======= COPIED =======" << endl;
- copied.print();
- cout << endl;
- cout << "======= ASSIGNED =======" << endl;
- assigned.print();
- }
- else if (testCase == 2) {
- cout << "======= FILE EQUALS & EQUALS TYPE =======" << endl;
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File first(fileName, fileOwner, fileSize, (Extension) ext);
- first.print();
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File second(fileName, fileOwner, fileSize, (Extension) ext);
- second.print();
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File third(fileName, fileOwner, fileSize, (Extension) ext);
- third.print();
- bool equals = first.equals(second);
- cout << "FIRST EQUALS SECOND: ";
- if (equals)
- cout << "TRUE" << endl;
- else
- cout << "FALSE" << endl;
- equals = first.equals(third);
- cout << "FIRST EQUALS THIRD: ";
- if (equals)
- cout << "TRUE" << endl;
- else
- cout << "FALSE" << endl;
- bool equalsType = first.equalsType(second);
- cout << "FIRST EQUALS TYPE SECOND: ";
- if (equalsType)
- cout << "TRUE" << endl;
- else
- cout << "FALSE" << endl;
- equalsType = second.equals(third);
- cout << "SECOND EQUALS TYPE THIRD: ";
- if (equalsType)
- cout << "TRUE" << endl;
- else
- cout << "FALSE" << endl;
- }
- else if (testCase == 3) {
- cout << "======= FOLDER CONSTRUCTOR =======" << endl;
- cin >> fileName;
- Folder folder(fileName);
- folder.print();
- }
- else if (testCase == 4) {
- cout << "======= ADD FILE IN FOLDER =======" << endl;
- char name[20];
- cin >> name;
- Folder folder(name);
- int iter;
- cin >> iter;
- while (iter > 0) {
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File file(fileName, fileOwner, fileSize, (Extension) ext);
- folder.add(file);
- iter--;
- }
- folder.print();
- }
- else {
- cout << "======= REMOVE FILE FROM FOLDER =======" << endl;
- char name[20];
- cin >> name;
- Folder folder(name);
- int iter;
- cin >> iter;
- while (iter > 0) {
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File file(fileName, fileOwner, fileSize, (Extension) ext);
- folder.add(file);
- iter--;
- }
- cin >> fileName;
- cin >> fileOwner;
- cin >> fileSize;
- cin >> ext;
- File file(fileName, fileOwner, fileSize, (Extension) ext);
- folder.remove(file);
- folder.print();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement