Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 142 additions & 34 deletions 1/1.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

class Group;
class Person {
public:

Person(const string& name) {
this->name = name;

}
string& getName() { return name; }
Person(const string& name, int const _age) {
this->name = name;
this->age = _age;

}
Person() : name(" "), age(0) {}

string& getName() { return name; }

const string& getName() const { return name; }

void operator = (Person const p) {
this->name = p.name;
this->age = p.age;

}

bool operator < (Person const p) {
return ((this->name < p.name) ? true : false);
}

bool operator == (Person const p) {
return ((this->age == p.age && this->name == p.name) ? true : false);
}

private:
string name;
int age;

friend Group;

};

ostream& operator<<(ostream& out, const Person& p) {
Expand All @@ -29,11 +54,25 @@ ostream& operator<<(ostream& out, const Person& p) {
class Group {
public:

Group(int max_lenght) {
Group(int max_lenght) {
this->cap = max_lenght;
this->members = new Person[max_lenght];
this->size = 0;
}
Group() {
this->size = 0;
this->cap = 0;
this->members = NULL;
}
Group(const Group& g) {
this->size = g.size;
this->cap = g.cap;
this->members = new Person[this->cap];

for (int i = 0; i < this->size; i++) {
this->members[i] = g.members[i];
}
}
void add(Person p) {
if (this->size == this->cap) {
return;
Expand All @@ -44,16 +83,69 @@ class Group {
void deletePerson(string name) {
int i;
for (i = 0; i < size; i++) {
if(members[i].name == name)
if (members[i].name == name)
break;

}
for (int index = i; index < size - 1; index++) {
members[index] = members[index + 1];
}
size--;
}

Group& operator += (Group const p) {
Group tmp;

tmp.cap = this->cap + p.cap;
tmp.size = this->size + p.size;
tmp.members = new Person[tmp.cap];

for (int i = 0; i < this->size; i++) {
tmp.members[i] = this->members[i];
}

int i = this->size;
for (int j = 0; j < p.size; j++) {
tmp.members[i] = p.members[j];
i++;
}


*this = tmp;
return *this;
}

Group operator + (Group const p) {
Group tmp;

tmp.cap = this->cap + p.cap;
tmp.size = this->size + p.size;
tmp.members = new Person[tmp.cap];

for (int i = 0; i < this->size; i++) {
tmp.members[i] = this->members[i];
}
int i = this->size;
for (int j = 0; j < p.size; j++) {
tmp.members[i] = p.members[j];
i++;
}

return tmp;

}

void operator = (Group const g) {
this->size = g.size;
this->cap = g.cap;
this->members = new Person[cap];

for (int i = 0; i < this->size; i++) {
this->members[i] = g.members[i];
}

}

~Group() {
delete[] members;
}
Expand All @@ -63,56 +155,72 @@ class Group {
int cap;
Person* members;

friend bool isGroupFull(Group g);
friend ostream& operator<<(ostream& out, Group& p);

};

ostream& operator<<(ostream& out, Group& p) {
Person* ptmp = p.members;

for (int i = 0; i < p.size; i++) {
cout << p.members[i] << '\t';
}


return out;
}

bool isGroupFull(Group g) {
return (g.size == g.cap);
}

int main()
{
/*
Person p1("somename");
Person p1("somename2");
cout << p1<<p2;
*/
/*
Person p2("somename2");
cout << p1 << p2;


Group g1(5);
g1.add({ "Erfan",20 });
g1.add({ "Saba",20 });
g1.add({ "Mahrokh",20 });
g1.add({ "Yasin",21 });
cout<<isGroupFull(g1)<<endl;
*/
/*
cout << isGroupFull(g1) << endl;


Group g2(g1);
Group g3(10);
g2.deletePerson("Erfan");
cout << g1<<endl;//should cout every person in it with a \t as the separator
cout << g1 << endl;//should cout every person in it with a \t as the separator
cout << g2 << endl;
*/
/*


g3 = g1;
cout << g3;
g3 = g2 + g3;//adds members of g2 and g3 to each other
g3 = g2 + g3;//adds members of g2 and g3 to each other
g3 += g3 += g3 += g3;
*/
/*


Person persons[10];
Person Erfan("Erfan",20);
persons[1]=Erfan;
for(int i=0;i<10;i++){
persons[i].getName()=to_string(rand()%100);
Person Erfan("Erfan", 20);
persons[1] = Erfan;
for (int i = 0; i < 10; i++) {
persons[i].getName() = to_string(rand() % 100);
}
sort(persons, persons + 10);//what operator should be overloaded for sort? :)

for (int i = 0; i < 10; i++) {
cout << persons[i].getName() << ' ';
}
sort(persons,persons+10);//what operator should be overloaded for sort? :)

for(int i=0;i<10;i++){
cout<<persons[i].getName()<<' ';
}*/
/*
cout<<*find(persons,persons+10,Erfan);
*/

if ((find(persons, persons + 10, Erfan)) >= persons + 10)
cout << "There is no person with this name.";
else
cout << (*find(persons, persons + 10, Erfan)).getName();





Expand All @@ -121,5 +229,5 @@ int main()



}

}
16 changes: 15 additions & 1 deletion 2/Score.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Score.h"

Score::Score(int score,char* detail, Label label) :ID(Score::ID_generator) {
int Score :: ID_generator;

Score::Score(int score, const char* detail, Label label) :ID(Score::ID_generator) {
this->score = score;
this->label = label;
this->detail = detail;
Expand All @@ -9,4 +11,16 @@ Score::Score(int score,char* detail, Label label) :ID(Score::ID_generator) {

}

Score :: Score() : ID(ID_generator) , score(0) , label(GREAT) , detail("") {
ID_generator++;
}

void Score :: setID_generator(int _ID_generator) {
ID_generator = _ID_generator;
}

void :: Score :: operator = (const Score& _score){
this->score = _score.score;
this->label = _score.label;
this->detail = _score.detail;
}
4 changes: 4 additions & 0 deletions 2/Score.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <iostream>
#include <string>
using namespace std;
#pragma once
class Score {
public:
friend void print(const Score& score);
friend int main();

typedef enum {
GREAT,
Expand All @@ -13,6 +15,8 @@ class Score {
}Label;

Score(int score, char* detail, Label label);
Score();
static void setID_generator(int _ID_generator);
private:
int score;
Label label;
Expand Down
18 changes: 17 additions & 1 deletion 2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,28 @@
#include <vector>
#include <string>
#include <algorithm>
#include <time.h>
#include "Score.h"
#include "SomeRandomDotHFile.h"
using namespace std;
#define FOR(n) for(int i=0;i<n;i++)
#define SETSCORE(i) int score=rand()%20+1;/*no 0s*/\
scores[i] = Score( score,("some string " + to_string(i)).c_str(),Score::Label(3-score/5)) ;
scores[i] = Score( score,("some string " + to_string(i)).c_str(),Score::Label(3-score/5));

void print(const Score& score){
cout << "score : " << score.score << endl;
cout << "score label : ";
if(score.label==0)
cout << "GREAT" << endl;
else if(score.label==1)
cout << "GOOD" << endl;
else if(score.label==2)
cout << "NOTBAD" << endl;
else if(score.label==3)
cout << "BAD" << endl;
cout << "score detail : " << score.detail << endl;
cout << "score ID : " << score.ID << endl<<endl;
}


int main()
Expand Down