MaximTakkaTo

Untitled

Nov 20th, 2020
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. class Clock
  6. {
  7. protected:
  8.     unsigned int hh;
  9.     unsigned int mm;
  10.     unsigned int ss;
  11.     bool isSummer;
  12.     int dailyseconds;
  13.     std::string time;
  14.  
  15.     void SetTime(int deltaseconds)
  16.     {
  17.         unsigned int currtime = this->hh * 3600 + this->mm * 60 + this->ss;
  18.         int newtime = (currtime + deltaseconds);
  19.         if (newtime > this->dailyseconds)
  20.             newtime -= this->dailyseconds * int(newtime / this->dailyseconds);
  21.         if (newtime < 0)
  22.             newtime += this->dailyseconds * (int(std::abs(newtime) / this->dailyseconds) + 1);
  23.         this->hh = int(newtime / 3600);
  24.         this->mm = int((newtime - this->hh * 3600) / 60);
  25.         this->ss = newtime - this->hh * 3600 - this->mm * 60;
  26.         time = "";
  27.         if (hh < 10)
  28.         {
  29.             time += "0" + std::to_string(hh);
  30.         }
  31.         else
  32.         {
  33.             time += std::to_string(hh);
  34.         }
  35.         time += ":";
  36.         if (mm < 10)
  37.         {
  38.             time += "0" + std::to_string(mm);
  39.         }
  40.         else
  41.         {
  42.             time += std::to_string(mm);
  43.         }
  44.         time += ":";
  45.         if (ss < 10)
  46.         {
  47.             time += "0" + std::to_string(ss);
  48.         }
  49.         else
  50.         {
  51.             time += std::to_string(ss);
  52.         }
  53.         std::cout << "Время успешно изменено" << std::endl;
  54.     }
  55. public:
  56.     std::string GetTime() { return time; }
  57.     virtual void PrintTime() = 0;
  58.     virtual void TransferToSummer()
  59.     {
  60.         std::cout << "Время успешно переведено на летнее" << std::endl;
  61.         this->isSummer = !this->isSummer;
  62.     }
  63.     virtual void TransferToWinter()
  64.     {
  65.         std::cout << "Время успешно переведено на зимнее" << std::endl;
  66.         this->isSummer = !this->isSummer;
  67.     }
  68.     Clock()
  69.     {
  70.         this->time = "00:00:00";
  71.         this->hh = 0;
  72.         this->mm = 0;
  73.         this->ss = 0;
  74.         this->isSummer = false;
  75.     }
  76. };
  77.  
  78. class MechanicalClock: protected Clock
  79. {
  80. public:
  81.     void RotateMinuteArrow(float angleValue)
  82.     {
  83.         std::cout << "Изменение времени с помщью поворота минутной стрелки часов на " << angleValue << " градус(ов)" << std::endl;
  84.         SetTime(angleValue * 10);
  85.     }
  86.     void RotateHourArrow(float angleValue)
  87.     {
  88.         std::cout << "Изменение времени с помщью поворота часовой стрелки часов на " << angleValue << " градус(ов)" << std::endl;
  89.         SetTime(angleValue * 120);
  90.     }
  91.     void PrintTime() override
  92.     {
  93.         std::cout << "Механические часы показывают время: " << this->time << std::endl;
  94.     }
  95.     void TransferToSummer() override
  96.     {
  97.         if (!this->isSummer)
  98.         {
  99.             this->RotateMinuteArrow(360);
  100.             Clock::TransferToSummer();
  101.         }
  102.     }
  103.     void TransferToWinter() override
  104.     {
  105.         if (isSummer)
  106.         {
  107.             this->RotateMinuteArrow(-360);
  108.             Clock::TransferToWinter();
  109.         }
  110.     }
  111.  
  112.     MechanicalClock() : Clock()
  113.     {
  114.         this->dailyseconds = 43200;
  115.     }
  116. };
  117.  
  118. class DigitalClock : protected Clock
  119. {
  120. public:
  121.     void IncSecond()
  122.     {
  123.         std::cout << "Изменение времени с помощью нажатия на кнопку увеличения секунд: " << std::endl;
  124.         SetTime(1);
  125.     }
  126.     void IncMinute()
  127.     {
  128.         std::cout << "Изменение времени с помощью нажатия на кнопку увеличения минут: " << std::endl;
  129.         SetTime(60);
  130.     }
  131.     void IncHour()
  132.     {
  133.         std::cout << "Изменение времени с помощью нажатия на кнопку увеличения часов: " << std::endl;
  134.         SetTime(3600);
  135.     }
  136.     void PrintTime() override
  137.     {
  138.         std::cout << "Цифровые часы показывают время: " << this->time << std::endl;
  139.     }
  140.     void TransferToSummer() override
  141.     {
  142.         if (!this->isSummer)
  143.         {
  144.             this->IncHour();
  145.             Clock::TransferToSummer();
  146.         }
  147.     }
  148.     void TransferToWinter() override
  149.     {
  150.         if (this->isSummer)
  151.         {
  152.             for (int i = 0; i < 23; ++i)
  153.                 this->IncHour();
  154.             Clock::TransferToSummer();
  155.         }
  156.     }
  157.     DigitalClock() : Clock()
  158.     {
  159.         this->dailyseconds = 86400;
  160.     }
  161. };
  162.  
  163.  
  164. int main()
  165. {
  166.     system("chcp 1251");
  167.     DigitalClock dc = DigitalClock();
  168.     dc.IncHour();
  169.     dc.TransferToSummer();
  170.     dc.TransferToWinter();
  171.     dc.PrintTime();
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment