This commit is contained in:
Alexander Zhirov 2021-10-15 03:42:52 +03:00
parent 567b8b15d0
commit 881a266d2e
5 changed files with 222 additions and 38 deletions

BIN
lesson_7/data.bin Normal file

Binary file not shown.

53
lesson_7/exercise_2.hpp Normal file
View File

@ -0,0 +1,53 @@
/*
* exercise_2.hpp
*
* Created on: 15 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <numeric>
#include <fstream>
#include "src-gen/exercise_2.pb.h"
void exercise_1_2()
{
exercise_2::FullName fn;
fn.set_name("Alexander");
fn.set_surname("Zhirov");
fn.set_patronymic("Alexandrovich");
exercise_2::Student s;
*s.mutable_name() = fn;
s.add_grades(5);
s.add_grades(2);
s.add_grades(4);
s.add_grades(3);
s.add_grades(4);
s.add_grades(3);
s.add_grades(4);
s.add_grades(3);
s.set_avg_score(std::accumulate(s.grades().begin(), s.grades().end(), 0) / s.grades().size());
exercise_2::StudentsGroup sg;
*sg.add_students() = s;
std::ofstream out("student.bin", std::ios_base::binary);
sg.SerializeToOstream(&out);
out.close();
exercise_2::StudentsGroup new_sg;
std::ifstream in("student.bin", std::ios_base::binary);
if (new_sg.ParseFromIstream(&in))
{
std::cout << new_sg.students(0).name().surname() << std::endl;
std::cout << new_sg.students(0).avg_score() << std::endl;
}
else
{
std::cout << "Error!" << std::endl;
}
in.close();
}

38
lesson_7/exercise_3.cpp Normal file
View File

@ -0,0 +1,38 @@
/*
* exercise_3.cpp
*
* Created on: 15 окт. 2021 г.
* Author: alexander
*/
#include "exercise_3.hpp"
#include <numeric>
void exercise_3()
{
exercise_2::FullName fn;
fn.set_name("Alexander");
fn.set_surname("Zhirov");
fn.set_patronymic("Alexandrovich");
exercise_2::Student s;
*s.mutable_name() = fn;
s.add_grades(5);
s.add_grades(2);
s.add_grades(4);
s.add_grades(3);
s.add_grades(4);
s.add_grades(3);
s.add_grades(4);
s.add_grades(3);
s.set_avg_score(std::accumulate(s.grades().begin(), s.grades().end(), 0) / s.grades().size());
SG::StudentsGroup sg;
sg.add_student(s);
sg.Save();
SG::StudentsGroup new_sg;
new_sg.Open();
std:: cout << new_sg.GetAllInfo(fn) << std::endl;
}

127
lesson_7/exercise_3.hpp Normal file
View File

@ -0,0 +1,127 @@
/*
* exercise_3.hpp
*
* Created on: 15 окт. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include "src-gen/exercise_2.pb.h"
namespace SG
{
class IRepository
{
public:
virtual void Open() = 0; // бинарная десериализация в файл
virtual void Save() = 0; // бинарная сериализация в файл
virtual ~IRepository() {};
};
class IMethods
{
public:
virtual double GetAverageScore(const exercise_2::FullName &name) = 0;
virtual std::string GetAllInfo(const exercise_2::FullName &name) = 0;
virtual std::string GetAllInfo() = 0;
virtual ~IMethods() {};
};
class StudentsGroup: public IRepository, public IMethods
{
private:
std::vector<exercise_2::Student> m_students;
public:
void add_student(const exercise_2::Student &s)
{
m_students.push_back(s);
}
double GetAverageScore(const exercise_2::FullName &name)
{
auto it = std::find_if(m_students.begin(), m_students.end(), [&](const exercise_2::Student &s)
{
return tie(s.name().name(), s.name().surname(), s.name().patronymic()) ==
tie(name.name(), name.surname(), name.patronymic());
});
if (it == m_students.end())
{
return 0.0;
}
return it->avg_score();
}
std::string GetAllInfo(const exercise_2::FullName &name)
{
auto it = std::find_if(m_students.begin(), m_students.end(), [&](const exercise_2::Student &s)
{
return tie(s.name().name(), s.name().surname(), s.name().patronymic()) ==
tie(name.name(), name.surname(), name.patronymic());
});
if (it == m_students.end())
{
return {};
}
std::string temp = "Имя: " + it->name().name() + ' ' + it->name().surname() + ' ' + it->name().patronymic() + '\n';
temp += "Оценки: ";
for (int i = 0; i < it->grades().size(); ++i)
{
temp += std::to_string(it->grades(i)) + "; ";
}
temp += '\n';
temp += "Средний балл: " + std::to_string(it->avg_score()) + "\n\n";
return temp;
}
std::string GetAllInfo()
{
std::string temp;
std::for_each(m_students.begin(), m_students.end(), [&](const exercise_2::Student &s)
{
temp += GetAllInfo(s.name());
});
return temp;
}
void Save()
{
std::ofstream out("data.bin", std::ios_base::binary);
auto size = m_students.size();
out.write(reinterpret_cast<char*>(&size), sizeof(size));
std::for_each(m_students.begin(), m_students.end(), [&](const exercise_2::Student &s)
{
s.SerializeToOstream(&out);
});
out.close();
}
void Open()
{
std::ifstream in("data.bin", std::ios_base::binary);
size_t size = 0;
in.read(reinterpret_cast<char*>(&size), sizeof(size));
while (size--)
{
exercise_2::Student s;
s.ParseFromIstream(&in);
m_students.push_back(std::move(s));
}
in.close();
}
virtual ~StudentsGroup() {};
};
}
void exercise_3();

View File

@ -5,47 +5,13 @@
* Author: alexander * Author: alexander
*/ */
#include <iostream> #include "exercise_2.hpp"
#include <numeric> #include "exercise_3.hpp"
#include <fstream>
#include "src-gen/exercise_2.pb.h"
int main() int main()
{ {
// exercise_2::FullName fn; exercise_1_2();
// fn.set_name("Alexander"); exercise_3();
// fn.set_surname("Zhirov");
// fn.set_patronymic("Alexandrovich");
//
// exercise_2::Student s;
// s.set_allocated_name(&fn);
// s.add_grades(5);
// s.add_grades(2);
// s.add_grades(4);
// s.add_grades(3);
// s.add_grades(4);
// s.add_grades(3);
// s.add_grades(4);
// s.add_grades(3);
// s.set_avg_score(std::accumulate(s.grades().begin(), s.grades().end(), 0) / s.grades().size());
//
// exercise_2::StudentsGroup sg;
// *sg.add_students() = s;
//
// std::ofstream out("student.bin", std::ios_base::binary);
// sg.SerializeToOstream(&out);
exercise_2::StudentsGroup sg;
std::ifstream in("student.bin", std::ios_base::binary);
if (sg.ParseFromIstream(&in))
{
std::cout << sg.students(0).name().surname() << std::endl;
std::cout << sg.students(0).avg_score() << std::endl;
}
else
{
std::cout << "Error!" << std::endl;
}
return 0; return 0;
} }