Advertisement
happyguy89

task 0.0

Jul 13th, 2021
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. //times1.cpp
  2. //converts from time24 to time12 using operator in time24
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. ////////////////////////////////////////////////////////////////
  7. class time12
  8. {
  9. private:
  10.     bool pm; //true = pm, false = am
  11.     int hrs; //1 to 12
  12.     int mins; //0 to 59
  13. public: //no-arg constructor
  14.     time12() : pm(true), hrs(0), mins(0)
  15.     { }
  16. //3-arg constructor
  17.     time12(bool ap, int h, int m) : pm(ap), hrs(h), mins(m)
  18.     { }
  19.     void display() const //format: 11:59 p.m.
  20.     {
  21.         cout << hrs << ":";
  22.              if(mins < 10)
  23.                  cout << "0"; //extra zero for “01”
  24.         cout << mins << " " ;
  25.         string am_pm = pm ? "p.m." : "a.m.";
  26.         cout << am_pm;
  27.     }
  28. };
  29. ////////////////////////////////////////////////////////////////
  30. class time24
  31. {
  32. private:
  33.     int hours; //0 to 23
  34.     int minutes; //0 to 59
  35.     int seconds; //0 to 59
  36. public: //no-arg constructor
  37.     time24() : hours(0), minutes(0), seconds(0)
  38.     { }
  39.     time24(int h, int m, int s) : //3-arg constructor
  40.         hours(h), minutes(m), seconds(s)
  41.     { }
  42.     void display() const //format: 23:15:01
  43.     {
  44.         if(hours < 10) cout << "0";
  45. cout << hours << ": ";
  46.         if(minutes < 10) cout << "0";
  47. cout << minutes << ":";
  48.         if(seconds < 10) cout << "0";
  49.         cout << seconds;
  50.     }
  51.     operator time12() const; //conversion operator
  52. };
  53. //--------------------------------------------------------------
  54. time24::operator time12() const //conversion operator
  55. {
  56.     int hrs24 = hours;
  57.     bool pm = hours < 12 ? false : true; //find am/pm
  58. //round secs
  59.     int roundMins = seconds < 30 ? minutes : minutes+1;
  60.     if(roundMins == 60) //carry mins?
  61.     {
  62.         roundMins=0;
  63.         ++hrs24;
  64.         if(hrs24 == 12 || hrs24 == 24) //carry hrs?
  65.             pm = (pm==true) ? false : true; //toggle am/pm
  66.     }
  67.     int hrs12 = (hrs24 < 13) ? hrs24 : hrs24-12;
  68.     if(hrs12==0) //00 is 12 a.m.
  69.     {
  70.         hrs12=12;
  71.         pm=false;
  72.     }
  73.     return time12(pm, hrs12, roundMins);
  74. }
  75. ////////////////////////////////////////////////////////////////
  76. int main()
  77. {
  78.     int h, m, s;
  79.     while(true)
  80.     {
  81.         //get 24-hr time from user
  82. cout << "Enter 24-hour time:\n";
  83.         cout << " Hours (0 to 23): ";
  84.         cin >> h;
  85.         if(h > 23) //quit if hours > 23
  86.             return(1);
  87. cout << " Minutes:";
  88.         cin >> m;
  89. cout << " Seconds:";
  90.         cin >> s;
  91.         time24 t24(h, m, s); //make a time24
  92. cout << "You entered:"; //display the time24
  93.         t24.display();
  94.         time12 t12 = t24; //convert time24 to time12
  95. cout << "\n12-hour time:"; //display equivalent time12
  96.         t12.display();
  97.         cout << "\n\n";
  98.     }
  99.     return 0;
  100. }
  101.  
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement