Advertisement
skb50bd

Time24

Jun 17th, 2015
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. class Time24{
  7. private:
  8.     int hh, mm, ss;
  9. public:
  10.     Time24(): hh(00), mm(00), ss(00) {}
  11.     Time24(int h, int m, int s): hh(h), mm(m), ss(s) {}
  12.     ~Time24() {}
  13.  
  14.     int getH() {return hh;}
  15.     int getM() {return mm;}
  16.     int getS() {return ss;}
  17.     void showData();
  18.     Time24 operator ++ ();
  19.     Time24 operator ++ (int);
  20.     Time24 operator -- ();
  21.     Time24 operator -- (int);
  22.     Time24 operator + (Time24 A);
  23.     Time24 operator - (Time24 A);
  24.     bool operator > (Time24 A);
  25.     bool operator < (Time24 A);
  26.     bool operator == (Time24 A);
  27.     bool operator != (Time24 A);
  28. };
  29.  
  30.  
  31. int main (){
  32.     Time24 t1(15, 0, 0), t2(14, 59 , 59);
  33.  
  34.     ++t1;
  35.     t2--;
  36.  
  37.     Time24 t3 = t1 + t2;
  38.     Time24 t4 = t1 - t2;
  39.  
  40.     t1.showData();
  41.     t2.showData();
  42.     t3.showData();
  43.     t4.showData();
  44.  
  45.     if(t3 != t4) cout << "t3 is not equal to t4" << endl;
  46.     if(t3 < t4) cout << "t3 is smaller than t4" << endl;
  47.     else if(t3 > t4) cout << "t3 is larger than t4" << endl;
  48.     else cout << "t3 is equal to t4" << endl;
  49.  
  50.     return 0;
  51. }
  52.  
  53. void Time24::showData() {
  54.     cout << setw(2) << hh << ":" << setw(2) << mm << ":" << setw(2) << ss << endl;
  55.     return;
  56. }
  57.  
  58. Time24 Time24::operator ++ () {
  59.     Time24 temp;
  60.     temp.hh = ++hh;
  61.     temp.mm = mm;
  62.     temp.ss = ss;
  63.  
  64.     return temp;
  65. }
  66.  
  67. Time24 Time24::operator ++ (int) {
  68.     Time24 temp;
  69.     temp.hh = ++hh;
  70.     temp.mm = mm;
  71.     temp.ss = ss;
  72.  
  73.     return temp;
  74. }
  75.  
  76. Time24 Time24::operator -- () {
  77.     Time24 temp;
  78.     temp.hh = --hh;
  79.     temp.mm = mm;
  80.     temp.ss = ss;
  81.  
  82.     return temp;
  83. }
  84.  
  85. Time24 Time24::operator -- (int) {
  86.     Time24 temp;
  87.     temp.hh = --hh;
  88.     temp.mm = mm;
  89.     temp.ss = ss;
  90.  
  91.     return temp;
  92. }
  93.  
  94. Time24 Time24::operator + (Time24 A){
  95.     Time24 temp;
  96.  
  97.     if(A.ss + ss < 60)
  98.         temp.ss = A.ss + ss;
  99.     else {
  100.         mm++;
  101.         temp.ss = A.ss + ss - 60;
  102.     }
  103.  
  104.     if(A.mm + mm < 60)
  105.         temp.mm = A.mm + mm;
  106.     else {
  107.         hh++;
  108.         temp.mm = A.mm + mm - 60;
  109.     }
  110.  
  111.     temp.hh = A.hh + hh;
  112.  
  113.     return temp;
  114. }
  115.  
  116. Time24 Time24::operator - (Time24 A){
  117.     Time24 temp;
  118.  
  119.     if(ss - A.ss >= 0)
  120.         temp.ss = ss - A.ss;
  121.     else {
  122.         mm--;
  123.         temp.ss = ss + 60 - A.ss;
  124.     }
  125.  
  126.     if(mm - A.mm >= 00)
  127.         temp.mm = mm - A.mm;
  128.     else {
  129.         hh--;
  130.         temp.mm = mm + 60 - A.mm;
  131.     }
  132.  
  133.     temp.hh = hh - A.hh;
  134.  
  135.     return temp;
  136. }
  137.  
  138. bool Time24::operator > (Time24 A) {
  139.     if(hh > A.hh) return true;
  140.     else if(hh < A.hh) return false;
  141.     if(mm > A.mm) return true;
  142.     else if(mm < A.mm) return false;
  143.     if(ss > A.ss) return true;
  144.     else if(ss < A.ss) return false;
  145. }
  146.  
  147. bool Time24::operator < (Time24 A) {
  148.     if(hh < A.hh) return true;
  149.     else if(hh > A.hh) return false;
  150.     if(mm < A.mm) return true;
  151.     else if(mm > A.mm) return false;
  152.     if(ss < A.ss) return true;
  153.     else if(ss > A.ss) return false;
  154. }
  155.  
  156. bool Time24::operator == (Time24 A) {
  157.     if(hh == A.hh && mm == A.mm && ss == A.ss) return true;
  158.     else return false;
  159. }
  160.  
  161. bool Time24::operator != (Time24 A) {
  162.     if(hh == A.hh && mm == A.mm && ss == A.ss) return false;
  163.     else return true;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement