Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sigachev week14 #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
71 changes: 71 additions & 0 deletions ps/week14/task1/sigachev/HW_14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "teacher.h"
#include "course.h"
#include "student.h"
#include <iostream>

using namespace std;

void toCerr(const char* str) {
cerr << str << endl;
}

int byRegistration(const void* lhs, const void* rhs) {
return static_cast<const Student*>(lhs)->Registration - static_cast<const Student*>(rhs)->Registration;
}

int main(int argc, char** argv) {

Course cppFundamentals;
cppFundamentals.Name = "C++ Fundamentals";

Course windowsConfiguration = { "Windows Configuration" };

Teacher boklazhenko = { "Alexandr", "Boklazhenko", 5000, 3 };
boklazhenko.Salary = 6500;
boklazhenko.Experience = 4;

Teacher* ivanov = new Teacher{ "Grigori", "Ianov", 7000, 5 };
cout << toString(*ivanov) << endl;

set(cppFundamentals, ivanov);

if (Error err = set(cppFundamentals, &boklazhenko);
err == Error::TeacherAlreadyRegistered) {
unsetTeacher(cppFundamentals);
err = set(cppFundamentals, &boklazhenko);

if (err != Error::No)
return 1;
}

set(windowsConfiguration, ivanov);

Student s1 = { "Nilolay", "Neradovsky", time(nullptr) };

Student* pS2 = createStudent("Anna", "Zorina");

Student* pS3 = createStudent("4uvak", "Levii");
cout << toString(pS3) << endl;
remove(cppFundamentals, *pS3);

add(cppFundamentals, s1);
add(cppFundamentals, *pS2);

print(cppFundamentals);

add(windowsConfiguration, s1);
add(windowsConfiguration, *pS2);

print(cppFundamentals, toCerr);
print(windowsConfiguration);

cerr << "students count: " << getStudentsCount(&cppFundamentals) << endl;

cerr << toString(pS3) << " is " << (isExist(cppFundamentals, *pS3) ? "" : "not") << "exist" << endl;

sortStudents(cppFundamentals, byRegistration);

cerr << toString(cppFundamentals) << endl;

return 0;
}
121 changes: 121 additions & 0 deletions ps/week14/task1/sigachev/course.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#define _CRT_SECURE_NO_WARNINGS
#include "course.h"
#include <iostream>
#include <string>

Error set(Course& course, Teacher* teacher)
{
if (course.pTeacher == nullptr)
{
course.pTeacher = teacher;
return No;
}
else
{
return TeacherAlreadyRegistered;
}
}

void unsetTeacher(Course& course)
{
course.pTeacher = nullptr;
}

void remove(Course& course, Student& student)
{
for (int i = 0; i < getStudentsCount(&course); ++i) {
if ((strcmp(course.Students[i]->Name, student.Name) == 0) &&
(strcmp(course.Students[i]->LastName, student.LastName) == 0) &&
course.Students[i]->Registration == student.Registration) {
course.Students[i] = nullptr;
}
}
}

void add(Course& course, Student& student)
{
Student** pStudents = new Student * [getStudentsCount(&course) + 1];
for (int i = 0; i < getStudentsCount(&course); ++i)
{
pStudents[i] = course.Students[i];
pStudents[course.studentsCount + 1] = &student;
course.Students = pStudents;
++course.studentsCount;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

т.е. при добавлении одного студента, если у нас в группе было 10, после завершения этой функции - станет 20, так как например эта операция выпоняется на каждой итерации цикла. выше операции также не нужно выполнять на каждой итерации

}
}

//void add(Course& course, Student* student)
//{
//}

void print(Course& course)
{
cout << "Course name: " << course.Name << endl;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

есть функционал по првращщению курса в строку, его и нужно тут использовать. toString

cout << "Course teacher: " << course.pTeacher->Name << " " << course.pTeacher->LastName << endl;

for (int i = 0; i < getStudentsCount(&course); ++i)
{
cout << "Student � " << i + 1 << " " << course.Students[i]->Name << " " << course.Students[i]->LastName << " " << course.Students[i]->Registration << endl;
}
}

void print(Course& course, void(*stream)(const char*))
{
stream(course.Name);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже самое, это дублирование кода

stream(toString(*course.pTeacher));

int studentCount = getStudentsCount(&course);

for (int i = 0; i < studentCount; ++i)
{
stream(course.Students[i]->Name);
stream(course.Students[i]->LastName);
cout << endl;
}

cout << "Count of students = " << studentCount << " peoples" << endl;
}


int getStudentsCount(Course* course)
{
return course->studentsCount;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

когда меняется эту переменная, например в методе ремув вроде бы мы удаляем студента и их количество должно измениться, он и там она не меняется

}

bool isExist(Course& course, Student& student)
{
for (int i = 0; i < getStudentsCount(&course); ++i)
{
if (course.Students[i]->Registration == student.Registration)
{
return true;
}
else
{
return false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

функция работает неверно, мы выйдем на первой итерации цикла в любом случае, не проверив всех

}
}
}

void sortStudents(Course& course, int(*byRegistration)(const void*, const void*))
{

int count = getStudentsCount(&course);
qsort(course.Students, count, sizeof(Student), (byRegistration));
}

char* toString(Course& course)
{
char* buff = new char[777];
strcpy(buff, course.Name);
char* pTeacher = toString(*course.pTeacher);
strcat(buff, pTeacher);
int size = getStudentsCount(&course);
char** ppStudents = new char* [size];
for (int i = 0; i < size; ++i)
{
ppStudents[i] = toString(course.Students[i]);
strcat(buff, ppStudents[i]);
}
return buff;
}
30 changes: 30 additions & 0 deletions ps/week14/task1/sigachev/course.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include "teacher.h"
#include "student.h"

using namespace std;

struct Course
{
const char* Name;
Teacher* pTeacher = nullptr;
Student** Students;// = new const Student * [0];
int studentsCount = 0;
};

enum Error
{
No,
TeacherAlreadyRegistered
};
Error set(Course& course, Teacher* teacher);
void unsetTeacher(Course& course);
void remove(Course& course, Student& student);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ссылка на студента должна быть константная

void add(Course& course, Student& student);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже

//void add(Course& course, Student* student);
void print(Course& course);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тоже и в нижних функция так же

void print(Course& course, void(*)(const char*));
int getStudentsCount(Course* course);
bool isExist(Course& course, Student& student);
void sortStudents(Course& course, int(*)(const void*, const void*));
char* toString(Course& course);
22 changes: 22 additions & 0 deletions ps/week14/task1/sigachev/student.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#define _CRT_SECURE_NO_WARNINGS
#include "student.h"
#include <string>

Student* createStudent(const char* name, const char* lastName)
{
return new Student{ name,lastName,static_cast<int>(time(nullptr)) };
}

char* toString(Student* student)
{
char* buff = new char[777];
sprintf(buff, "Student name: %s, Last name: %s, Registration: %d",
student->Name, student->LastName, &student->Registration);
return buff;
}
void print(Student* student, void(*stream)(char*))
{
char* buff = new char[255];
sprintf(buff, "%s %s", student->Name, student->LastName);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

дублирование кода. функция toString уже есть

stream(buff);
}
14 changes: 14 additions & 0 deletions ps/week14/task1/sigachev/student.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include <ctime>

using namespace std;

struct Student
{
const char* Name;
const char* LastName;
time_t Registration;
};
Student* createStudent(const char* name, const char* lastName);
char* toString(Student* student);
void print(Student* student, void(*stream)(char*));
11 changes: 11 additions & 0 deletions ps/week14/task1/sigachev/teacher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#define _CRT_SECURE_NO_WARNINGS
#include "teacher.h"
#include <string>

char* toString(const Teacher& const teacher)
{
char* buff = new char[777];
sprintf(buff, "Teacher name: %s, Last name: %s, Salary: %d, Experience: %d years",
teacher.Name, teacher.LastName, teacher.Salary, teacher.Experience);
return buff;
}
12 changes: 12 additions & 0 deletions ps/week14/task1/sigachev/teacher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

using namespace std;

struct Teacher
{
const char* Name;
const char* LastName;
int Salary;
int Experience;
};
char* toString(const Teacher& const teacher);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это не компилируется