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
137 changes: 98 additions & 39 deletions 1/1.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@

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

class Person {
public:

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

}
string& getName() { return name; }

Person(const string n, const int a) { name = n; age = a; }
Person() {}
string& getName() { return name; }
const string& getName()const { return name; }
bool operator <(const Person p)
{
if (this->name < p.name)
return true;
else
return false;
}
bool operator ==(const Person p)
{
if (this->age == p.age)
return true;
else
return false;
}
friend class Group;
private:
string name;
int age;
Expand All @@ -25,15 +42,22 @@ 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(const Group& g)
{
this->cap = g.cap;
this->size = g.size;
this->members = new Person[this->cap];
for (int i = 0; i < g.size; i++)
this->members[i] = g.members[i];
}
void add(Person p) {
if (this->size == this->cap) {
return;
Expand All @@ -44,16 +68,50 @@ 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 +(const Group g)
{
Group result(this->cap + g.cap);
result.size = this->size + g.size;
for (int i = 0; i < this->size; i++)
result.members[i] = this->members[i];
for (int i = 0; i < g.size; i++)
result.members[i + this->size] = g.members[i];
return result;
}
Group operator +=(const Group g)
{
Group temp = *this;
this->cap += g.cap;
this->size += g.size;
delete[] this->members;
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 < g.size; i++)
this->members[i + temp.size] = g.members[i];
return *this;
}
Group operator =(const Group g)
{
this->cap = g.cap;
this->size = g.size;
delete[] this->members;
this->members = new Person[this->cap];
for (int i = 0; i < this->size; i++)
this->members[i] = g.members[i];
return *this;
}
friend bool isGroupFull(Group g);
friend ostream& operator<<(ostream& out, const Group& g);
~Group() {
delete[] members;
}
Expand All @@ -69,57 +127,58 @@ bool isGroupFull(Group g) {
return (g.size == g.cap);
}

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


int main()
{
/*

Person p1("somename");
Person p1("somename2");
cout << p1<<p2;
*/
/*
//had to change it from <p1> to <p2> to pervent redefinition
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);
}
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);
*/



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? :)

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

cout << *find(persons, persons + 10, Erfan);




}

32 changes: 32 additions & 0 deletions 2/Score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,37 @@ Score::Score(int score,char* detail, Label label) :ID(Score::ID_generator) {
ID_generator++;

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

ID_generator++;
}
Score::Score() : ID(-1)
{
this->score = 0;
this->label = BAD;
this->detail = "";
}
void Score::setID_generator(int input)
{
if (ID_generator < input)
ID_generator += input - ID_generator;
else
ID_generator -= ID_generator - input;
}
Score Score::operator =(const Score s)
{
this->score = s.score;
this->detail = s.detail;
this->label = s.label;
return *this;
}
void print(const Score& score)
{
cout << score.score << " ";
}
int Score::ID_generator = 0;

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

friend int main();
typedef enum {
GREAT,
GOOD,
Expand All @@ -13,6 +14,10 @@ class Score {
}Label;

Score(int score, char* detail, Label label);
Score(int score, const char* detail, Label label);
Score();
static void setID_generator(int input);
Score operator =(const Score s);
private:
int score;
Label label;
Expand Down
1 change: 1 addition & 0 deletions 2/SomeRandomDotHFile.h
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#pragma once
#include "Score.h"