Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include<iomanip>
  6. using namespace std;
  7. class ship
  8. {
  9. protected:
  10.     string name;
  11.     string tactical_number;
  12.     double speed;
  13.     bool readiness;
  14.     double resource_DU;
  15.  
  16. public:
  17.     virtual ~ship();
  18.     void set_remont(bool x);
  19.     bool check_speed(double s);
  20.     double show_resource_DU();
  21.     virtual void Show();
  22.     virtual void ReadFromFile(ifstream &f);
  23.     virtual void WriteToFile(ofstream &f);
  24.     virtual void input();
  25.     virtual void input(int k);
  26. };
  27.  
  28. class aerocarrier: public ship
  29. {
  30. private:
  31.     double displacement;
  32.     string num_of_wings;
  33.     string weapons;
  34. public:
  35.     ~aerocarrier();
  36.     void Show();
  37.     void ReadFromFile(ifstream &f);
  38.     void input(int k);
  39.     void WriteToFile(ofstream &f);
  40.  
  41. };
  42.  
  43. class destroyer: public ship
  44. {
  45. private:
  46.     double displacement;
  47.     string weapons;
  48.  
  49. public:
  50.     ~destroyer();
  51.     void Show();
  52.     void ReadFromFile(ifstream &f);
  53.     void input(int k);
  54.     void WriteToFile(ofstream &f);
  55.  
  56. };
  57.  
  58. class submarine: public ship
  59. {
  60. private:
  61.     double surface_displacement;
  62.     double underwater_displacement;
  63.     string weapons;
  64.     string resource_body;
  65.  
  66. public:
  67.     ~submarine();
  68.     void Show();
  69.     void ReadFromFile(ifstream &f);
  70.     void input(int k);
  71.     void WriteToFile(ofstream &f);
  72.  
  73. };
  74.  
  75. class small_rocket_ship: public ship
  76. {
  77. private:
  78.     string displacement;
  79.     string weapons;
  80. public:
  81.     ~small_rocket_ship();
  82.     void Show();
  83.     void ReadFromFile(ifstream &f);
  84.     void input(int k);
  85.     void WriteToFile(ofstream &f);
  86.  
  87.  
  88. };
  89.  
  90.  
  91.  
  92. ship::~ship()
  93. {
  94.    // cout << "Deleted - " << Show() << endl;
  95. }
  96.  
  97.  
  98. void ship::Show()
  99. {
  100.     cout << " | Название: "    << internal << setw(10) << name
  101.          << " | Тактический номер: "    << internal << setw(10) << tactical_number
  102.          << " | Скорость: " << internal << setw(10) << speed
  103.          << " | Готовность (1 готов, 0 нет): "    << internal << setw(10) << readiness
  104.          << " | Ост.ресурс ДУ: " << internal << setw(10) << resource_DU;
  105.  
  106.  
  107. }
  108. void ship::ReadFromFile(ifstream &f)
  109. {
  110.  
  111. }
  112. void ship::WriteToFile(ofstream &f)
  113. {
  114.  
  115. }
  116. double ship::show_resource_DU()
  117. {
  118.     return resource_DU;
  119. }
  120.  
  121. void ship::input()
  122. {
  123.     cout << "Введите название: ";
  124.     cin >> name;
  125.     cout << "Введите тактический номер: ";
  126.     cin >> tactical_number;
  127.     cout << "Введите скорость: ";
  128.     cin >> speed;
  129.     cout << "Введите ост.ресурс ДУ: ";
  130.     cin >> resource_DU;
  131.     readiness = true;
  132. }
  133. void ship::input(int k)
  134. {
  135.     cout << "Введите название: ";
  136.     cin >> name;
  137.     cout << "Введите тактический номер: ";
  138.     cin >> tactical_number;
  139.     cout << "Введите скорость: ";
  140.     cin >> speed;
  141.     cout << "Введите ост.ресурс ДУ: ";
  142.     cin >> resource_DU;
  143.     readiness = false;
  144. }
  145. bool ship::check_speed(double s)
  146. {
  147.     if (s <= speed) return true;
  148.     else return false;
  149. }
  150.  
  151.  
  152. aerocarrier::~aerocarrier()
  153. {
  154.     //cout << "Deleted - " << Show() << endl;
  155. }
  156.  
  157. void aerocarrier::Show()
  158. {
  159.  
  160.     cout <<" aerocarrier: ";
  161.     ship::Show();
  162.     cout << " | Водоизмещение: "    << internal << setw(10) << displacement
  163.          << " | Численность авиакрыла: "    << internal << setw(10) << num_of_wings
  164.          << " | Вооружение: "    << internal << setw(10) << weapons;
  165.  
  166.  
  167. }
  168. void aerocarrier::ReadFromFile(ifstream &f)
  169. {
  170.     f>> name >> tactical_number >> speed >> readiness >> displacement >> num_of_wings >> weapons >> resource_DU;
  171. }
  172. void aerocarrier::WriteToFile(ofstream &f)
  173. {
  174.     f <<"aerocarrier " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << displacement << " " <<  num_of_wings << " " << weapons << " " << resource_DU << endl;
  175.  
  176. }
  177. void aerocarrier::input(int k)
  178. {
  179.     if (k == 1)
  180.         ship::input(k);
  181.     else
  182.         ship::input();
  183.     cout << "Введите водоизмещение: ";
  184.     cin >> displacement;
  185.     cout << "Введите численность авиакрыла: ";
  186.     cin >> num_of_wings;
  187.     cout << "Введите вооружение: ";
  188.     cin >> weapons;
  189.  
  190. }
  191.  
  192.  
  193. destroyer::~destroyer()
  194. {
  195.    // cout << "Deleted - " << Show() << endl;
  196. }
  197.  
  198. void destroyer::Show()
  199. {
  200.     cout <<" destroyer  ";
  201.     ship::Show();
  202.     cout << " | displacement: "    << internal << setw(10) << displacement
  203.          << " | Вооружение: "    << internal << setw(10) << weapons;
  204.  
  205.  
  206. }
  207. void destroyer::ReadFromFile(ifstream &f)
  208. {
  209.     f>> name >> tactical_number >> speed >> readiness >> displacement >> weapons >> resource_DU;
  210. }
  211. void destroyer::WriteToFile(ofstream &f)
  212. {
  213.     f <<"destroyer " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << displacement << " " << weapons << " " << resource_DU << endl;
  214.  
  215. }
  216. void destroyer::input(int k)
  217. {
  218.     if (k == 1)
  219.         ship::input(k);
  220.     else
  221.         ship::input();
  222.     cout << "Введите водоизмещение: ";
  223.     cin >> displacement;
  224.     cout << "Введите вооружение: ";
  225.     cin >> weapons;
  226.  
  227. }
  228.  
  229. submarine::~submarine()
  230. {
  231.     //cout << "Deleted - " << Show() << endl;
  232. }
  233.  
  234.  
  235. void submarine::Show()
  236. {
  237.     cout <<" submarine ";
  238.     ship::Show();
  239.     cout << " | Надводное водоизмещение: "    << internal << setw(10) << surface_displacement
  240.          << " | Подводное водоизмещение: "    << internal << setw(10) << underwater_displacement
  241.          << " | Вооружение: "    << internal << setw(10) << weapons
  242.          << " | Ост.ресурс корпуса: "    << internal << setw(10) << resource_body;
  243.  
  244.  
  245. }
  246. void submarine::ReadFromFile(ifstream &f)
  247. {
  248.     cout << " 143 ";
  249.     f>> name >> tactical_number >> speed >> readiness >> surface_displacement >> underwater_displacement >> weapons >> resource_DU >> resource_body;
  250. }
  251. void submarine::WriteToFile(ofstream &f)
  252. {
  253.     f <<"submarine " << name << " " << tactical_number << " " << speed <<" "<< readiness << " " << surface_displacement << " " << underwater_displacement << " " << weapons << " " << resource_DU << " " << resource_body <<endl;
  254.  
  255.  
  256. }
  257. void submarine::input(int k)
  258. {
  259.     if (k == 1)
  260.         ship::input(k);
  261.     else
  262.         ship::input();
  263.     cout << "Введите надводное водоизмещение: ";
  264.     cin >> surface_displacement;
  265.     cout << "Введите подводное водоизмещение: ";
  266.     cin >> underwater_displacement;
  267.     cout << "Введите вооружение: ";
  268.     cin >> weapons;
  269.     cout << "Введите ост.ресурс корпуса: ";
  270.     cin >> resource_body;
  271. }
  272.  
  273.  
  274. small_rocket_ship::~small_rocket_ship()
  275. {
  276.     //cout << "Deleted - " << Show() << endl;
  277. }
  278.  
  279.  
  280. void small_rocket_ship::Show()
  281. {
  282.     cout <<" small_rocket_ship ";
  283.     ship::Show();
  284.     cout << " | Водоизмещение: "    << internal << setw(10) << displacement
  285.          << " | Вооружение: "    << internal << setw(10) << weapons;
  286.  
  287.  
  288. }
  289. void small_rocket_ship::ReadFromFile(ifstream &f)
  290. {
  291.     f>> name >> tactical_number >> speed >> readiness >> resource_DU >> displacement >> weapons;
  292. }
  293. void small_rocket_ship::WriteToFile(ofstream &f)
  294. {
  295.     f <<"small_rocket_ship " << name << " " << tactical_number << " " << speed <<" "<< readiness <<  " " << resource_DU << " " << displacement << " " << weapons << endl;
  296.  
  297.  
  298. }
  299. void small_rocket_ship::input(int k)
  300. {
  301.     if (k == 1)
  302.         ship::input(k);
  303.     else
  304.         ship::input();
  305.     cout << "Введите водоизмещение: ";
  306.     cin >> displacement;
  307.     cout << "Введите вооружение: ";
  308.     cin >> weapons;
  309. }
  310.  
  311. int main()
  312. {
  313.     setlocale(LC_ALL, "Russian");
  314.     vector <ship *> vShip; // создаем вектор указателей
  315.     bool isProgramActive = true;
  316.     while (isProgramActive) // пока не поступило сигнала о завершении программы, выполняем код ниже
  317.     {
  318.         cout << "\n1. Добавить новый корабль\n"
  319.                 "2. Добавить корабль после ремонта\n"
  320.                 "3. Вывод корабля из состава (удаление)\n"
  321.                 "4. Показать корабельный состав с указанием боеготовности\n"
  322.                 "5. Показать список кораблей со скоростью движения не ниже заданной\n"
  323.                 "6. Вывод корабля с наименьшим остаточным ресурсом ДУ\n"
  324.                 "0. Выход\n"
  325.                 "9. Считать с файла\n"
  326.                 "10. Записать в файл\n"
  327.                     "\nВведите номер действия: ";
  328.         //156790
  329.         int action = -1;
  330.         cin >>action;
  331.         cout << endl;
  332.         switch (action)
  333.         {
  334.         case 0:
  335.         {
  336.             for ( int i = 0; i<vShip.size();i++)
  337.             {
  338.                 delete vShip[i];
  339.             }
  340.             isProgramActive = false;
  341.             break;
  342.         }
  343.         case 1:
  344.  
  345.         {
  346.             cout << "Введите тип судна (aerocarrier, destroyer, submarine, small_rocket_ship): ";
  347.             string type;
  348.             cin >> type;
  349.             ship *t=0;
  350.             if (type=="aerocarrier")t=new aerocarrier;
  351.             if (type=="destroyer")t=new destroyer;
  352.             if (type=="submarine") t=new submarine;
  353.             if (type=="small_rocket_ship") t=new small_rocket_ship;
  354.             t->input(0);
  355.             vShip.push_back(t);
  356.  
  357.             break;
  358.         }
  359.         case 2:
  360.         {
  361.             cout << "Введите тип судна (aerocarrier, destroyer, submarine, small_rocket_ship): ";
  362.             string type;
  363.             cin >> type;
  364.             ship *t=0;
  365.             if (type=="aerocarrier")t=new aerocarrier;
  366.             if (type=="destroyer")t=new destroyer;
  367.             if (type=="submarine") t=new submarine;
  368.             if (type=="small_rocket_ship") t=new small_rocket_ship;
  369.             t->input(1);
  370.             vShip.push_back(t);
  371.  
  372.             break;
  373.         }
  374.         case 3:
  375.         {
  376.             int id = -1;
  377.             while (id < 0)
  378.             {
  379.                 cout << "Введите порядковый номер: ";
  380.                 cin >> id;
  381.                 if (id >= vShip.size())
  382.                 id = -1;
  383.             }
  384.             cout << "Удалён: ";
  385.             vShip[id]->Show();
  386.             delete vShip[id];     // освобождаем дин.память
  387.             vShip.erase(vShip.begin() + id);  // удаляем указатель из вектора
  388.             break;
  389.         }
  390.         case 4:
  391.         {
  392.             for (int i = 0; i < vShip.size(); i++)
  393.             {
  394.                 cout << i << ". ";           // номер
  395.                 vShip[i]->Show();
  396.                 cout <<endl;
  397.              }
  398.             break;
  399.         }
  400.         case 5:
  401.         {
  402.             int k=0;
  403.             cout << "Введите скорость: ";
  404.             double speed;
  405.             cin >> speed;
  406.             for ( int i = 0; i < vShip.size(); i++)
  407.             {
  408.  
  409.                 if ( vShip[i]->check_speed(speed)==true)
  410.                 {
  411.                     cout << k << ". ";           // номер
  412.                     vShip[i]->Show();
  413.                     k++;
  414.                     cout <<endl;
  415.                 }
  416.  
  417.              }
  418.             break;
  419.         }
  420.         case 6:
  421.         {
  422.             double max =999999999;
  423.             int k = 0;
  424.             for (int i = 0; i<vShip.size(); ++i)
  425.             {
  426.                 if (vShip[i]->show_resource_DU() < max)
  427.                 {
  428.                     k = i;
  429.                     max = vShip[i]->show_resource_DU();
  430.                 }
  431.             }
  432.             if (max!=999999999)
  433.                 cout << " Наименьший остаточный ресурс у " << k << " корабля и он равен " << max << endl;
  434.             break;
  435.         }
  436.  
  437.  
  438.         case 9:
  439.         {
  440.             ifstream fin("E:\input.txt");
  441.             if(!fin.is_open())
  442.             {
  443.                 cout << "Файл отсутствует\n";
  444.                 break;
  445.             }
  446.             while (!fin.eof())
  447.             {
  448.                 string type;
  449.                 fin >> type;
  450.                 ship *t=0;
  451.                 if (type=="aerocarrier") t=new aerocarrier;
  452.                 if (type=="destroyer") t=new destroyer;
  453.                 if (type=="submarine") t=new submarine;
  454.                 if (type=="small_rocket_ship") t=new small_rocket_ship;
  455.                 if(t)
  456.                 {
  457.                     t->ReadFromFile(fin);
  458.                     vShip.push_back(t);
  459.                 }
  460.             }
  461.             break;
  462.         }
  463.         case 10:
  464.         {
  465.             ofstream fout("E:\input.txt");
  466.             for (int i = 0; i<vShip.size();++i)
  467.             {
  468.                 vShip[i]->WriteToFile(fout);
  469.             }
  470.             break;
  471.         }
  472.  
  473.  
  474.         }
  475.     }
  476. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement