| CODE |
// Times In a Day // By: Darwin Aguirre #include <iostream> using std::cout; using std::endl; #include <iomanip> using std::setfill; using std::setw; #include <cstdlib> #include <ctime> class Time { public: Time(); void setTime( int, int, int, int ); void printsetUp(); void printDate(); private: int month; int day; int hour; int minute; }; Time::Time() { month = day = 1; hour = minute = 0; } void Time::setTime( int m1, int d, int h, int m2 ) { month = ( m1 >= 0 && m1 <= 12 ) ? m1 : 0; day = ( d >= 1 && d <= 31 ) ? d : 0; hour = ( h >= 0 && h < 24 ) ? h : 0; minute = ( m2 >= 0 && m2 < 60 ) ? m2 : 0; } void Time::printsetUp() { cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) << minute; } void Time::printDate() { const char *month1[ 13 ] = { "WONT HAPPEN", "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "December" }; cout << "On " << month1[month] << '/' << day << "/92 at "; cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ( hour < 12 ? " AM" : " PM" ) << endl; } int main() { Time T; srand( time( 0 ) ); int m1; int d; int h; int m2; cout << "Base time without change is: "; T.printDate(); cout << "Base time changed to my birthday is: "; T.setTime( 8, 16, 19, 20 ); T.printDate(); cout << "Base time changed to randome is: "; m1 = ( 1 + rand() % 11 ); d = ( 1 + rand() % 30 ); h = ( rand() % 23 ); m2 = ( rand() % 59 ); T.setTime( m1, d, h, m2 ); T.printDate(); return 0; } |
| CODE |
// Times In a Day, User Input // By: Darwin Aguirre #include <iostream> using std::cout; using std::cin; using std::endl; #include <iomanip> using std::setfill; using std::setw; class Time { public: Time(); void setTime( int, int, int, int ); void printsetUp(); void printDate(); private: int month; int day; int hour; int minute; }; Time::Time() { month = day = 1; hour = minute = 0; } void Time::setTime( int m1, int d, int h, int m2 ) { month = ( m1 >= 1 && m1 <= 12 ) ? m1 : 0; day = ( d >= 1 && d <= 31 ) ? d : 0; hour = ( h >= 0 && h < 24 ) ? h : 0; minute = ( m2 >= 0 && m2 < 60 ) ? m2 : 0; } void Time::printsetUp() { cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) << minute; } void Time::printDate() { const char *month1[ 13 ] = { "WONT HAPPEN", "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "December" }; cout << "On " << month1[month] << '/' << day << "/92 at "; cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ( hour < 12 ? " AM" : " PM" ) << endl; } int main() { Time T; int m1; int d; int h; int m2; cout << "Base time without change is: "; T.printDate(); cout << "Base time changed to my birthday is: "; T.setTime( 8, 16, 19, 20 ); T.printDate(); cout << "\nNow Enter Your Time\n"; cout << "Enter month( number according to month ): "; cin >> m1; cout << "Enter day: "; cin >> d; cout << "Enter hour: "; cin >> h; cout << "Enter minute: "; cin >> m2; cout << "Base time changed to your settings are: "; T.setTime( m1, d, h, m2 ); T.printDate(); return 0; } |