hpnq

AntonGandon

Nov 13th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <windows.h>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. class celestialBody {
  10. protected:
  11.     string name; //Название
  12.     float maxtemp, age;//Максимальная температура и возраст
  13. public:
  14.     celestialBody();  // конструктор без параметров
  15.     celestialBody(string _name, float _maxtemp, float _age);// конструктор инициализации
  16.     virtual void print();
  17.     string getName();
  18.     float getMaxTemp();
  19.     float getAge();
  20.     virtual ~celestialBody();  //  деструктор
  21.     virtual string get_t(){
  22.         return "0";
  23.     }
  24. };
  25.  
  26. celestialBody::celestialBody()
  27. {
  28.     name = "";
  29.     maxtemp =0;
  30.     age = 0;
  31. }
  32.  
  33. celestialBody::celestialBody(string _name, float _maxtemp, float _age) : name(_name), maxtemp(_maxtemp), age(_age)
  34. {}
  35.  
  36. void celestialBody::print()
  37. {
  38.     //cout<<left<<setw(20) << "Название " <<setw(12)<< "Температура" <<setw(7) << "Возраст"<< endl;
  39.     cout<<left<<fixed<<setw(20)<<name<<setw(12)<<setprecision(2)<<maxtemp << setw(7) <<setprecision(2)<<age;
  40. }
  41.  
  42.  
  43. string celestialBody::getName()
  44. {
  45.     return name;
  46. }
  47.  
  48.  
  49. float celestialBody::getMaxTemp()
  50. {
  51.     return maxtemp;
  52. }
  53.  
  54.  
  55. float celestialBody::getAge()
  56. {
  57.     return age;
  58. }
  59.  
  60. celestialBody::~celestialBody()
  61. {
  62.     cout << "Объект удален из памяти!" << endl;
  63. }
  64.  
  65. class cometa : public celestialBody{
  66.     string type;
  67.     float dia, period, ecc;
  68. public:
  69.     cometa(): type(""), dia(0), period(0), ecc(0){}
  70.  
  71.     cometa(string _name, float _maxtemp, float _age, string _type, float _dia, float _period, float _ecc): celestialBody(_name, _maxtemp, _age) {
  72.         dia = _dia;
  73.         period = _period;
  74.         ecc = _ecc;
  75.     }
  76.  
  77.     void print(){
  78.         celestialBody::print();
  79.         cout << "WITH: " << dia << " " << period << " " << ecc << "\n";
  80.     }
  81.     string get_t(){
  82.         return type;
  83.     }
  84.     float get_p(){
  85.         return period;
  86.     }
  87.  
  88.     ~cometa(){
  89.         cout << "object destroyed \n";
  90.     }
  91. };
  92.  
  93. class Container {
  94.     celestialBody** arr;
  95.     int n;
  96. public:
  97.     Container() :arr(nullptr), n(0) {}
  98.  
  99.     Container(string filename) {
  100.         /*freopen(filename.c_str(), "r", stdin);
  101.         ofstream cout(filename.c_str(), std::ios::out);*/
  102.         ifstream in(filename.c_str(), ios::in);
  103.  
  104.  
  105.         in >> this->n;
  106.         arr = new celestialBody * [this->n];
  107.         cout << "n " << n << endl;
  108.         for (int i = 0; i < this->n; i++) {
  109.             string s;
  110.             in >> s;
  111.  
  112.             if (s == "тело") {
  113.                 string name;
  114.                 float a, b;
  115.                 in >> name >> a >> b;
  116.  
  117.                 arr[i] = new celestialBody(name, a, b);
  118.  
  119.             }
  120.             else if (s == "комета") {
  121.                 string name, tp;
  122.                 float a, b, c, d,  e;
  123.                 //int d;
  124.                 in >> name >> a >> b >> c >> d >> e;
  125.  
  126.                 arr[i] = new cometa(name, a, b, tp, c, d, e);
  127.             }
  128.         }
  129.  
  130.  
  131.     }
  132.     ~Container() {
  133.         for (int i = 0; i < this->n; i++) {
  134.             delete arr[i];
  135.         }
  136.         delete[]arr;
  137.     }
  138.     //2
  139.     void printData() {
  140.         for (int i = 0; i < this->n; i++) {
  141.             arr[i]->print();
  142.         }
  143.     }
  144.     //3
  145.     double f3(char startW) {
  146.         float mnt = -10000;
  147.         for (int i = 0; i < this->n; i++) {
  148.             if (arr[i]->getName()[0] == startW) {
  149.                 mnt = min(arr[i]->getMaxTemp(), mnt);
  150.             }
  151.         }
  152.         return mnt ;
  153.     }
  154.     //4 // most fr type cometa
  155.     string f4() {
  156.         float a[3];
  157.         a[0] = 0;
  158.         a[1] = 0;
  159.         a[2] = 0;
  160.         for(int i = 0; i < n; i++){
  161.             if(arr[i]->get_t() == "0"){
  162.                 continue;
  163.             }
  164.  
  165.  
  166.             if(arr[i]->get_t() == "короткопериодическая"){
  167.                 a[0] +=1;
  168.             }
  169.             if(arr[i]->get_t() == "долгопериодическая"){
  170.                 a[1] +=1;
  171.             }
  172.             if(arr[i]->get_t() == "межзвездная"){
  173.                 a[2] +=1;
  174.             }
  175.         }
  176.         float mx = max(a[0], max(a[1], a[2]));
  177.         if(a[0] == mx){
  178.             cout << "короткопериодическая\n";
  179.             return "короткопериодическая";
  180.         }
  181.         if(a[1] == mx){
  182.             cout << "долгопериодическая\n";
  183.             return "долгопериодическая";
  184.         }
  185.         if(a[2] == mx){
  186.             cout << "долгопериодическая\n";
  187.             return "долгопериодическая";
  188.         }
  189.     }
  190.  
  191. };
  192.  
  193.  
  194.  
  195.  
  196. int main() {
  197.     setlocale(LC_ALL, "rus");
  198.     //SetConsoleCP(1251);
  199.     string fn = "variant7.txt";
  200.     Container c(fn);
  201.  
  202.     c.printData();
  203.     char st;
  204.     cin >> st;
  205.     cout << "f3: " << c.f3(st) << "\n";
  206.  
  207.     c.f4();
  208.  
  209.     return 0;
  210. }
  211.  
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment