Advertisement
Sofe1204

Untitled

May 13th, 2022
691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                               Online C++ Compiler.
  4.                Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <iostream>
  10. #include <cstring>
  11. #include <cmath>
  12. using namespace std;
  13.  
  14. // Online C++ compiler to run C++ program online
  15.  
  16.  
  17. class Clock
  18. {
  19.     private:
  20.     int hour;
  21.     int min;
  22.     int sec;
  23.     public:
  24.     Clock()
  25.     {
  26.         this->hour=0;
  27.         this->min=0;
  28.         this->sec=0;
  29.     }
  30.      Clock(int hour,int min,int sec)
  31.     {
  32.         this->hour=hour;
  33.         this->min=min;
  34.         this->sec=sec;
  35.     }
  36.  
  37.     void info()
  38.     {
  39.          cout<<hour<<":"<<min<<":"<<sec<<endl;
  40.     }
  41.     int miliseconds()
  42.     {
  43.         return 1000*(3600* this->hour + 60* this->min + this->sec);
  44.     }
  45.    
  46.      friend Clock operator+(const Clock &a,const Clock &b)
  47.     {
  48.         Clock c;
  49.         c.hour=a.hour+b.hour;
  50.         c.min=a.min+b.min;
  51.         c.sec=a.sec+b.sec;
  52.         return c;
  53.     }
  54.    
  55.       friend Clock operator-(const Clock &a,const Clock &b)
  56.     {
  57.         Clock c;
  58.         c.hour=a.hour-b.hour;
  59.         c.min=a.min-b.min;
  60.         c.sec=a.sec-b.sec;
  61.         return c;
  62.     }
  63.     friend ostream &operator<<(ostream &o,const Clock &c)
  64.     {
  65.              o<<c.hour<<":"<<c.min<<":"<<c.sec<<endl;
  66.         return o;
  67.     }
  68.    
  69.     int getHour()
  70.     {
  71.         return hour;
  72.     }
  73.     int getMin()
  74.     {
  75.         return min;
  76.     }
  77.     int getSec()
  78.     {
  79.         return sec;
  80.     }
  81.     void setHours(int h)
  82.     {
  83.         this->hour=h;
  84.     }
  85.     void setMin(int m)
  86.     {
  87.         this->min=m;
  88.     }
  89.     void setSec(int s)
  90.     {
  91.         this->sec=s;
  92.     }
  93.    
  94.    
  95.     ~Clock(){}
  96. };
  97.  
  98. void rectify(Clock &c)
  99. {
  100.     if(c.getSec()>=60)
  101.     {
  102.         c.setSec(c.getSec()-60);
  103.         c.setMin(c.getMin()+1);
  104.     }
  105.     else if(c.getMin()>=60)
  106.     {
  107.         c.setMin(c.getMin()-60);
  108.         c.setHours(c.getHour()+1);
  109.     }
  110.     else if(c.getHour()>=12)
  111.     {
  112.         c.setHours(c.getHour()-12);
  113.     }
  114.     else if(c.getSec()<0)
  115.     {
  116.         c.setSec(c.getSec()+60);
  117.         c.setMin(c.getMin()-1);
  118.     }
  119.     else if(c.getMin()<0)
  120.     {
  121.         c.setMin(c.getMin()+60);
  122.         c.setHours(c.getHour()-1);
  123.     }
  124.     else if(c.getHour()<0)
  125.     {
  126.         c.setHours(c.getHour()+12);
  127.     }
  128.    
  129. }
  130. int main() {
  131.  
  132.     int hour,min,sec;
  133.    
  134.     cin>>hour>>min>>sec;
  135.     Clock a(hour,min,sec);
  136.     rectify(a);
  137.    
  138.     cin>>hour>>min>>sec;
  139.     Clock b(hour,min,sec);
  140.     rectify(b);
  141.    
  142.     a.info();
  143.     cout<<a.miliseconds()<<endl;
  144.     b.info();
  145.     cout<<b.miliseconds()<<endl;
  146.     return 0;
  147. }
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement