Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "employees.h"
- #include <cstring>
- #include <iostream>
- #include <fstream>
- using std::string;
- #define BITS_PER_BYTE 8
- int Developer::salary() const {
- int salary = _base_salary;
- if (_has_bonus) { salary += 1000; }
- return salary;
- }
- int SalesManager::salary() const {
- return _base_salary + _sold_nm * _price * 0.01;
- }
- EmployeesArray::~EmployeesArray() {
- for (auto i : _employees) {
- delete i;
- }
- }
- void EmployeesArray::add(Employee *e) {
- _employees.push_back(e);
- }
- int EmployeesArray::total_salary() const {
- int total = 0;
- for (int i = 0; i < _employees.size(); i++) {
- total += _employees[i]->salary();
- }
- return total;
- }
- void Developer::print(std::ostream &os) const {
- os << "Developer" << '\n' << "Name: " << name << '\n' << "Base Salary: " << _base_salary << '\n' << "Has bonus: ";
- if (_has_bonus) os << "+\n";
- if (!_has_bonus) os << "-\n";
- }
- void SalesManager::print(std::ostream &os) const {
- os << "SalesManager" << '\n' << "Name: " << name << '\n' << "Base Salary: " << _base_salary << '\n'
- << "Sold items: "
- << _sold_nm << '\n' << "Item price: " << _price << '\n';
- }
- void Developer::scan(std::istream &is) {
- is >> name >> _base_salary >> _has_bonus;
- }
- void SalesManager::scan(std::istream &is) {
- is >> name >> _base_salary >> _sold_nm >> _price;
- }
- std::ostream &operator<<(std::ostream &os, const Employee &p) {
- p.print(os);
- return os;
- }
- std::istream &operator>>(std::istream &is, Employee &p) {
- p.scan(is);
- return is;
- }
- void print_le_int32_t(std::ofstream &out, int32_t n) {
- for (size_t i = 0; i < sizeof(int32_t); i++) {
- unsigned char c = (n >> (i * BITS_PER_BYTE)) & 0xFF;
- out.write(reinterpret_cast<const char *>(&c), sizeof(c));
- }
- }
- void print_common_fields(std::ofstream &out, string name, int32_t base_salary) {
- for (int i = 0; i < name.length(); i++) out.write(&(name[i]), sizeof(char));
- char nul = '\0';
- out.write(&nul, sizeof(nul));
- print_le_int32_t(out, base_salary);
- }
- void Developer::print_bytes(std::ofstream &out) const {
- int32_t type = 1;
- print_le_int32_t(out, type);
- print_common_fields(out, name, _base_salary);
- char bonus = _has_bonus;
- out.write(&bonus, sizeof(char));
- }
- void SalesManager::print_bytes(std::ofstream &out) const {
- int32_t type = 2;
- print_le_int32_t(out, type);
- print_common_fields(out, name, _base_salary);
- print_le_int32_t(out, _sold_nm);
- print_le_int32_t(out, _price);
- }
- string read_name(std::ifstream &in) {
- string name;
- char c;
- while (in.read(&c, sizeof(c))) {
- if (c == '\0') {
- break;
- }
- name.push_back(c);
- }
- return name;
- }
- int32_t read_int32_t(std::ifstream &in) {
- uint8_t c;
- int32_t n = 0;
- for (size_t i = 0; i < sizeof(int32_t); i++) {
- if (in.read(reinterpret_cast<char *>(&c), sizeof(c))) {
- n += c << (i * BITS_PER_BYTE);
- }
- }
- return n;
- }
- int Developer::scan_bytes(std::ifstream &in) {
- string Name = read_name(in);
- if ((in.rdstate() & std::ifstream::eofbit) != 0) {
- in.setstate(std::ios::badbit);
- return -1;
- }
- name = Name;
- _base_salary = read_int32_t(in);
- char bonus;
- if (in.read(&bonus, sizeof(char))) {
- _has_bonus = bonus;
- }
- return 0;
- }
- int SalesManager::scan_bytes(std::ifstream &in) {
- string Name = read_name(in);
- if ((in.rdstate() & std::ifstream::eofbit) != 0) {
- in.setstate(std::ios::badbit);
- return -1;
- }
- name = Name;
- _base_salary = read_int32_t(in);
- _sold_nm = read_int32_t(in);
- _price = read_int32_t(in);
- return 0;
- }
- std::ofstream &operator<<(std::ofstream &os, const Employee &p) {
- p.print_bytes(os);
- return os;
- }
- std::ifstream &operator>>(std::ifstream &is, Employee &p) {
- p.scan_bytes(is);
- return is;
- }
- std::ostream &operator<<(std::ostream &os, const EmployeesArray &p) {
- if (p._employees.size() == 0) {
- std::cerr << "List is empty" << '\n';
- return os;
- }
- for (int i = 0; i < p._employees.size(); i++) {
- os << i + 1 << ". ";
- p._employees[i]->print(os);
- }
- os << "== Total salary: " << p.total_salary() << "\n\n";
- return os;
- }
- Employee *new_employee(int32_t type) {
- Employee *p;
- if (type == 1) p = new Developer();
- if (type == 2) p = new SalesManager;
- return p;
- }
- std::istream &operator>>(std::istream &is, EmployeesArray &p) {
- int32_t type;
- is >> type;
- Employee *a = new_employee(type);
- is >> *a;
- p.add(a);
- }
- std::ofstream &operator<<(std::ofstream &os, const EmployeesArray &p) {
- print_le_int32_t(os, p._employees.size());
- for (auto i : p._employees) {
- i->print_bytes(os);
- }
- return os;
- }
- std::ifstream &operator>>(std::ifstream &is, EmployeesArray &p) {
- int32_t size = read_int32_t(is);
- for (int i = 0; i < size; i++) {
- int32_t type = read_int32_t(is);
- Employee *a = new_employee(type);
- is >> *a;
- if (is.bad()) {
- std::cerr << "Wrong file format!" << '\n';
- break;
- }
- p.add(a);
- }
- return is;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement