Created
August 13, 2016 17:02
-
-
Save Soulstorm50/d3d96c237d87aec262ead5bfd38d79bc to your computer and use it in GitHub Desktop.
Group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include"Group.h" | |
Group::Group() | |
{ | |
group_size = 0; | |
group_name = nullptr; | |
group_spec = nullptr; | |
course_num = nullptr; | |
students = nullptr; | |
} | |
Group::Group(const char*group_name, const char*group_spec, const char*course_num) | |
{ | |
SetName(group_name); | |
SetSpec(group_spec); | |
SetNum(course_num); | |
students = nullptr; | |
} | |
Group::~Group() | |
{ | |
if (students) | |
delete[]students; | |
if (group_name) | |
delete[]group_name; | |
group_name= nullptr; | |
if (group_spec) | |
delete[]group_spec; | |
group_spec= nullptr; | |
if (course_num) | |
delete[]course_num; | |
course_num= nullptr; | |
}; | |
void Group::SetName(const char*group_name) | |
{ | |
if (this->group_name) | |
delete[]this->group_name; | |
this->group_name = new char[strlen(group_name) + 1]; | |
strcpy_s(this->group_name, strlen(group_name) + 1, group_name); | |
} | |
void Group::SetSpec(const char*group_spec) | |
{ | |
if (this->group_spec) | |
delete[]this->group_spec; | |
this->group_spec = new char[strlen(group_spec) + 1]; | |
strcpy_s(this->group_spec,strlen(group_spec) + 1, group_spec); | |
} | |
void Group::SetNum(const char*course_num) | |
{ | |
if (this->course_num) | |
delete[]this->course_num; | |
this->course_num = new char[strlen(course_num) + 1]; | |
strcpy_s(this->course_num, strlen(course_num) + 1, course_num); | |
} | |
Group::Group(ushort group_size) | |
{ | |
if (!group_size) | |
return; | |
SetName("No group name"); | |
SetSpec("Not group spec"); | |
SetNum("Not course number"); | |
//this->group_size = group_size; | |
Student *temp; | |
for (int i = 0; i < group_size; i++) | |
{ | |
temp = new Student("Not phone", "Not name", "Not secondname", "Not lastname"); | |
AddStudToGroup(*temp); | |
} | |
} | |
char*Group::Getter(char*str)const | |
{ | |
char*temp = new char[strlen(str)+1]; | |
strcpy_s(temp, strlen(str) + 1, str); | |
return temp; | |
} | |
char*Group::GetGroup_name()const | |
{ | |
return Getter(this->group_name); | |
} | |
char*Group::GetGroup_spec()const | |
{ | |
return Getter(this->group_spec); | |
} | |
char*Group::GetCourse_num()const | |
{ | |
return Getter(this->course_num); | |
} | |
void Group::AddStudToGroup(Student & student) | |
{ | |
Student**buf = new Student*[group_size + 1]; | |
if ((group_size + 1) < 30) | |
for (int i = 0; i < group_size; i++) | |
buf[i] = (this->students[i]); | |
buf[group_size] = &student; | |
if (this->students) | |
delete[]this->students; | |
this->students = buf; | |
group_size++; | |
} | |
Group::Group(Group & other) | |
{ | |
this->group_name = other.GetGroup_name(); | |
this->group_spec = other.GetGroup_spec(); | |
this->course_num = other.GetCourse_num(); | |
this->group_size =0 ; | |
for (int i = 0; i < other.group_size; i++) | |
AddStudToGroup(*other.students[i]); | |
} | |
char*Group::GroupList() | |
{ | |
SortStud(); | |
ushort capasity = 250; | |
char*temp = new char[capasity]; | |
strcpy_s(temp, strlen(GroupInfo()) + 1, GroupInfo()); | |
for (int i = 0; i < group_size; i++) | |
{ | |
if (capasity < strlen(temp) + 50) | |
capasity +=100; | |
char*a = new char[3]; | |
_itoa_s(i + 1, a, 3, 10); | |
strcat_s(temp, capasity, a); | |
strcat_s(temp, capasity, ")"); | |
strcat_s(temp, capasity, (*students[i]).FullName()); | |
strcat_s(temp, capasity, "\n"); | |
} | |
char*res = new char[strlen(temp) + 1]; | |
strcpy_s(res, strlen(temp) + 1, temp); | |
return res; | |
} | |
char*Group::GroupInfo() | |
{ | |
char*temp = new char[250]; | |
strcpy_s(temp, strlen(GetGroup_spec()) + 1, GetGroup_spec()); | |
strcat_s(temp, 250, ","); | |
strcat_s(temp, 250, GetGroup_name()); | |
strcat_s(temp, 250, "-"); | |
strcat_s(temp, 250, GetCourse_num()); | |
strcat_s(temp, 250, ":\n"); | |
char *res = new char[strlen(temp)+1]; | |
res = temp; | |
return res; | |
} | |
void Group::GroupUnion(Group &some) | |
{ | |
if (!some.group_size) | |
return; | |
if (this->group_size + some.group_size > 30) | |
return; | |
for (int i = 0; i < some.group_size; i++) | |
AddStudToGroup(*some.students[i]); | |
} | |
void Group::SortStud() | |
{ | |
if (group_size < 2) | |
return; | |
char * name1 = new char[255]; | |
char * name2 = new char[255]; | |
for (int i = 0; i < group_size-1; i++) | |
{ | |
name1 = students[i]->GetLastname(); | |
name2 = students[i + 1]->GetLastname(); | |
if (_stricmp(name1,name2) < 0) | |
{ | |
Student*temp = students[i]; | |
students[i] = students[i + 1]; | |
students[i + 1] = temp; | |
} | |
} | |
} | |
void Group::StudDelete(ushort stud_num) | |
{ | |
if (group_size < stud_num) | |
return; | |
Student**temp=new Student*[group_size-1]; | |
for (int i= 0; i<stud_num-1 ; i++) | |
{ | |
temp[i] = students[i]; | |
} | |
for (int j = stud_num-1; j < group_size; j++) | |
temp[j] = students[j + 1]; | |
group_size--; | |
delete[]students; | |
students = temp; | |
} | |
void Group::StudMove(Group &target,ushort stud_num) | |
{ | |
if (this->group_size < stud_num) | |
return; | |
target.AddStudToGroup (*this->students[stud_num - 1]); | |
StudDelete(stud_num); | |
} | |
Group& Group::operator=(Group&other) | |
{ | |
SetName(other.group_name); | |
SetSpec(other.group_spec); | |
SetNum(other.course_num); | |
this->group_size = other.group_size; | |
for (int i = 0; i < other.group_size; i++) | |
AddStudToGroup(*other.students[i]); | |
return *this; | |
} | |
char*Group:: operator[](ushort num) | |
{ | |
return (*students[num - 1]).FullName(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include<iostream> | |
#include"Student.h" | |
typedef unsigned short ushort; | |
using namespace std; | |
class Group | |
{ | |
Student**students; | |
ushort group_size; | |
char*group_name; | |
char*group_spec; | |
char*course_num; | |
char*Getter(char*str)const; | |
void SetSize(ushort group_size); | |
public: | |
void SetName(const char* group_name); | |
void SetSpec(const char*group_spec); | |
void SetNum(const char*course_num); | |
ushort GetSize() const{ return this->group_size; } | |
char*GetGroup_name()const; | |
char*GetGroup_spec()const; | |
char*GetCourse_num()const; | |
Group(); | |
Group(const char*group_name, const char*group_spec, const char*course_num); | |
Group(ushort group_size); | |
Group(Group & other); | |
~Group(); | |
void AddStudToGroup(Student & student); | |
char*GroupList(); | |
char*GroupInfo(); | |
void GroupUnion(Group &some); | |
void SortStud(); | |
void StudMove(Group &target, ushort stud_num); | |
void StudDelete(ushort stud_num); | |
Group& operator=(Group&other); | |
char* operator[](ushort num); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment