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

class Person {
Expand All @@ -11,12 +12,47 @@ class Person {
this->name = name;

}
string& getName() { return name; }
Person(string s, int g) {
name = s;
age = g;
}
Person() { name = " "; age = 0; };
string& GetName(const string& newName = "") {
if (!newName.empty()) {
name = newName;
}
return name;
}
friend ostream& operator<<(ostream& out, const Person& p);
const Person& operator=(const Person& other) {

name = other.name;
age = other.age;

return *this;
}
string getName() const { return name; }
void setName(string n) {
name = n;
}
bool operator <( const Person p) const {
if (strcmp(&name[0], &p.name[0]) > 0)
return 1;
return 0;
}
bool operator >(const Person p) const {
if (strcmp(&name[0], &p.name[0]) > 0)
return 0;
return 1;
}
bool operator ==(const Person p) const {
if (strcmp(&name[0], &p.name[0]) ==0)
return 0;
return 1;
}
private:
string name;
int age;

};

ostream& operator<<(ostream& out, const Person& p) {
Expand All @@ -31,7 +67,7 @@ class Group {

Group(int max_lenght) {
this->cap = max_lenght;
this->members = new Person[max_lenght];
this->members = new Person[max_lenght +1];
this->size = 0;
}
void add(Person p) {
Expand All @@ -44,82 +80,152 @@ class Group {
void deletePerson(string name) {
int i;
for (i = 0; i < size; i++) {
if(members[i].name == name)
if(members[i].getName() == name)
break;

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

~Group() {
delete[] members;
}

Group( const Group& g) {
this->members = new Person[g.cap + 1];
size = g.size;
cap = g.cap;
for (int i = 0; i < g.cap; i++) {
members[i] = g.members[i];
}
}
//~Group() {
//delete[] members;
//}
int getSize() { return size; }
int getCap() { return cap; }
void setCap(int f) { cap = f; }
void setSize(int f) { size = f; }
friend ostream& operator << (ostream& out, Group p);
Group operator +( Group g2)
{
Group all;
int i = 0;
all.setCap(cap+ g2.getCap());
all.setSize(size + g2.getSize());
all.members = new Person[all.getCap()];
for (i = 0; i < size; i++)
{
all.members[i] = members[i];
}
for (int j = 0; j < g2.getSize(); j++)
{
all.members[i] = members[j];
i++;
}
return all;



}
Group operator +=( Group g2)
{
Group all;
int i = 0;
all.size = size + g2.size;
all.setCap(cap + g2.cap);
all.members = new Person[all.getCap()];
for (i = 0; i < cap; i++)
{
all.members[i] =members[i];
}
for (int j = 0; j < g2.cap; j++)
{
all.members[i] =members[j];
i++;
}
*this = all;
return *this;



}
void operator =(const Group g) {
members = new Person[g.cap + 1];
for (int i = 0; i < g.cap; i++)
members[i] = g.members[i];
size = g.size;
cap = g.cap;
}
private:
int size;
int cap;
Person* members;

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


bool isGroupFull(Group g) {
return (g.size == g.cap);
if(g.getSize() == g.getCap())
return 0;
return 1;

}

int main()
{
/*

Person p1("somename");
Person p1("somename2");
cout << p1<<p2;
*/
/*
Person p2("somename2");
cout << p1<<p2<<endl;


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;
cout << g3<<endl;
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);
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);
*/







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

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

}

36 changes: 29 additions & 7 deletions 2/Score.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
#include "Score.h"
int Score::ID_generator = 0; // Initialize to 0 or any other suitable value
Score::Score(int score, string detail , Label l) : ID(Score::ID_generator) {
this->score = score;
// this->label = label; // Uncomment this line if needed
this->detail = detail;
label = l;
ID_generator++;
}

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

void Score::setID_generator(static int h)
{
ID_generator = h;
}
Score::Score():ID(Score::ID_generator)
{
ID_generator++;

}

void print(const Score& score) {
cout << score.detail << endl ;
}
Score::Score( const Score& s):ID(Score::ID_generator)
{
score = s.score;
label = s.label;
detail = s.detail;
}
void Score:: operator=(Score s)
{
score = s.score;
label = s.label;
detail = s.detail;
}

7 changes: 6 additions & 1 deletion 2/Score.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using namespace std;
class Score {
public:
friend void print(const Score& score);
friend int main();

typedef enum {
GREAT,
Expand All @@ -12,7 +13,11 @@ class Score {
BAD,
}Label;

Score(int score, char* detail, Label label);
Score(int score, string detail, Label label);
Score();
Score(const Score& s);
static void setID_generator(static int h);
void operator=(Score s);
private:
int score;
Label label;
Expand Down
10 changes: 4 additions & 6 deletions 2/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "Score.h"
//#include "Score.h"
#include "SomeRandomDotHFile.h"
using namespace std;
#define FOR(n) for(int i=0;i<n;i++)
Expand All @@ -13,20 +12,19 @@ scores[i] = Score( score,("some string " + to_string(i)).c_str(),Score::Label(3-

int main()
{

srand(time(NULL));

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

FOR(10) {
SETSCORE(i)
//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
Score::setID_generator(Score::ID_generator + 100);//just a setter for ID_generator

}