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* ime;
- Extension ext;
- char* imeOwner;
- int golemina; //MB
- public:
- File() {
- ime = NULL;
- imeOwner = NULL;
- ext = (Extension)0;
- golemina = 0;
- }
- File(char* ime, char* imeOwner, int golemina, Extension ext) {
- this->ime = new char[strlen(ime) + 1];
- strcpy(this->ime, ime);
- this->imeOwner = new char[strlen(imeOwner) + 1];
- strcpy(this->imeOwner, imeOwner);
- this->golemina = golemina;
- this->ext = ext;
- }
- File(const File& f) {
- this->ime = new char[strlen(f.ime) + 1];
- strcpy(this->ime, f.ime);
- this->imeOwner = new char[strlen(f.imeOwner) + 1];
- strcpy(this->imeOwner, f.imeOwner);
- this->golemina = f.golemina;
- this->ext = f.ext;
- }
- ~File() {
- delete[]ime;
- delete[]imeOwner;
- }
- File& operator=(const File& f) {
- if (this != &f) {
- delete[]ime;
- delete[]imeOwner;
- this->ime = new char[strlen(f.ime) + 1];
- strcpy(this->ime, f.ime);
- this->imeOwner = new char[strlen(f.imeOwner) + 1];
- strcpy(this->imeOwner, f.imeOwner);
- this->golemina = f.golemina;
- this->ext = f.ext;
- }
- return *this;
- }
- void print() {
- cout << "File name: " << ime << ".";
- if (ext == 0)
- cout << "pdf" << endl;
- else if (ext == 1)
- cout << "txt" << endl;
- else
- cout<<"exe"<<endl;
- cout << "File owner: " << imeOwner << endl;
- cout << "File size: " << golemina << endl;
- }
- bool equals(const File& that) {
- return((strcmp(ime, that.ime) == 0)&&ext == that.ext&&(strcmp(imeOwner, that.imeOwner) == 0));
- }
- bool equalsType(const File& that) {
- return((strcmp(ime, that.ime) == 0)&&ext == that.ext);
- }
- };
- class Folder {
- private:
- char* ime;
- int brDatoteki;
- File* datoteki;
- public:
- Folder() {
- ime = NULL;
- brDatoteki = 0;
- datoteki = NULL;
- }
- Folder(const char* ime) {
- this->ime = new char[strlen(ime) + 1];
- strcpy(this->ime, ime);
- this->brDatoteki = 0;
- this->datoteki = new File[brDatoteki];
- for (int i = 0; i < brDatoteki; i++) {
- this->datoteki[i] = datoteki[i];
- }
- }
- Folder(const Folder& f) {
- this->ime = new char[strlen(f.ime) + 1];
- strcpy(this->ime, f.ime);
- this->brDatoteki = f.brDatoteki;
- this->datoteki = new File [brDatoteki];
- for (int i = 0; i < brDatoteki; i++) {
- this->datoteki[i] = f.datoteki[i];
- }
- }
- Folder& operator=(const Folder& f) {
- if (this != &f) {
- delete[]ime;
- delete[]datoteki;
- this->ime = new char[strlen(f.ime) + 1];
- strcpy(this->ime, f.ime);
- this->brDatoteki = f.brDatoteki;
- this->datoteki = new File [brDatoteki];
- for (int i = 0; i < brDatoteki; i++) {
- this->datoteki[i] = f.datoteki[i];
- }
- }
- return *this;
- }
- ~Folder() {
- delete[]ime;
- delete[]datoteki;
- }
- void remove(const File& file) {
- int index = 0;
- for (int i = 0; i < brDatoteki; i++) {
- if (datoteki[i].equals(file)) {
- index = i;
- break;
- }
- }
- File* tmp = new File[brDatoteki - 1];
- for (int i = 0; i < brDatoteki; i++) {
- if (i == index) {
- continue;
- }
- tmp[i] = datoteki[i];
- }
- delete[]datoteki;
- datoteki = tmp;
- --brDatoteki;
- }
- void add(const File& file) {
- File* tmp = new File[brDatoteki + 1];
- for (int i = 0; i < brDatoteki; i++) {
- tmp[i] = datoteki[i];
- }
- tmp[brDatoteki++] = file;
- delete[]datoteki;
- datoteki = tmp;
- }
- void print() {
- cout << "Folder name: " << ime << endl;
- for (int i = 0; i < brDatoteki; i++) {
- datoteki[i].print();
- }
- }
- };
- 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