toanalien

CTime.cpp

Apr 25th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include "CTime.h"
  3.  
  4. using namespace std;
  5.  
  6. CTime::CTime()
  7. {
  8. }
  9.  
  10. CTime::CTime(int gio_i, int phut_i, int giay_i)
  11. {
  12.     this->gio_i_ = gio_i;
  13.     this->phut_i_ = phut_i;
  14.     this->giay_i_ = giay_i;
  15. }
  16.  
  17. CTime::~CTime()
  18. {
  19. }
  20.  
  21. void CTime::nhap(int gio_i, int phut_i, int giay_i)
  22. {
  23.     this->gio_i_ = gio_i;
  24.     this->phut_i_ = phut_i;
  25.     this->giay_i_ = giay_i;
  26. }
  27.  
  28. void CTime::xuat()
  29. {
  30.     cout << gio_i_ << " gio, " << phut_i_ << " phut, " << giay_i_ << " giay." << endl;
  31. }
  32.  
  33. int CTime::covert_to_sec()
  34. {
  35.     int time_i;
  36.     time_i = this->gio_i_ * 3600 + this->phut_i_ * 60 + this->giay_i_;
  37.     return time_i;
  38. }
  39.  
  40. void CTime::covert_to_time(int sec_i)
  41. {
  42.     int gio_i_ = 0;
  43.     int phut_i_ = 0;
  44.     int giay_i_ = 0;
  45.     gio_i_ = sec_i / 3600;
  46.     sec_i %= 3600;
  47.     phut_i_ = sec_i / 60;
  48.     giay_i_ = sec_i % 60;
  49.     this->nhap( gio_i_, phut_i_, giay_i_);
  50. }
  51.  
  52. void CTime::cong(int sec_i)
  53. {
  54.     this->covert_to_time(this->covert_to_sec() + sec_i);
  55. }
  56.  
  57. bool CTime::tru(int sec_i)
  58. {
  59.     if (this->covert_to_sec() - sec_i >= 0)
  60.     {
  61.         this->covert_to_time(this->covert_to_sec() - sec_i);
  62.         return 1;
  63.     }
  64.     return 0;
  65. }
  66.  
  67. CTimeSpan CTime::tru_to_span(CTime time)
  68. {
  69.     CTimeSpan span;
  70.     if (this->covert_to_sec() - time.covert_to_sec() >= 0)
  71.     {
  72.         span.covert_to_time_span(this -> covert_to_sec() - time.covert_to_sec());
  73.     }
  74.     return span;
  75. }
  76.  
  77. void CTime::cong1giay()
  78. {
  79.     this->covert_to_time(this->covert_to_sec()+1);
  80. }
  81.  
  82. void CTime::tru1giay()
  83. {
  84.     this->covert_to_time(this->covert_to_sec() - 1);
  85. }
  86.  
  87. void nhap(CTime &time)
  88. {
  89.     int gio_i, phut_i, giay_i;
  90.     do
  91.     {
  92.         cout << "Nhap thoi gian (gio phut giay) : ";
  93.         cin >> gio_i >> phut_i >> giay_i;
  94.     } while (gio_i < 0 || phut_i < 0 || giay_i<0);
  95.  
  96.     if (giay_i > 60)
  97.     {
  98.         phut_i += giay_i / 60;
  99.         giay_i %= 60;
  100.  
  101.     }
  102.     if (phut_i > 60)
  103.     {
  104.         gio_i += phut_i / 60;
  105.         phut_i %= 60;
  106.     }
  107.     time.nhap(gio_i, phut_i, giay_i);
  108. }
  109.  
  110. int main()
  111. {
  112.     CTime a(1, 1, 1);
  113.     CTime b(24, 59, 59);
  114.     CTimeSpan c = b.tru_to_span(a);
  115.     c.xuat();
  116.     system("pause");
  117. }
Add Comment
Please, Sign In to add comment