C/C++

Создать структуры в с++

Требования и общие указания к заданиям:

2) задание выполнить с помощью динамических массивов
Для этого:
 в структуре объявить и создать динамический массив
 размерность этого массива включить в структуру в качестве одного из
полей
 создать динамический массив структур .
составить и использовать
функции, параметром которого является указатель на структуру или ссылка на
структуру.
ЗАДАЧА:
Сформировать массив структур, каждый элемент которого содержит
следующие поля:
шифр подразделения, содержащий не более трёх символов (вводим);
фамилия, имя отчество (вводим);
год рождения (вводим);
Для заданного подразделения, шифр которого вводим, рассортировать
сотрудников по возрасту
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
unsigned integer(const char* msg, istream& inp = std::cin) {
cout << msg;
unsigned value;
inp >> value;
inp.ignore(inp.rdbuf()->in_avail());
return value;
}
struct Employee {
int year;
char code[4];
string name;
bool is_code(char* code)const {
return! strcmp(code, this->code);
}
friend bool operator<(const Employee& a, const Employee& b) {
return a.year < b.year;
}
friend bool operator==(const Employee& a, const Employee& b) {
return a.year == b.year && a.name == b.name && !strcmp(a.code, b.code);
}
friend istream& operator>>(istream& inp, Employee& e) {
cout << "Код подразделения: ";
inp.getline(e.code, size(e.code));
cout << "Ф. И. О.: ";
getline(inp, e.name);
e.year = integer("Год рождения: ", inp);
return inp;
}
friend ostream& operator<<(ostream& out, const Employee& e) {
return out << e.code << ' ' << e.name << ' ' << e.year;
}
};
class EmployeeArray {
public:
static const size_t npos = -1;
EmployeeArray() = delete;
EmployeeArray(const size_t len) : len(len), box(new Employee[len]) {}
~EmployeeArray() {
if (box != nullptr) {
delete[] box;
box = nullptr;
}
}
EmployeeArray(const EmployeeArray& e) : len(e.len), box(new Employee[e.len]) {
for (auto i = 0U; i < len; ++i) {
box[i] = e.box[i];
}
}
EmployeeArray& operator=(const EmployeeArray& e) {
if (&e != this) {
if (this->box != nullptr) {
delete[] box;
}
this->len = e.len;
this->box = new Employee[len];
for (auto i = 0U; i < len; ++i) {
this->box[i] = e.box[i];
}
}
return *this;
}
Employee& operator[](int index) {
return box[index];
}
EmployeeArray code_query(char* code) {
auto n = code_count(code);
EmployeeArray acc(n);
auto i = 0U;
for (auto& x : *this) {
if (!strcmp(code, x.code)) {
acc[i++] = x;
}
}
return acc;
}
size_t code_count(char* code)const {
auto n = 0U;
for (auto i = 0U; i < len; ++i) {
if (box[i].is_code(code)) {
++n;
}
}
return n;
}
bool code_contains(char* code)const {
for (auto i = 0U; i < len; ++i) {
if (!strcmp(box[i].code, code)) {
return true;
}
}
return false;
}
size_t index_of(const Employee& e) {
for (auto i = 0U; i < len; ++i) {
if (box[i] == e) {
return i;
}
}
return npos;
}
Employee* begin() {
return box;
}
Employee* end() {
return box + len;
}
private:
size_t len;
Employee* box;
};
int main() {
system("chcp 1251 > nul");
auto n = integer("Введите размер массива: ");
EmployeeArray ea(n);
for (auto& x : ea) cin >> x;
cout << "\nВведите код подразделения: ";
char code[4];
cin.getline(code, size(code));
puts("");
if (ea.code_contains(code)) {
auto list = ea.code_query(code);
sort(list.begin(), list.end());
for (const auto& item : list) {
cout << item << '\n';
}
} else {
puts("Код подразделения не существует!");
}
system("pause > nul");
}
Александр Лоистый
Александр Лоистый
62 704
Лучший ответ