Advertisement
Guest User

empl.cpp

a guest
Feb 21st, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1.  
  2. #include "employees.h"
  3. #include <cstring>
  4. #include <iostream>
  5. #include <fstream>
  6.  
  7.  
  8. using std::string;
  9.  
  10. #define BITS_PER_BYTE 8
  11.  
  12.  
  13.  
  14. int Developer::salary() const {
  15.     int salary = _base_salary;
  16.     if (_has_bonus) { salary += 1000; }
  17.     return salary;
  18. }
  19.  
  20. int SalesManager::salary() const {
  21.     return _base_salary + _sold_nm * _price * 0.01;
  22. }
  23.  
  24. EmployeesArray::~EmployeesArray() {
  25.     for (auto i : _employees) {
  26.         delete i;
  27.     }
  28. }
  29.  
  30. void EmployeesArray::add(Employee *e) {
  31.     _employees.push_back(e);
  32. }
  33.  
  34. int EmployeesArray::total_salary() const {
  35.     int total = 0;
  36.     for (int i = 0; i < _employees.size(); i++) {
  37.         total += _employees[i]->salary();
  38.     }
  39.     return total;
  40. }
  41.  
  42. void Developer::print(std::ostream &os) const {
  43.     os << "Developer" << '\n' << "Name: " << name << '\n' << "Base Salary: " << _base_salary << '\n' << "Has bonus: ";
  44.     if (_has_bonus) os << "+\n";
  45.     if (!_has_bonus) os << "-\n";
  46. }
  47.  
  48. void SalesManager::print(std::ostream &os) const {
  49.     os << "SalesManager" << '\n' << "Name: " << name << '\n' << "Base Salary: " << _base_salary << '\n'
  50.        << "Sold items: "
  51.        << _sold_nm << '\n' << "Item price: " << _price << '\n';
  52. }
  53.  
  54. void Developer::scan(std::istream &is) {
  55.     is >> name >> _base_salary >> _has_bonus;
  56. }
  57.  
  58. void SalesManager::scan(std::istream &is) {
  59.     is >> name >> _base_salary >> _sold_nm >> _price;
  60. }
  61.  
  62.  
  63. std::ostream &operator<<(std::ostream &os, const Employee &p) {
  64.     p.print(os);
  65.     return os;
  66. }
  67.  
  68. std::istream &operator>>(std::istream &is, Employee &p) {
  69.     p.scan(is);
  70.     return is;
  71. }
  72.  
  73. void print_le_int32_t(std::ofstream &out, int32_t n) {
  74.     for (size_t i = 0; i < sizeof(int32_t); i++) {
  75.         unsigned char c = (n >> (i * BITS_PER_BYTE)) & 0xFF;
  76.         out.write(reinterpret_cast<const char *>(&c), sizeof(c));
  77.     }
  78. }
  79.  
  80. void print_common_fields(std::ofstream &out, string name, int32_t base_salary) {
  81.     for (int i = 0; i < name.length(); i++) out.write(&(name[i]), sizeof(char));
  82.     char nul = '\0';
  83.     out.write(&nul, sizeof(nul));
  84.     print_le_int32_t(out, base_salary);
  85. }
  86.  
  87.  
  88. void Developer::print_bytes(std::ofstream &out) const {
  89.     int32_t type = 1;
  90.     print_le_int32_t(out, type);
  91.     print_common_fields(out, name, _base_salary);
  92.     char bonus = _has_bonus;
  93.     out.write(&bonus, sizeof(char));
  94. }
  95.  
  96. void SalesManager::print_bytes(std::ofstream &out) const {
  97.     int32_t type = 2;
  98.     print_le_int32_t(out, type);
  99.     print_common_fields(out, name, _base_salary);
  100.     print_le_int32_t(out, _sold_nm);
  101.     print_le_int32_t(out, _price);
  102. }
  103.  
  104. string read_name(std::ifstream &in) {
  105.     string name;
  106.     char c;
  107.     while (in.read(&c, sizeof(c))) {
  108.         if (c == '\0') {
  109.             break;
  110.         }
  111.         name.push_back(c);
  112.     }
  113.     return name;
  114. }
  115.  
  116. int32_t read_int32_t(std::ifstream &in) {
  117.     uint8_t c;
  118.     int32_t n = 0;
  119.     for (size_t i = 0; i < sizeof(int32_t); i++) {
  120.         if (in.read(reinterpret_cast<char *>(&c), sizeof(c))) {
  121.             n += c << (i * BITS_PER_BYTE);
  122.         }
  123.     }
  124.     return n;
  125. }
  126.  
  127. int Developer::scan_bytes(std::ifstream &in) {
  128.     string Name = read_name(in);
  129.     if ((in.rdstate() & std::ifstream::eofbit) != 0) {
  130.         in.setstate(std::ios::badbit);
  131.         return -1;
  132.     }
  133.     name = Name;
  134.     _base_salary = read_int32_t(in);
  135.     char bonus;
  136.     if (in.read(&bonus, sizeof(char))) {
  137.         _has_bonus = bonus;
  138.     }
  139.     return 0;
  140. }
  141.  
  142. int SalesManager::scan_bytes(std::ifstream &in) {
  143.     string Name = read_name(in);
  144.     if ((in.rdstate() & std::ifstream::eofbit) != 0) {
  145.         in.setstate(std::ios::badbit);
  146.         return -1;
  147.     }
  148.     name = Name;
  149.     _base_salary = read_int32_t(in);
  150.     _sold_nm = read_int32_t(in);
  151.     _price = read_int32_t(in);
  152.     return 0;
  153. }
  154.  
  155.  
  156. std::ofstream &operator<<(std::ofstream &os, const Employee &p) {
  157.     p.print_bytes(os);
  158.     return os;
  159. }
  160.  
  161. std::ifstream &operator>>(std::ifstream &is, Employee &p) {
  162.     p.scan_bytes(is);
  163.     return is;
  164. }
  165.  
  166. std::ostream &operator<<(std::ostream &os, const EmployeesArray &p) {
  167.     if (p._employees.size() == 0) {
  168.         std::cerr << "List is empty" << '\n';
  169.         return os;
  170.     }
  171.     for (int i = 0; i < p._employees.size(); i++) {
  172.         os << i + 1 << ". ";
  173.         p._employees[i]->print(os);
  174.     }
  175.     os << "== Total salary: " << p.total_salary() << "\n\n";
  176.     return os;
  177. }
  178.  
  179. Employee *new_employee(int32_t type) {
  180.     Employee *p;
  181.     if (type == 1) p = new Developer();
  182.     if (type == 2) p = new SalesManager;
  183.     return p;
  184. }
  185.  
  186.  
  187. std::istream &operator>>(std::istream &is, EmployeesArray &p) {
  188.     int32_t type;
  189.     is >> type;
  190.     Employee *a = new_employee(type);
  191.     is >> *a;
  192.     p.add(a);
  193.  
  194. }
  195.  
  196.  
  197. std::ofstream &operator<<(std::ofstream &os, const EmployeesArray &p) {
  198.     print_le_int32_t(os, p._employees.size());
  199.     for (auto i : p._employees) {
  200.         i->print_bytes(os);
  201.     }
  202.     return os;
  203. }
  204.  
  205. std::ifstream &operator>>(std::ifstream &is, EmployeesArray &p) {
  206.     int32_t size = read_int32_t(is);
  207.     for (int i = 0; i < size; i++) {
  208.         int32_t type = read_int32_t(is);
  209.         Employee *a = new_employee(type);
  210.         is >> *a;
  211.         if (is.bad()) {
  212.             std::cerr << "Wrong file format!" << '\n';
  213.             break;
  214.         }
  215.         p.add(a);
  216.     }
  217.     return is;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement