/******************************************************************* C201 Computer Programming II Fall 2006 Dana Vrajitoru & A small program doing time conversions using enumerated types and structs. ********************************************************************/ #include #include using namespace std; // An enumeerated type defining the constants AM and PM for the // standard time notation. enum AMPM { AM, PM }; // A structure representing standard time. struct Time_str { int hours, minutes; AMPM suffix; }; // Prototypes. void increment(Time_str ¤t); void add(Time_str ¤t, int h, int m); void convert(int military, Time_str ¤t); void output(Time_str current); int main() { Time_str now; int military, hr, min; // Input-convert-output a time value. cout << "Enter a time value in military notation as 4 digits." << endl; cin >> military; convert(military, now); cout << "The time you entered converts to standard notation as:" << endl; output(now); // Increment the value by a given number of hours and minutes. cout << "We'll add a number of hours and minutes to this value" << endl << "Enter the hours" << endl; cin >> hr; cout << "Enter the minutes" << endl; cin >> min; add(now, hr, min); cout << "The result of the addition is:" << endl; output(now); return 0; } // Increments the current time by one minute. // To be completed by the student. void increment(Time_str ¤t) { if (current.minutes < 59) current.minutes++; else { // To be completed by the student. current.minutes = 0; // Update the hours and the suffix. } } // Adds a number of hours and minutes to the current time. // It should call the function increment a number of times equal to // the number of minutes plus 60 times the number of hours. // To be completed by the student. void add(Time_str ¤t, int h, int m) { } // Converts the time from military notation to standard notation. // To be completed by the student. void convert(int military, Time_str ¤t) { current.minutes = military % 100; int military_hr = military/100; if (military_hr == 0) { current.hours = 12; current.suffix = AM; } // This part must be completed by the student. else { // Some default values to be able to compile. current.hours = 0; current.suffix = AM; } } // Outputs the current time. void output(Time_str current) { cout << current.hours << ":" << setw(2) << setfill('0') << current.minutes << ' '; if (current.suffix == AM) cout << "am" << endl; else cout << "pm" << endl; } /******************* Example of output *************** Enter a time value in military notation as 4 digits. 0015 The time you entered converts to standard notation as: 12:15 am We'll add a number of hours and minutes to this value Enter the hours 1 Enter the minutes 12 The result of the addition is: 12:15 am *******************************************************/