C/C++
Динамические массивы объектов класса. Нужна помощь.
Класс Сотрудник (табельный номер, ФИО, отдел, должность, кол-во отработанных часов, стоимость одного часа). Метод класса: вывести информацию, есть ли у сотрудника переработка (рабочий день – 8 часов). Функция: Вывести сотрудника, кто отработал меньше всех часов.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Employee {
Employee() : id(0), hours(0), h_rate(0) {}
string name;
string department;
string position;
unsigned id;
unsigned hours;
double h_rate;
bool above(float norm = 8) { return hours > norm; }
void create() {
cout << "Табельный номер: ";
cin >> id;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Ф. И. О.: ";
getline(cin, name);
cout << "Отдел: ";
getline(cin, department);
cout << "Должность: ";
getline(cin, position);
cout << "Количество отработанных часов: ";
cin >> hours;
cout << "Стоимость одного часа: ";
cin >> h_rate;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
friend bool operator<(const Employee& a, const Employee& b) {
return a.hours < b.hours;
}
};
class Staff {
public:
using iterator = Employee*;
using const_iterator = const Employee*;
using reference = Employee&;
using const_reference = const Employee&;
Staff() : pos_(-1), buffer_(min_buffer), box_(new Employee[buffer_]) {}
Staff(const size_t value) : pos_(value), buffer_(value), box_(new Employee[buffer_]) {}
Staff(const Staff& staff)
: pos_(staff.pos_), buffer_(staff.buffer_), box_(new Employee[buffer_]) {
for (const auto& item : staff) this->push_back(item);
}
reference operator[](int index) {
try {
if (index > pos_ || index < 0)
throw out_of_range("\aОшибка: попытка обратиться к несуществующему элементу\n");
} catch (const out_of_range& ex) {
cerr << ex.what() << '\n';
system("pause > nul");
}
return box_[index];
}
const_reference operator[](int index)const {
try {
if (index > pos_ || index < 0)
throw out_of_range("\aОшибка: попытка обратиться к несуществующему элементу\n");
} catch (const out_of_range& ex) {
cerr << ex.what() << '\n';
system("pause > nul");
}
return box_[index];
}
Staff& operator=(const Staff& staff) {
if (this != &staff) {
if (box_ != nullptr) delete[] box_;
pos_ = staff.pos_;
buffer_ = staff.buffer_;
box_ = new Employee[buffer_];
for (auto i = 0; i < pos_; ++i) box_[i] = staff.box_[i];
}
return *this;
}
~Staff() {
if (box_ != nullptr) {
delete[] box_;
box_ = nullptr;
}
}
void push_back(const Employee& emp) {
if (pos_ == buffer_) {
auto buffer = buffer_ << 1U;
Staff tmp(buffer);
tmp.clear();
for (auto i = 0U; i < buffer_; ++i) tmp.push_back(this->box_[i]);
*this = tmp;
}
this->box_[pos_] = emp;
++pos_;
}
void pop_back() {
if (!empty()) --pos_;
}
void clear() { pos_ = -1; }
bool empty()const { return pos_ == -1; }
iterator begin() { return box_; }
iterator end() { return box_ + pos_; }
const_iterator begin()const { return box_; }
const_iterator end()const { return box_ + pos_; }
private:
int pos_;
size_t buffer_;
Employee* box_;
static const auto min_buffer = 8U;
};
int main() {
system("chcp 1251 > nul");
cout << "Количество сотрудников: ";
size_t count;
cin >> count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Staff box(count);
for (auto& item : box) {
Employee emp;
emp.create();
item = emp;
cout.put('\n');
}
auto me = *min_element(box.begin(), box.end());
cout << me.name << " : " << me.hours << '\n';
system("pause > nul");
}
#include <string>
#include <algorithm>
using namespace std;
struct Employee {
Employee() : id(0), hours(0), h_rate(0) {}
string name;
string department;
string position;
unsigned id;
unsigned hours;
double h_rate;
bool above(float norm = 8) { return hours > norm; }
void create() {
cout << "Табельный номер: ";
cin >> id;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Ф. И. О.: ";
getline(cin, name);
cout << "Отдел: ";
getline(cin, department);
cout << "Должность: ";
getline(cin, position);
cout << "Количество отработанных часов: ";
cin >> hours;
cout << "Стоимость одного часа: ";
cin >> h_rate;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
friend bool operator<(const Employee& a, const Employee& b) {
return a.hours < b.hours;
}
};
class Staff {
public:
using iterator = Employee*;
using const_iterator = const Employee*;
using reference = Employee&;
using const_reference = const Employee&;
Staff() : pos_(-1), buffer_(min_buffer), box_(new Employee[buffer_]) {}
Staff(const size_t value) : pos_(value), buffer_(value), box_(new Employee[buffer_]) {}
Staff(const Staff& staff)
: pos_(staff.pos_), buffer_(staff.buffer_), box_(new Employee[buffer_]) {
for (const auto& item : staff) this->push_back(item);
}
reference operator[](int index) {
try {
if (index > pos_ || index < 0)
throw out_of_range("\aОшибка: попытка обратиться к несуществующему элементу\n");
} catch (const out_of_range& ex) {
cerr << ex.what() << '\n';
system("pause > nul");
}
return box_[index];
}
const_reference operator[](int index)const {
try {
if (index > pos_ || index < 0)
throw out_of_range("\aОшибка: попытка обратиться к несуществующему элементу\n");
} catch (const out_of_range& ex) {
cerr << ex.what() << '\n';
system("pause > nul");
}
return box_[index];
}
Staff& operator=(const Staff& staff) {
if (this != &staff) {
if (box_ != nullptr) delete[] box_;
pos_ = staff.pos_;
buffer_ = staff.buffer_;
box_ = new Employee[buffer_];
for (auto i = 0; i < pos_; ++i) box_[i] = staff.box_[i];
}
return *this;
}
~Staff() {
if (box_ != nullptr) {
delete[] box_;
box_ = nullptr;
}
}
void push_back(const Employee& emp) {
if (pos_ == buffer_) {
auto buffer = buffer_ << 1U;
Staff tmp(buffer);
tmp.clear();
for (auto i = 0U; i < buffer_; ++i) tmp.push_back(this->box_[i]);
*this = tmp;
}
this->box_[pos_] = emp;
++pos_;
}
void pop_back() {
if (!empty()) --pos_;
}
void clear() { pos_ = -1; }
bool empty()const { return pos_ == -1; }
iterator begin() { return box_; }
iterator end() { return box_ + pos_; }
const_iterator begin()const { return box_; }
const_iterator end()const { return box_ + pos_; }
private:
int pos_;
size_t buffer_;
Employee* box_;
static const auto min_buffer = 8U;
};
int main() {
system("chcp 1251 > nul");
cout << "Количество сотрудников: ";
size_t count;
cin >> count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Staff box(count);
for (auto& item : box) {
Employee emp;
emp.create();
item = emp;
cout.put('\n');
}
auto me = *min_element(box.begin(), box.end());
cout << me.name << " : " << me.hours << '\n';
system("pause > nul");
}
Похожие вопросы
- Устройство статических и динамических массивов в оперативной памяти (...)
- Двумерный динамический массив с неизвестны количеством столбиков или строк
- Почему динамический массив напрямую в функцию передать нельзя, а динамический массив указателей можно?
- Доработка класса. Создание массива объектов
- Динамический массив и указатель Си
- Нужна помощь в составлении одномерных массивов С++
- Задача по С++ ДИНАМИЧЕСКИЙ МАССИВ
- C++ динамический массив
- Программирование на C++ с использованием динамического массива
- Откуда взялся мусор в динамическом массиве char?