Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- enum Extension{
- pdf,txt,exe};
- using namespace std;
- class File{
- private:
- char *fileName;
- Extension ext;
- char *fileOwner;
- int fileSize;
- public:
- File(){}
- File(char *fileName,char *fileOwner,int fileSize,Extension ext)
- {
- this->fileName=new char[strlen(fileName)+1];
- strcpy(this->fileName,fileName);
- this->fileOwner=new char[strlen(fileOwner)+1];
- strcpy(this->fileOwner,fileOwner);
- this->ext=ext;
- this->fileSize=fileSize;
- }
- File(const File &f)
- {
- this->fileName=new char[strlen(f.fileName)+1];
- strcpy(this->fileName,f.fileName);
- this->fileOwner=new char[strlen(f.fileOwner)+1];
- strcpy(this->fileOwner,f.fileOwner);
- this->ext=f.ext;
- this->fileSize=f.fileSize;
- }
- File &operator=(const File &f)
- {
- if(this!=&f)
- {
- delete [] fileName;
- delete [] fileOwner;
- this->fileName=new char[strlen(f.fileName)+1];
- strcpy(this->fileName,f.fileName);
- this->fileOwner=new char[strlen(f.fileOwner)+1];
- strcpy(this->fileOwner,f.fileOwner);
- this->ext=f.ext;
- this->fileSize=f.fileSize;
- }
- return *this;
- }
- void print()
- {
- cout<<"File name: "<<fileName;
- if(this->ext==pdf)
- {
- cout<<".pdf"<<endl;
- }
- else if(this->ext==txt)
- {
- cout<<".txt"<<endl;
- }
- else if(this->ext==exe)
- {
- cout<<".exe"<<endl;
- }
- cout<<"File owner: "<<fileOwner<<endl;
- cout<<"File size: "<<fileSize<<endl;
- }
- bool equals(const File &that)
- {
- if((strcmp(fileName,that.fileName)==0)&&
- (strcmp(fileOwner,that.fileOwner)==0)&&
- (this->ext==that.ext))
- return true;
- else return false;
- }
- bool equalsType(const File &that)
- {
- if((strcmp(fileName,that.fileName)==0)&&(this->ext==that.ext))
- return true;
- else return false;
- }
- ~File(){ delete [] fileName; delete [] fileOwner;}
- };
- class Folder{
- private:
- char *name;
- int broj_Dat;
- File *files;
- public:
- Folder(){}
- Folder(const char *name)
- {
- this->name=new char[strlen(name)+1];
- strcpy(this->name,name);
- this->broj_Dat=0;
- this->files=new File[broj_Dat];
- for(int i=0;i<broj_Dat;i++)
- {
- this->files[i]=files[i];
- }
- }
- Folder(const Folder &f)
- {
- this->name=new char[strlen(f.name)+1];
- strcpy(this->name,f.name);
- this->broj_Dat=f.broj_Dat;
- this->files=new File[f.broj_Dat];
- for(int i=0;i<broj_Dat;i++)
- {
- this->files[i]=f.files[i];
- }
- }
- Folder &operator=(const Folder &f)
- {
- if(this!=&f)
- {
- delete [] name; delete [] files;
- this->name=new char[strlen(f.name)+1];
- strcpy(this->name,f.name);
- this->broj_Dat=f.broj_Dat;
- this->files=new File[f.broj_Dat];
- for(int i=0;i<broj_Dat;i++)
- {
- this->files[i]=f.files[i];
- }
- }
- return *this;
- }
- void print()
- {
- cout<<"Folder name: "<<name<<endl;
- //cout<<"======= "<<name<<" ======="<<endl;
- for(int i=0;i<broj_Dat;i++)
- {
- files[i].print();
- }
- }
- void remove(const File &file)
- {
- int newbroj=0;
- for(int i=0;i<broj_Dat;i++)
- {
- if(files[i].equals(file))
- newbroj++;
- }
- File *tmp = new File[newbroj];
- newbroj=0;
- for(int i=0;i<newbroj;i++)
- {
- if(!files[i].equals(file))
- {
- tmp[newbroj++]=files[i];
- }
- }
- delete [] files;
- files = tmp;
- broj_Dat=newbroj;
- }
- void add(const File &file)
- {
- File *tmp = new File[broj_Dat+1];
- for(int i=0;i<broj_Dat;i++)
- {
- tmp[i] = files[i];
- }
- tmp[broj_Dat++]= file;
- delete [] files;
- files=tmp;
- }
- };
- 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;
- }
Add Comment
Please, Sign In to add comment