001: #include <iostream> 002: #include <iomanip> 003: #include <string> 005: using namespace std; 006: 007: #include "ccc_time.h" 008: 009: class Clock { 011: public: 012:/** 013: Constructs a clock that can tell the local time. 014: @param use_military true if the clock uses military format (21:05) and
flase if the clock uses "am/pm" format (9:05 pm) 015:*/ 016: Clock(bool use_military); 018:/** 019: Gets the location of this clock. 020: @return the location 021:*/ 022: string get_location() const; 024:/** 025: Gets the hours of this clock. 026: @return the hours, in military or am/pm format 027:*/ 028: int get_hours() const; 030:/** 031: Gets the minutes of this clock. 032: @return the minutes 033:*/ 034: int get_minutes() const; 036:/** 037: Checks whether this clock uses miltary format. 038: @return true if miltary format 039:*/ 040: bool is_military() const; 041: private: 042: bool military; 043: }; 044: 045: Clock::Clock(bool use_military) 046: { military = use_military; } 049: 050: string Clock::get_location() const 051: { return "Local"; } 054: 055: int Clock::get_hours() const 056: { Time now; 058: int hours = now.get_hours(); 059: if (military) return hours; 060: if (hours == 0) return 12; 062: else if (hours > 12) return hours - 12; 064: else return hours; 066: } 067: 068: int Clock::get_minutes() const 069: { Time now; 071: return now.get_minutes(); 072: } 073: 074: bool Clock::is_military() const 075: { return military; } 078: 079: int main() 080: { Clock clock1(true); 082: Clock clock2(false); 083: 084: bool more = true; 085: while (more) 086: { cout << "Military time: " 088: << clock1.get_hours() << ":" 089: << setw(2) << setfill('0')
090: << clock1.get_minutes()
091: << setfill(' ') << "\n"; 092: cout << "am/pm time: " 093: << clock2.get_hours() << ":" 094: << setw(2) << setfill('0')
095: << clock2.get_minutes()
096: << setfill(' ') << "\n"; 097: 098: cout << "Try again? (y/n) "; 099: string input; 100: getline(cin, input); 101: if (input != "y") more = false; 102: } 103: return 0; 104: }