Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ListContainer
- #include <iostream>
- using namespace std;
- class List{
- private:
- int *broevi;
- int brojBroevi;
- public:
- List()
- {
- brojBroevi=0;
- broevi=new int[0];
- }
- List(int *broevi,int brojBroevi)
- {
- this->brojBroevi=brojBroevi;
- this->broevi=new int[brojBroevi];
- for(int i=0;i<brojBroevi;i++)
- {
- this->broevi[i]=broevi[i];
- }
- }
- List(const List &l)
- {
- brojBroevi=l.brojBroevi;
- broevi=new int[brojBroevi];
- for(int i=0;i<brojBroevi;i++)
- {
- broevi[i]=l.broevi[i];
- }
- }
- List &operator=(const List &l)
- {
- if(this!=&l)
- {
- delete [] broevi;
- brojBroevi=l.brojBroevi;
- broevi=new int[brojBroevi];
- for(int i=0;i<brojBroevi;i++)
- {
- broevi[i]=l.broevi[i];
- }
- }
- return *this;
- }
- int sum()
- {
- int suma=0;
- for(int i=0;i<brojBroevi;i++)
- {
- suma+=broevi[i];
- }
- return suma;
- }
- double average()
- {
- return sum()*1.0/brojBroevi;
- }
- int brojElementiLista()
- {
- int br=0;
- for(int i=0;i<brojBroevi;i++)
- {
- br++;
- }
- return br;
- }
- void pecati()
- {
- cout<<brojBroevi<<": ";
- for(int i=0;i<brojBroevi;i++)
- {
- cout<<" "<<broevi[i];
- }
- cout<<" sum: "<<sum()<<" average: "<<average()<<endl;
- }
- ~List()
- {
- delete [] broevi;
- }
- };
- class ListContainer{
- private:
- List *listi;
- int brojListi;
- int brojObidi;
- public:
- ListContainer()
- {
- listi=new List[0];
- brojListi=0;
- brojObidi=0;
- }
- ListContainer(const ListContainer &lc)
- {
- brojListi=lc.brojListi;
- listi=new List[brojListi];
- for(int i=0;i<brojListi;i++)
- {
- listi[i]=lc.listi[i];
- }
- brojObidi=lc.brojObidi;
- }
- ListContainer &operator=(const ListContainer &lc)
- {
- if(this!=&lc)
- {
- delete [] listi;
- brojListi=lc.brojListi;
- listi=new List[brojListi];
- for(int i=0;i<brojListi;i++)
- {
- listi[i]=lc.listi[i];
- }
- brojObidi=lc.brojObidi;
- }
- return *this;
- }
- int sum()
- {
- int suma=0;
- for(int i=0;i<brojListi;i++)
- {
- suma+=listi[i].sum();
- }
- return suma;
- }
- double average()
- {
- int suma=0;
- for(int i=0;i<brojListi;i++)
- {
- suma+=listi[i].brojElementiLista();
- }
- return sum()*1.0/suma;
- }
- void print()
- {
- if(brojListi==0)
- {
- cout<<"The list is empty"<<endl;
- }
- for(int i=0;i<brojListi;i++)
- {
- cout<<"List number: "<<i+1<<" List info: ";
- listi[i].pecati();
- }
- cout<<"Sum: "<<sum()<<" Average: "<<average()<<endl;
- }
- void addNewList(List l)
- {
- int flag=0;
- for(int i=0;i<brojListi;i++)
- {
- if(listi[i].sum()==l.sum())
- {
- flag++;
- }
- }
- if(flag==0)
- {
- List *temp=new List[brojListi+1];
- for(int i=0;i<brojListi;i++)
- {
- temp[i]=listi[i];
- }
- temp[brojListi]=l;
- delete [] listi;
- brojListi++;
- listi=temp;
- }
- }
- ~ListContainer()
- {
- delete [] listi;
- }
- };
- int main()
- {
- ListContainer lc;
- int N;
- cin>>N;
- for (int i=0;i<N;i++) {
- int n;
- int niza[100];
- cin>>n;
- for (int j=0;j<n;j++){
- cin>>niza[j];
- }
- List l=List(niza,n);
- lc.addNewList(l);
- }
- int testCase;
- cin>>testCase;
- if (testCase==1) {
- cout<<"Test case for operator ="<<endl;
- ListContainer lc1;
- lc1.print();
- cout<<lc1.sum()<<" "<<lc.sum()<<endl;
- lc1=lc;
- lc1.print();
- cout<<lc1.sum()<<" "<<lc.sum()<<endl;
- lc1.sum();
- lc1.average();
- }
- else {
- lc.print();
- }
- return 0;
- }
- Datotecen sistem
- #include <iostream>
- #include <string.h>
- using namespace std;
- enum Extension{
- TXT,
- PDF,
- EXE
- };
- class File{
- private:
- char *ime;
- Extension tip;
- char *imeSopstvenik;
- int golemina;
- public:
- File()
- {
- ime=new char[0];
- imeSopstvenik=new char[0];
- }
- File(char *ime,char *imeSopstvenik,int golemina,Extension tip)
- {
- this->ime=new char[strlen(ime)+1];
- strcpy(this->ime,ime);
- this->imeSopstvenik=new char[strlen(imeSopstvenik)+1];
- strcpy(this->imeSopstvenik,imeSopstvenik);
- this->golemina=golemina;
- this->tip=tip;
- }
- File(const File &f)
- {
- ime=new char[strlen(f.ime)+1];
- strcpy(ime,f.ime);
- imeSopstvenik=new char[strlen(f.imeSopstvenik)+1];
- strcpy(imeSopstvenik,f.imeSopstvenik);
- golemina=f.golemina;
- tip=f.tip;
- }
- File &operator=(const File &f)
- {
- if(this!=&f)
- {
- delete [] ime;
- delete [] imeSopstvenik;
- ime=new char[strlen(f.ime)+1];
- strcpy(ime,f.ime);
- imeSopstvenik=new char[strlen(f.imeSopstvenik)+1];
- strcpy(imeSopstvenik,f.imeSopstvenik);
- golemina=f.golemina;
- tip=f.tip;
- }
- return *this;
- }
- void print()
- {
- cout<<"File name: "<<ime<<endl;
- cout<<"File owner: "<<imeSopstvenik<<endl;
- cout<<"File size: "<<golemina<<endl;
- }
- bool equals(const File &that)
- {
- if((strcmp(ime,that.ime))==0 && (tip=that.tip) && (strcmp(imeSopstvenik,that.imeSopstvenik))==0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- bool equalsType(const File &that)
- {
- if((strcmp(ime,that.ime)==0) && (tip==that.tip))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- ~File()
- {
- delete [] ime;
- delete [] imeSopstvenik;
- }
- };
- class Folder{
- private:
- char *imeFolder;
- int brojDatoteki;
- File *files;
- public:
- Folder()
- {
- brojDatoteki=0;
- files=new File[0];
- }
- Folder(char *imeFolder)
- {
- this->imeFolder=new char[strlen(imeFolder)+1];
- strcpy(this->imeFolder,imeFolder);
- brojDatoteki=0;
- files=new File[0];
- }
- void print()
- {
- for(int i=0;i<brojDatoteki;i++)
- {
- files[i].print();
- }
- }
- void remove(const File &file)
- {
- File *temp = new File[brojDatoteki-1];
- for(int i=0;i<brojDatoteki;i++)
- {
- temp[i]=files[i];
- }
- temp[brojDatoteki]=file;
- delete [] files;
- files=temp;
- }
- void add(const File &file)
- {
- File *temp = new File[brojDatoteki+1];
- for(int i=0;i<brojDatoteki;i++)
- {
- temp[i]=files[i];
- }
- temp[brojDatoteki]=file;
- delete [] files;
- files=temp;
- }
- ~Folder()
- {
- delete imeFolder;
- delete [] files;
- }
- };
- 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