14

 avatar
gorazd
c_cpp
a year ago
6.6 kB
12
Indexable
#include <iostream>
#include <cstring>
#include  <iomanip>
using namespace std;
enum Extension{DEFAULT = -1,pdf,txt,exe};


class File {

    char *name;
    Extension ext;
    char *owner;
    int MB;
public:
    File() {
        ext = DEFAULT;
        MB = 0;
        name = new char[1]{'\0'};
        owner = new char[1]{'\0'};
    }
    File(const char *_name,const char *_owner, int _MB,Extension ext) {
        this->ext = ext;
        MB = _MB;
        if (_name!=nullptr) {
            name = new char[strlen(_name)+1];
            strcpy(name,_name);
        }
        else
            name = nullptr;
        if (_owner!=nullptr) {
            owner = new char[strlen(_owner)+1];
            strcpy(owner,_owner);
        }
        else
            owner = nullptr;
    }
    File(const File &f) {
        MB = f.MB;
        ext = f.ext;
        if (f.name!=nullptr) {
            name = new char[strlen(f.name)+1];
            strcpy(name,f.name);
        }
        else
            name = nullptr;
        if (f.owner!=nullptr) {
            owner = new char[strlen(f.owner)+1];
            strcpy(owner, f.owner);
        }
        else
            owner = nullptr;
    }
    ~File() {
        delete[] name;
        delete[] owner;
    }
    File &operator=(const File &f) {
        MB = f.MB;
        ext = f.ext;
        if (f.name!=nullptr) {
            name = new char[strlen(f.name)+1];
            strcpy(name,f.name);
        }
        else
            name = nullptr;
        if (f.owner!=nullptr) {
            owner = new char[strlen(f.owner)+1];
            strcpy(owner, f.owner);
        }
        else
            owner = nullptr;
        return *this;
    }
    void print() {
		cout<<"File name: "<<name;
    	if (ext == 0) {
    		cout<<".pdf"<<endl;
    	}
    	else if (ext == 1)
    		cout<<".txt"<<endl;
    	else if (ext == 2)
    		cout<<".exe"<<endl;
    	cout<<"File owner: "<<owner<<endl;
    	cout<<"File size: "<<MB<<endl;
    }
    bool equals(const File &t) {
        return (strcmp(this->name, t.name)==0) and (this->ext == t.ext) and (strcmp(this->owner, t.owner)==0);
    }
    bool equalsType(const File &f) {
        return this->ext == f.ext;
    }
};

class Folder {
    char *name;
    int numberOfFiles;
    File *files;
public:
    Folder(const char *name) {
        numberOfFiles = 0;
        files = nullptr;
        if (name!=nullptr) {
            this->name = new char[strlen(name)+1];
            strcpy(this->name,name);
        }
        else
            this->name = nullptr;
    }
    ~Folder() {
        delete[] name;
        delete[] files;
    }
    void print() {
	    cout<<"Folder name: "<<name<<endl;
    	for (int i = 0; i<numberOfFiles; i++) {
    		files[i].print();
    	}
    }

    void remove(const File &f) {
        int index = 0;
        bool flag = false;
        for (int i = 0; i<numberOfFiles; i++) {
            if (files[i].equals(f)) {
                index = i;
                flag = true;
                break;
            }
        }
        if (flag and numberOfFiles>0) {
            File *temp = new File[numberOfFiles-1];
            for (int i = 0; i<index; i++) {
                temp[i] = files[i];
            }
            for (int i = index+1; i<numberOfFiles; i++) {
                temp[i-1] = files[i];
            }
            delete[] files;
            files = temp;
            numberOfFiles--;
        }
    }
    void add(const File &f) {
        File *temp = new File[numberOfFiles+1];
        for (int i = 0; i<numberOfFiles; i++) {
            temp[i] = files[i];
        }
        temp[numberOfFiles] = f;
        delete[] files;
        files = temp;
        numberOfFiles++;
    }
};


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;
}
Editor is loading...
Leave a Comment