Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <iostream>
- #include <typeinfo>
- #include "conio.h"
- using namespace std;
- // Максимальный размер строки (разделитель включительно)
- const int LEN = 32;
- // Максимальный размер
- const int MAXEM = 100;
- // Типы работников
- enum employee_type { tmanager, tscientist, tlaborer };
- // Базовый класс
- class employee {
- protected:
- // Фамилия сотрудника
- char name[LEN];
- // Его порядковый номер
- unsigned long number;
- // Вывод данных в консоль
- virtual void putdata() {
- cout << "Фамилия: " << name << endl;
- cout << "Номер: " << number << endl;
- }
- // Ввод данных с консоли
- virtual void getdata() {
- cout << "Введите фамилию: "; cin >> name;
- cout << "Введите номер: "; cin >> number;
- }
- // Виртуальный метод записи в файл
- virtual void writeToStream(ofstream& outfile) {
- outfile.write((char*)name, sizeof(char) * LEN);
- outfile.write((char*)&number, sizeof(long));
- }
- // Виртуальный метод чтения из файла
- virtual void readInStream(ifstream& infile) {
- infile.read((char*)name, sizeof(char) * LEN);
- infile.read((char*)&number, sizeof(long));
- }
- public:
- // Добавить работника
- static void add();
- // Вывести данные обо всех
- static void display();
- // Чтение из файла
- static void read();
- // Запись в файл
- static void write();
- // Поиск работника по номеру
- static void fileFind();
- };
- // Текущее число работников
- int n = 0;
- // Массив указателей на родительский класс
- employee* arrap[MAXEM];
- ///////////////////////////////////////////////////////////
- // Дочерний класс Менеджер
- class manager : public employee {
- private:
- // Должность менеджера
- char title[LEN];
- // Налоги
- double dues;
- public:
- // Перегрузка ввода данных с консоли
- void getdata() {
- employee::getdata();
- cout << "Введите титул: "; cin >> title;
- cout << "Введите налоги: "; cin >> dues;
- }
- // Перегрузка вывода данных в консоль
- void putdata() {
- cout << "manager" << endl;
- employee::putdata();
- cout << "Титул: " << title << endl;
- cout << "Налог: " << dues << endl;
- }
- // Перегрузка записи данных в файл
- void writeToStream(ofstream& outfile) {
- employee_type temp_type = tmanager;
- outfile.write((char*)&temp_type, sizeof(employee_type));
- outfile.write((char*)title, sizeof(char) * LEN);
- outfile.write((char*)&dues, sizeof(double));
- employee::writeToStream(outfile);
- }
- // Перегрузка чтения данных из файла
- void readInStream(ifstream& infile) {
- infile.read((char*)title, sizeof(char) * LEN);
- infile.read((char*)&dues, sizeof(double));
- employee::readInStream(infile);
- }
- };
- ///////////////////////////////////////////////////////////
- class scientist : public employee {
- private:
- int pubs;
- public:
- // Перегрузка ввода данных с консоли
- void getdata() {
- employee::getdata();
- cout << "Введите число публикаций: "; cin >> pubs;
- }
- // Перегрузка вывода данных в консоль
- void putdata() {
- cout << "scientist" << endl;
- employee::putdata();
- cout << "Число публикаций: " << pubs << endl;
- }
- // Перегрузка записи данных в файл
- void writeToStream(ofstream& outfile) {
- employee_type temp_type = tscientist;
- outfile.write((char*)&temp_type, sizeof(employee_type));
- outfile.write((char*)&pubs, sizeof(int));
- employee::writeToStream(outfile);
- }
- // Перегрузка чтения данных из файла
- void readInStream(ifstream& infile) {
- infile.read((char*)&pubs, sizeof(int));
- employee::readInStream(infile);
- }
- };
- ///////////////////////////////////////////////////////////
- class laborer : public employee {
- public:
- // Перегрузка вывода данных в консоль
- void putdata() {
- cout << "laborer" << endl;
- employee::putdata();
- }
- // Перегрузка записи данных в файл
- void writeToStream(ofstream& outfile) {
- employee_type temp_type = tlaborer;
- outfile.write((char*)&temp_type, sizeof(employee_type));
- employee::writeToStream(outfile);
- }
- // Перегрузка чтения данных из файла
- void readInStream(ifstream& infile) {
- employee::readInStream(infile);
- }
- };
- ///////////////////////////////////////////////////////////
- // Добавить работника в список (в RAM)
- void employee::add() {
- cout << "Выберите должность:" << endl
- << "[M] - Менеджер" << endl
- << "[S] - Ученый" << endl
- << "[L] - Рабочий" << endl
- << endl;
- char ch;
- while(true) {
- ch = getch();
- if (ch == 'm') { arrap[n] = new manager; break; } else
- if (ch == 's') { arrap[n] = new scientist; break; } else
- if (ch == 'l') { arrap[n] = new laborer; break; }
- }
- arrap[n++] -> getdata();
- cout << "Сотрудник добавлен" << endl;
- }
- //---------------------------------------------------------
- // Вывести данные обо всех работниках (в RAM)
- void employee::display() {
- for(int i = 0; i < n; i++) {
- cout << i + 1 << ". ";
- arrap[i] -> putdata(); // Вывод данных
- cout << endl;
- }
- }
- //---------------------------------------------------------
- // Записать объектов из RAM в файл
- void employee::write() {
- ofstream outfile; outfile.open("EMPLOY.DAT", ios::trunc);
- for (int i = 0; i < n; i++) { arrap[i] -> writeToStream(outfile); }
- outfile.close();
- cout << "Объекты записаны в файл" << endl;
- }
- //---------------------------------------------------------
- // Чтение всех данных из файла в память
- void employee::read() {
- ifstream infile;
- employee_type etype;
- infile.open("EMPLOY.DAT", ios::binary);
- infile.seekg(0, ios::end);
- int index_end_file = infile.tellg();
- infile.seekg(0, ios::beg);
- // Утечка
- n = 0;
- // Считывать данные до конца
- while(infile) {
- infile.read(reinterpret_cast<char*>(&etype), sizeof(employee_type));
- switch(etype) {
- case tmanager: arrap[n] = new manager; break;
- case tscientist: arrap[n] = new scientist; break;
- case tlaborer: arrap[n] = new laborer; break;
- }
- arrap[n++] -> readInStream(infile);
- }
- infile.close();
- cout << "Объекты записаны в RAM" << endl;
- }
- //---------------------------------------------------------
- void employee::fileFind() {
- ifstream infile;
- employee_type etype;
- infile.open("EMPLOY.DAT", ios::binary);
- infile.seekg(0, ios::end);
- int index_end_file = infile.tellg();
- infile.seekg(0, ios::beg);
- cout << "Введите номер искомого сотрудника: ";
- long search_number; cin >> search_number;
- long current_number;
- // Считывать данные, пока файловый указатель не укажет на конец файла
- while(infile.tellg() != index_end_file) {
- infile.read(reinterpret_cast<char*>(&etype), sizeof(employee_type));
- switch(etype) {
- case tmanager: infile.seekg((int)infile.tellg() + sizeof(char) * LEN * 2 + sizeof(double), ios::beg); break;
- case tscientist: infile.seekg((int)infile.tellg() + sizeof(char) * LEN + sizeof(int), ios::beg); break;
- case tlaborer: infile.seekg((int)infile.tellg() + sizeof(char) * LEN, ios::beg); break;
- }
- infile.read((char*)(¤t_number), sizeof(long));
- if (current_number == search_number) {
- cout << "Найден сотрудник по номеру" << endl;
- employee* temp_obj;
- if (etype == tmanager) {
- infile.seekg((int)infile.tellg() - sizeof(char) * LEN * 2 - sizeof(double) - sizeof(long), ios::beg);
- temp_obj = new manager;
- } else
- if (etype == tscientist) {
- infile.seekg((int)infile.tellg() - sizeof(char) * LEN - sizeof(int) - sizeof(long), ios::beg);
- temp_obj = new scientist;
- } else
- if (etype == tlaborer) {
- infile.seekg((int)infile.tellg() - sizeof(char) * LEN - sizeof(long), ios::beg);
- temp_obj = new laborer;
- }
- temp_obj -> readInStream(infile);
- temp_obj -> putdata();
- delete temp_obj;
- }
- }
- }
- ///////////////////////////////////////////////////////////
- int main(int count, char* args[]) {
- cout << "Выберите действие:" << endl
- << "[A] - Добавить работника" << endl
- << "[D] - Вывести всех работников" << endl
- << "[W] - Записать все данные в файл" << endl
- << "[R] - Прочитать все данные из файла" << endl
- << "[F] - Вывести рабочего из файла по номеру" << endl
- << "[X] - Выход" << endl
- << endl;
- char ch;
- while(true) {
- ch = getch();
- switch(ch) {
- case 'a': employee::add(); break;
- case 'd': employee::display(); break;
- case 'w': employee::write(); break;
- case 'r': employee::read(); break;
- case 'f': employee::fileFind(); break;
- case 'x': exit(0);
- }
- }
- return 0;
- }
- // ヽ(°□° )ノ СКОЛЬКО СТРОК (разделить бы все на файлы...)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement