Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include<iomanip>
- using namespace std;
- class ship
- {
- protected:
- string name;
- string tactical_number;
- double speed;
- bool readiness;
- double resource_DU;
- public:
- virtual ~ship();
- void set_remont(bool x);
- bool check_speed(double s);
- double show_resource_DU();
- virtual void Show();
- virtual void ReadFromFile(ifstream &f);
- virtual void WriteToFile(ofstream &f);
- virtual void input();
- virtual void input(int k);
- };
- class aerocarrier: public ship
- {
- private:
- double displacement;
- string num_of_wings;
- string weapons;
- public:
- ~aerocarrier();
- void Show();
- void ReadFromFile(ifstream &f);
- void input(int k);
- void WriteToFile(ofstream &f);
- };
- class destroyer: public ship
- {
- private:
- double displacement;
- string weapons;
- public:
- ~destroyer();
- void Show();
- void ReadFromFile(ifstream &f);
- void input(int k);
- void WriteToFile(ofstream &f);
- };
- class submarine: public ship
- {
- private:
- double surface_displacement;
- double underwater_displacement;
- string weapons;
- string resource_body;
- public:
- ~submarine();
- void Show();
- void ReadFromFile(ifstream &f);
- void input(int k);
- void WriteToFile(ofstream &f);
- };
- class small_rocket_ship: public ship
- {
- private:
- string displacement;
- string weapons;
- public:
- ~small_rocket_ship();
- void Show();
- void ReadFromFile(ifstream &f);
- void input(int k);
- void WriteToFile(ofstream &f);
- };
- ship::~ship()
- {
- // cout << "Deleted - " << Show() << endl;
- }
- void ship::Show()
- {
- cout << " | Название: " << internal << setw(10) << name
- << " | Тактический номер: " << internal << setw(10) << tactical_number
- << " | Скорость: " << internal << setw(10) << speed
- << " | Готовность (1 готов, 0 нет): " << internal << setw(10) << readiness
- << " | Ост.ресурс ДУ: " << internal << setw(10) << resource_DU;
- }
- void ship::ReadFromFile(ifstream &f)
- {
- }
- void ship::WriteToFile(ofstream &f)
- {
- }
- double ship::show_resource_DU()
- {
- return resource_DU;
- }
- void ship::input()
- {
- cout << "Введите название: ";
- cin >> name;
- cout << "Введите тактический номер: ";
- cin >> tactical_number;
- cout << "Введите скорость: ";
- cin >> speed;
- cout << "Введите ост.ресурс ДУ: ";
- cin >> resource_DU;
- readiness = true;
- }
- void ship::input(int k)
- {
- cout << "Введите название: ";
- cin >> name;
- cout << "Введите тактический номер: ";
- cin >> tactical_number;
- cout << "Введите скорость: ";
- cin >> speed;
- cout << "Введите ост.ресурс ДУ: ";
- cin >> resource_DU;
- readiness = false;
- }
- bool ship::check_speed(double s)
- {
- if (s <= speed) return true;
- else return false;
- }
- aerocarrier::~aerocarrier()
- {
- //cout << "Deleted - " << Show() << endl;
- }
- void aerocarrier::Show()
- {
- cout <<" aerocarrier: ";
- ship::Show();
- cout << " | Водоизмещение: " << internal << setw(10) << displacement
- << " | Численность авиакрыла: " << internal << setw(10) << num_of_wings
- << " | Вооружение: " << internal << setw(10) << weapons;
- }
- void aerocarrier::ReadFromFile(ifstream &f)
- {
- f>> name >> tactical_number >> speed >> readiness >> displacement >> num_of_wings >> weapons >> resource_DU;
- }
- void aerocarrier::WriteToFile(ofstream &f)
- {
- f <<"aerocarrier " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << displacement << " " << num_of_wings << " " << weapons << " " << resource_DU << endl;
- }
- void aerocarrier::input(int k)
- {
- if (k == 1)
- ship::input(k);
- else
- ship::input();
- cout << "Введите водоизмещение: ";
- cin >> displacement;
- cout << "Введите численность авиакрыла: ";
- cin >> num_of_wings;
- cout << "Введите вооружение: ";
- cin >> weapons;
- }
- destroyer::~destroyer()
- {
- // cout << "Deleted - " << Show() << endl;
- }
- void destroyer::Show()
- {
- cout <<" destroyer ";
- ship::Show();
- cout << " | displacement: " << internal << setw(10) << displacement
- << " | Вооружение: " << internal << setw(10) << weapons;
- }
- void destroyer::ReadFromFile(ifstream &f)
- {
- f>> name >> tactical_number >> speed >> readiness >> displacement >> weapons >> resource_DU;
- }
- void destroyer::WriteToFile(ofstream &f)
- {
- f <<"destroyer " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << displacement << " " << weapons << " " << resource_DU << endl;
- }
- void destroyer::input(int k)
- {
- if (k == 1)
- ship::input(k);
- else
- ship::input();
- cout << "Введите водоизмещение: ";
- cin >> displacement;
- cout << "Введите вооружение: ";
- cin >> weapons;
- }
- submarine::~submarine()
- {
- //cout << "Deleted - " << Show() << endl;
- }
- void submarine::Show()
- {
- cout <<" submarine ";
- ship::Show();
- cout << " | Надводное водоизмещение: " << internal << setw(10) << surface_displacement
- << " | Подводное водоизмещение: " << internal << setw(10) << underwater_displacement
- << " | Вооружение: " << internal << setw(10) << weapons
- << " | Ост.ресурс корпуса: " << internal << setw(10) << resource_body;
- }
- void submarine::ReadFromFile(ifstream &f)
- {
- cout << " 143 ";
- f>> name >> tactical_number >> speed >> readiness >> surface_displacement >> underwater_displacement >> weapons >> resource_DU >> resource_body;
- }
- void submarine::WriteToFile(ofstream &f)
- {
- f <<"submarine " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << surface_displacement << " " << underwater_displacement << " " << weapons << " " << resource_DU << " " << resource_body <<endl;
- }
- void submarine::input(int k)
- {
- if (k == 1)
- ship::input(k);
- else
- ship::input();
- cout << "Введите надводное водоизмещение: ";
- cin >> surface_displacement;
- cout << "Введите подводное водоизмещение: ";
- cin >> underwater_displacement;
- cout << "Введите вооружение: ";
- cin >> weapons;
- cout << "Введите ост.ресурс корпуса: ";
- cin >> resource_body;
- }
- small_rocket_ship::~small_rocket_ship()
- {
- //cout << "Deleted - " << Show() << endl;
- }
- void small_rocket_ship::Show()
- {
- cout <<" small_rocket_ship ";
- ship::Show();
- cout << " | Водоизмещение: " << internal << setw(10) << displacement
- << " | Вооружение: " << internal << setw(10) << weapons;
- }
- void small_rocket_ship::ReadFromFile(ifstream &f)
- {
- f>> name >> tactical_number >> speed >> readiness >> resource_DU >> displacement >> weapons;
- }
- void small_rocket_ship::WriteToFile(ofstream &f)
- {
- f <<"small_rocket_ship " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << resource_DU << " " << displacement << " " << weapons << endl;
- }
- void small_rocket_ship::input(int k)
- {
- if (k == 1)
- ship::input(k);
- else
- ship::input();
- cout << "Введите водоизмещение: ";
- cin >> displacement;
- cout << "Введите вооружение: ";
- cin >> weapons;
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- vector <ship *> vShip; // создаем вектор указателей
- bool isProgramActive = true;
- while (isProgramActive) // пока не поступило сигнала о завершении программы, выполняем код ниже
- {
- cout << "\n1. Добавить новый корабль\n"
- "2. Добавить корабль после ремонта\n"
- "3. Вывод корабля из состава (удаление)\n"
- "4. Показать корабельный состав с указанием боеготовности\n"
- "5. Показать список кораблей со скоростью движения не ниже заданной\n"
- "6. Вывод корабля с наименьшим остаточным ресурсом ДУ\n"
- "0. Выход\n"
- "9. Считать с файла\n"
- "10. Записать в файл\n"
- "\nВведите номер действия: ";
- //156790
- int action = -1;
- cin >>action;
- cout << endl;
- switch (action)
- {
- case 0:
- {
- for ( int i = 0; i<vShip.size();i++)
- {
- delete vShip[i];
- }
- isProgramActive = false;
- break;
- }
- case 1:
- {
- cout << "Введите тип судна (aerocarrier, destroyer, submarine, small_rocket_ship): ";
- string type;
- cin >> type;
- ship *t=0;
- if (type=="aerocarrier")t=new aerocarrier;
- if (type=="destroyer")t=new destroyer;
- if (type=="submarine") t=new submarine;
- if (type=="small_rocket_ship") t=new small_rocket_ship;
- t->input(0);
- vShip.push_back(t);
- break;
- }
- case 2:
- {
- cout << "Введите тип судна (aerocarrier, destroyer, submarine, small_rocket_ship): ";
- string type;
- cin >> type;
- ship *t=0;
- if (type=="aerocarrier")t=new aerocarrier;
- if (type=="destroyer")t=new destroyer;
- if (type=="submarine") t=new submarine;
- if (type=="small_rocket_ship") t=new small_rocket_ship;
- t->input(1);
- vShip.push_back(t);
- break;
- }
- case 3:
- {
- int id = -1;
- while (id < 0)
- {
- cout << "Введите порядковый номер: ";
- cin >> id;
- if (id >= vShip.size())
- id = -1;
- }
- cout << "Удалён: ";
- vShip[id]->Show();
- delete vShip[id]; // освобождаем дин.память
- vShip.erase(vShip.begin() + id); // удаляем указатель из вектора
- break;
- }
- case 4:
- {
- for (int i = 0; i < vShip.size(); i++)
- {
- cout << i << ". "; // номер
- vShip[i]->Show();
- cout <<endl;
- }
- break;
- }
- case 5:
- {
- int k=0;
- cout << "Введите скорость: ";
- double speed;
- cin >> speed;
- for ( int i = 0; i < vShip.size(); i++)
- {
- if ( vShip[i]->check_speed(speed)==true)
- {
- cout << k << ". "; // номер
- vShip[i]->Show();
- k++;
- cout <<endl;
- }
- }
- break;
- }
- case 6:
- {
- double max =999999999;
- int k = 0;
- for (int i = 0; i<vShip.size(); ++i)
- {
- if (vShip[i]->show_resource_DU() < max)
- {
- k = i;
- max = vShip[i]->show_resource_DU();
- }
- }
- if (max!=999999999)
- cout << " Наименьший остаточный ресурс у " << k << " корабля и он равен " << max << endl;
- break;
- }
- case 9:
- {
- ifstream fin("E:\input.txt");
- if(!fin.is_open())
- {
- cout << "Файл отсутствует\n";
- break;
- }
- while (!fin.eof())
- {
- string type;
- fin >> type;
- ship *t=0;
- if (type=="aerocarrier") t=new aerocarrier;
- if (type=="destroyer") t=new destroyer;
- if (type=="submarine") t=new submarine;
- if (type=="small_rocket_ship") t=new small_rocket_ship;
- if(t)
- {
- t->ReadFromFile(fin);
- vShip.push_back(t);
- }
- }
- break;
- }
- case 10:
- {
- ofstream fout("E:\input.txt");
- for (int i = 0; i<vShip.size();++i)
- {
- vShip[i]->WriteToFile(fout);
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement