Advertisement
daniil_mironoff

Untitled

Nov 26th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.36 KB | None | 0 0
  1.  
  2.  
  3. #include <fstream>
  4. #include <iostream>
  5. #include <typeinfo>
  6. #include "conio.h"
  7.  
  8. using namespace std;
  9.  
  10. // Максимальный размер строки (разделитель включительно)
  11. const int LEN = 32;
  12. // Максимальный размер
  13. const int MAXEM = 100;
  14.  
  15. // Типы работников
  16. enum employee_type { tmanager, tscientist, tlaborer };
  17.  
  18. // Базовый класс
  19. class employee {
  20.     protected:
  21.         // Фамилия сотрудника
  22.         char name[LEN];
  23.         // Его порядковый номер
  24.         unsigned long number;
  25.  
  26.         // Вывод данных в консоль
  27.         virtual void putdata() {
  28.             cout << "Фамилия: " << name   << endl;
  29.             cout << "Номер: "   << number << endl;
  30.  
  31.         }
  32.  
  33.         // Ввод данных с консоли
  34.         virtual void getdata() {
  35.             cout << "Введите фамилию: "; cin >> name;
  36.             cout << "Введите номер: ";   cin >> number;
  37.        
  38.         }
  39.  
  40.         // Виртуальный метод записи в файл
  41.         virtual void writeToStream(ofstream& outfile) {
  42.             outfile.write((char*)name, sizeof(char) * LEN);
  43.             outfile.write((char*)&number, sizeof(long));
  44.         }
  45.  
  46.         // Виртуальный метод чтения из файла
  47.         virtual void readInStream(ifstream& infile) {
  48.             infile.read((char*)name, sizeof(char) * LEN);
  49.             infile.read((char*)&number, sizeof(long));
  50.         }
  51.  
  52.     public:
  53.         // Добавить работника
  54.         static void add();
  55.  
  56.         // Вывести данные обо всех
  57.         static void display();
  58.  
  59.         // Чтение из файла
  60.         static void read();
  61.  
  62.         // Запись в файл
  63.         static void write();
  64.  
  65.         // Поиск работника по номеру
  66.         static void fileFind();
  67.  
  68.  
  69. };
  70.  
  71. // Текущее число работников
  72. int n = 0;
  73.  
  74. // Массив указателей на родительский класс
  75. employee* arrap[MAXEM];
  76.  
  77. ///////////////////////////////////////////////////////////
  78.  
  79. // Дочерний класс Менеджер
  80. class manager : public employee {
  81.     private:
  82.         // Должность менеджера
  83.         char title[LEN];
  84.         // Налоги
  85.         double dues;
  86.  
  87.     public:
  88.         // Перегрузка ввода данных с консоли
  89.         void getdata() {
  90.             employee::getdata();
  91.             cout << "Введите титул: ";  cin >> title;
  92.             cout << "Введите налоги: "; cin >> dues;
  93.  
  94.         }
  95.  
  96.         // Перегрузка вывода данных в консоль
  97.         void putdata() {
  98.             cout << "manager" << endl;
  99.             employee::putdata();
  100.             cout << "Титул: "              << title << endl;
  101.             cout << "Налог: " << dues  << endl;
  102.  
  103.         }
  104.  
  105.         // Перегрузка записи данных в файл
  106.         void writeToStream(ofstream& outfile) {
  107.             employee_type temp_type = tmanager;
  108.             outfile.write((char*)&temp_type, sizeof(employee_type));
  109.             outfile.write((char*)title, sizeof(char) * LEN);
  110.             outfile.write((char*)&dues, sizeof(double));
  111.  
  112.             employee::writeToStream(outfile);
  113.         }
  114.  
  115.         // Перегрузка чтения данных из файла
  116.         void readInStream(ifstream& infile) {
  117.             infile.read((char*)title, sizeof(char) * LEN);
  118.             infile.read((char*)&dues, sizeof(double));
  119.  
  120.             employee::readInStream(infile);
  121.         }
  122.  
  123. };
  124.  
  125. ///////////////////////////////////////////////////////////
  126.  
  127. class scientist : public employee {
  128.     private:
  129.         int pubs;
  130.  
  131.     public:
  132.         // Перегрузка ввода данных с консоли
  133.         void getdata() {
  134.             employee::getdata();
  135.             cout << "Введите число публикаций: "; cin >> pubs;
  136.            
  137.         }
  138.  
  139.         // Перегрузка вывода данных в консоль
  140.         void putdata() {
  141.             cout << "scientist" << endl;
  142.             employee::putdata();
  143.             cout << "Число публикаций: " << pubs << endl;
  144.  
  145.         }
  146.  
  147.         // Перегрузка записи данных в файл
  148.         void writeToStream(ofstream& outfile) {
  149.             employee_type temp_type = tscientist;
  150.             outfile.write((char*)&temp_type, sizeof(employee_type));
  151.             outfile.write((char*)&pubs, sizeof(int));
  152.  
  153.             employee::writeToStream(outfile);
  154.         }
  155.  
  156.         // Перегрузка чтения данных из файла
  157.         void readInStream(ifstream& infile) {
  158.             infile.read((char*)&pubs, sizeof(int));
  159.  
  160.             employee::readInStream(infile);
  161.         }
  162.  
  163.  
  164. };
  165.  
  166. ///////////////////////////////////////////////////////////
  167.  
  168. class laborer : public employee {
  169.     public:
  170.         // Перегрузка вывода данных в консоль
  171.         void putdata() {
  172.             cout << "laborer" << endl;
  173.             employee::putdata();
  174.  
  175.         }
  176.  
  177.         // Перегрузка записи данных в файл
  178.         void writeToStream(ofstream& outfile) {
  179.             employee_type temp_type = tlaborer;
  180.             outfile.write((char*)&temp_type, sizeof(employee_type));
  181.  
  182.             employee::writeToStream(outfile);
  183.         }
  184.  
  185.         // Перегрузка чтения данных из файла
  186.         void readInStream(ifstream& infile) {
  187.  
  188.             employee::readInStream(infile);
  189.         }
  190.  };
  191.  
  192. ///////////////////////////////////////////////////////////
  193.  
  194. // Добавить работника в список (в RAM)
  195. void employee::add() {
  196.     cout << "Выберите должность:" << endl
  197.          << "[M] - Менеджер"      << endl
  198.          << "[S] - Ученый"        << endl
  199.          << "[L] - Рабочий"       << endl
  200.          << endl;
  201.  
  202.     char ch;
  203.  
  204.     while(true) {
  205.         ch = getch();
  206.  
  207.         if (ch == 'm') { arrap[n] = new manager;   break; } else
  208.         if (ch == 's') { arrap[n] = new scientist; break; } else
  209.         if (ch == 'l') { arrap[n] = new laborer;   break; }
  210.     }
  211.  
  212.     arrap[n++] -> getdata();
  213.  
  214.     cout << "Сотрудник добавлен" << endl;
  215. }
  216.  
  217. //---------------------------------------------------------
  218.  
  219. // Вывести данные обо всех работниках (в RAM)
  220. void employee::display() {
  221.  
  222.     for(int i = 0; i < n; i++) {
  223.         cout << i + 1 << ". ";
  224.         arrap[i] -> putdata(); // Вывод данных
  225.         cout << endl;
  226.  
  227.     }
  228.  
  229. }
  230.  
  231. //---------------------------------------------------------
  232.  
  233. // Записать объектов из RAM в файл
  234. void employee::write() {
  235.     ofstream outfile; outfile.open("EMPLOY.DAT", ios::trunc);
  236.  
  237.     for (int i = 0; i < n; i++) { arrap[i] -> writeToStream(outfile); }
  238.  
  239.     outfile.close();
  240.  
  241.     cout << "Объекты записаны в файл" << endl;
  242. }
  243.  
  244. //---------------------------------------------------------
  245.  
  246. // Чтение всех данных из файла в память
  247. void employee::read() {
  248.     ifstream infile;
  249.     employee_type etype;
  250.  
  251.     infile.open("EMPLOY.DAT", ios::binary);
  252.  
  253.     infile.seekg(0, ios::end);
  254.  
  255.     int index_end_file = infile.tellg();
  256.  
  257.     infile.seekg(0, ios::beg);
  258.  
  259.     // Утечка
  260.     n = 0;
  261.  
  262.     // Считывать данные до конца
  263.     while(infile) {
  264.         infile.read(reinterpret_cast<char*>(&etype), sizeof(employee_type));
  265.  
  266.         switch(etype) {
  267.             case tmanager:   arrap[n] = new manager;   break;
  268.             case tscientist: arrap[n] = new scientist; break;
  269.             case tlaborer:   arrap[n] = new laborer;   break;
  270.         }
  271.  
  272.         arrap[n++] -> readInStream(infile);
  273.        
  274.     }
  275.  
  276.     infile.close();
  277.  
  278.     cout << "Объекты записаны в RAM" << endl;
  279. }
  280.  
  281. //---------------------------------------------------------
  282.  
  283. void employee::fileFind() {
  284.     ifstream infile;
  285.     employee_type etype;
  286.  
  287.     infile.open("EMPLOY.DAT", ios::binary);
  288.  
  289.     infile.seekg(0, ios::end);
  290.  
  291.     int index_end_file = infile.tellg();
  292.  
  293.     infile.seekg(0, ios::beg);
  294.    
  295.     cout << "Введите номер искомого сотрудника: ";
  296.     long search_number; cin >> search_number;
  297.     long current_number;
  298.  
  299.     // Считывать данные, пока файловый указатель не укажет на конец файла
  300.     while(infile.tellg() != index_end_file) {
  301.         infile.read(reinterpret_cast<char*>(&etype), sizeof(employee_type));
  302.  
  303.         switch(etype) {
  304.             case tmanager:   infile.seekg((int)infile.tellg() + sizeof(char) * LEN * 2 + sizeof(double), ios::beg); break;
  305.             case tscientist: infile.seekg((int)infile.tellg() + sizeof(char) * LEN + sizeof(int),        ios::beg); break;
  306.             case tlaborer:   infile.seekg((int)infile.tellg() + sizeof(char) * LEN,                      ios::beg); break;
  307.         }
  308.  
  309.         infile.read((char*)(&current_number), sizeof(long));
  310.  
  311.         if (current_number == search_number) {
  312.             cout << "Найден сотрудник по номеру" << endl;
  313.             employee* temp_obj;
  314.  
  315.             if (etype == tmanager) {
  316.                 infile.seekg((int)infile.tellg() - sizeof(char) * LEN * 2 - sizeof(double) - sizeof(long), ios::beg);
  317.                 temp_obj = new manager;
  318.             } else
  319.  
  320.             if (etype == tscientist) {
  321.                 infile.seekg((int)infile.tellg() - sizeof(char) * LEN - sizeof(int) - sizeof(long), ios::beg);
  322.                 temp_obj = new scientist;
  323.             } else
  324.  
  325.             if (etype == tlaborer) {
  326.                 infile.seekg((int)infile.tellg() - sizeof(char) * LEN - sizeof(long), ios::beg);
  327.                 temp_obj = new laborer;
  328.             }
  329.  
  330.             temp_obj -> readInStream(infile);
  331.             temp_obj -> putdata();
  332.             delete temp_obj;
  333.         }
  334.     }
  335. }
  336.  
  337. ///////////////////////////////////////////////////////////
  338.  
  339. int main(int count, char* args[]) {
  340.     cout << "Выберите действие:"                        << endl
  341.          << "[A] - Добавить работника"                  << endl
  342.          << "[D] - Вывести всех работников"             << endl
  343.          << "[W] - Записать все данные в файл"          << endl
  344.          << "[R] - Прочитать все данные из файла"       << endl
  345.          << "[F] - Вывести рабочего из файла по номеру" << endl
  346.          << "[X] - Выход"                               << endl
  347.          << endl;
  348.  
  349.     char ch;
  350.  
  351.     while(true) {
  352.  
  353.         ch = getch();
  354.  
  355.         switch(ch) {
  356.             case 'a': employee::add();      break;
  357.             case 'd': employee::display();  break;
  358.             case 'w': employee::write();    break;
  359.             case 'r': employee::read();     break;
  360.             case 'f': employee::fileFind(); break;
  361.  
  362.             case 'x': exit(0);
  363.         }
  364.  
  365.     }
  366.  
  367.     return 0;
  368. }
  369.  
  370. // ヽ(°□° )ノ СКОЛЬКО СТРОК (разделить бы все на файлы...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement