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
163 changes: 126 additions & 37 deletions 1/1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,62 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

class Person {
public:

Person(const string& _name , int _age ) : name(_name) , age(_age) { //me
}
Person(const string& name) {
this->name = name;

}
Person() { //me
name = " ";
age = 0;
}
string& getName() { return name; }

string getName()const { return name; } //me
int getAge()const { return age; } //me
bool operator < (const Person& other) const{ //me
return this->name < other.name;
}
bool operator ==(const Person& other) const { //me
return this->name == other.name && this->age == other.age;
}

friend class Group;
private:
string name;
int age;

};

ostream& operator<<(ostream& out, const Person& p) {


/*ostream& operator<< (ostream& out, const Person* p) {
if (p == NULL) {
cout << "can not find\n";
return out;
}
cout << p->getName();
return out;
}*/
ostream& operator<<(ostream& out, const Person& p) { //check
cout << p.getName();
return out;
}
/*Person* find(Person* start, Person* end, const Person& goal) { //me
cout << "here";
for (Person* i = start; i <= end; ++i) {
if (i->getName() == goal.getName() && i->getAge() == goal.getAge()) {
cout << "Hey";
return i;
}
}
cout << "can not find\n";
return end;
}*/



Expand All @@ -31,9 +66,17 @@ class Group {

Group(int max_lenght) {
this->cap = max_lenght;
this->members = new Person[max_lenght];
this->members = new Person[max_lenght]; //check
this->size = 0;
}
Group(Group& obj) {
this->cap = obj.cap;
this->size = obj.size;
this->members = new Person[this->cap];
for (int i = 0; i < this->size; ++i) {
this->members[i] = obj.members[i];
}
}
void add(Person p) {
if (this->size == this->cap) {
return;
Expand All @@ -46,80 +89,126 @@ class Group {
for (i = 0; i < size; i++) {
if(members[i].name == name)
break;

}
for (int index = i; index < size - 1; index++) {
members[index] = members[index + 1];
}
size--;
}
Group& operator=(const Group& obj) { //me
this->size = obj.size;
this->cap = obj.cap;
this->members = new Person[this->cap];
for (int i = 0; i < this->size; ++i) {
this->members[i] = obj.members[i];
}
return *this;
}
Group operator+(const Group &obj) { //me
Group sum(this->cap + obj.cap);
sum.size = this->size + obj.size;
sum.cap = this->cap + obj.cap;
for (int i = 0; i < this->size ; ++i) {
sum.members[i] = this->members[i];
}
for (int i = 0; i < obj.size; ++i) {
sum.members[this->size + i] = obj.members[i];
}
return sum;
}
Group& operator+=(const Group& obj) { //me
Group temp(this->cap);
temp.size = this->size;
int pirmary_size = obj.size;
for (int i = 0; i < this->size; ++i) {
temp.members[i] = this->members[i];
}
delete[] this->members;
this->size = temp.size + obj.size;
this->cap = this->cap + obj.cap;
this->members = new Person[this->cap];
for (int i = 0; i < temp.size ; ++i) {
this->members[i] = temp.members[i];
}
for (int i = 0; i < pirmary_size; ++i) {
this->members[temp.size + i] = obj.members[i];
}
return *this;
}

friend bool isGroupFull(Group ); //me
friend ostream& operator<<(ostream& ,const Group& ); //me

~Group() {
delete[] members;
}

private:
int size;
int cap;
Person* members;

};
ostream& operator<<(ostream& out ,const Group& obj) { //me
for (int i = 0; i < obj.size ; ++i) {
out << obj.members[i] << "\t";
}
cout << endl;
return out;
}

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

int main()
{
/*

Person p1("somename");
Person p1("somename2");
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;
*/
/*


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 << g2 << endl;
*/
/*
g3 = g1;
cout << g3;
g3 = g2 + g3;//adds members of g2 and g3 to each other
cout << g2 << endl ;


g3 = g1 ;
cout << g3 ;
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);
}
sort(persons,persons+10);//what operator should be overloaded for sort? :)
sort(persons, persons + 10);//what operator should be overloaded for sort? :)

for(int i=0;i<10;i++){
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)
{
return 0;
}
else
cout << *find(persons, persons + 10, Erfan);









}

28 changes: 28 additions & 0 deletions 2/Score.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
#include "Score.h"
int Score:: ID_generator = 0;
void Score::setID_generator(int input)
{
ID_generator = input;
}

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

ID_generator++;
}
Score::Score(int _score, const char* _detail, Label _label) : ID(Score::ID_generator) {
this->score = _score;
this->label = _label;
this->detail = _detail;

ID_generator++;
}

Score::Score() : ID(Score::ID_generator) {
this->score =0;
this->detail = "";
this->label = GREAT;
ID_generator++;
}

Score& Score::operator=(const Score& obj)
{
this->score = obj.score;
this->detail = obj.detail;
this->label = obj.label;
return *this;
}




9 changes: 7 additions & 2 deletions 2/Score.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Score {
public:
friend void print(const Score& score);

friend void print( const Score& score); //check
friend int main();
static void setID_generator(int );
typedef enum {
GREAT,
GOOD,
Expand All @@ -13,6 +15,9 @@ class Score {
}Label;

Score(int score, char* detail, Label label);
Score(int, const char* , Label); //me
Score();//me
Score& operator=(const Score& obj);
private:
int score;
Label label;
Expand Down
6 changes: 5 additions & 1 deletion 2/SomeRandomDotHFile.h
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
#include "Score.h"
#pragma once
#include "Score.h"
void print(const Score& obj) {
cout << "score : " << obj.score << " Label : " << obj.label << " detail : " << obj.detail << " ID : " << obj.ID << endl;
}
7 changes: 4 additions & 3 deletions 2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ int main()

srand(time(NULL));

Score s1(1, "do better next time", Score::BAD);
Score scores[10];//we have default constructor so we should be fine right?:)
Score s1(1, "do better next time", Score::BAD); //check
Score scores[10];//we have default constructor so we should be fine right?:) //check

FOR(10) {
SETSCORE(i)
SETSCORE(i); //check
//why = betwean two scores raises an error?:)
}
FOR(10) {
print(scores[i]);
}

Score::setID_generator(Score::ID_generator+100);//just a setter for ID_generator

}
Expand Down