// useful_oldies.h (c) 1998 Kari Laitinen void Date::get_distance_to( Date another_date, long& distance_in_days ) { distance_in_days = 0 ; if ( *this < another_date ) { while ( *this != another_date ) { another_date.decrement() ; distance_in_days ++ ; } } else { while ( *this != another_date ) { another_date.increment() ; distance_in_days ++ ; } } } bool is_earlier_than( Date& another_date ) ; bool is_later_than( Date& another_date ) ; bool Date::is_earlier_than( Date& another_date ) { if ( ( this_year < another_date.this_year ) || ( ( this_year == another_date.this_year ) && ( this_month < another_date.this_month ) ) || ( ( this_year == another_date.this_year ) && ( this_month == another_date.this_month ) && ( this_day < another_date.this_day ) ) ) { return true ; } else { return false ; } } bool Date::is_later_than( Date& another_date ) { if ( ( this_year > another_date.this_year ) || ( ( this_year == another_date.this_year ) && ( this_month > another_date.this_month ) ) || ( ( this_year == another_date.this_year ) && ( this_month == another_date.this_month ) && ( this_day > another_date.this_day ) ) ) { return true ; } else { return false ; } } void Date::get_day_of_week( char day_of_week_as_string[], int& day_index ) { // day_index will get a value in the range from 0 to 6, // 0 meaning Monday and 6 meaning Sunday. day_index = 0 ; Date known_date( 6, 10, 1997 ) ; // Oct. 6, 1997 is Monday. if ( *this < known_date ) { while ( *this != known_date ) { if ( day_index > 0 ) { day_index -- ; } else { day_index = 6 ; } known_date.decrement() ; } } else { while ( *this != known_date ) { if ( day_index < 6 ) { day_index ++ ; } else { day_index = 0 ; } known_date.increment() ; } } char* days_of_week[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" } ; strcpy( day_of_week_as_string, days_of_week[ day_index ] ) ; } #define SEARCHING_FIRST_BOUNDARY_CHARACTER 2 #define SEARCHING_SECOND_BOUNDARY_CHARACTER 3 #define STRING_SEARCH_IS_READY 4 #define STRING_SEARCH_NOT_SUCCESSFUL 5 // With the following function you can extract a string // which is part of another string. The extraction is conducted // according to boundary characters. You can specify between // which boundary characters the extraction should take place. // For example, in the following string // // char example_string[] = "xxx,yyy,zzz,aaa/bbb/ccc" ; // // you could extract the string "aaa" with the following // function call: // // extract_string_from_string( ',', '/', 2, // example_string, // extracted_string ) ; // // In the above function call, the third argument specifies // that the first two commas would be skipped. // // In the case that the specified boundary characters are not // found, and empty string is given as the extracted string. void extract_string_from_string( char first_boundary_character, char second_boundary_character, char number_of_boundary_characters_to_skip, char string_where_to_extract[], char extracted_string[] ) { int first_boundary_index = 0 ; int second_boundary_index = 0 ; int string_index = 0 ; int index_for_extracted_string ; int string_inspection_state ; if ( number_of_boundary_characters_to_skip > 0 ) { string_inspection_state = SKIPPING_BOUNDARY_CHARACTERS ; while ( string_inspection_state == SKIPPING_BOUNDARY_CHARACTERS ) { if ( string_where_to_extract[ string_index ] == first_boundary_character ) { number_of_boundary_characters_to_skip -- ; if ( number_of_boundary_characters_to_skip == 0 ) { string_inspection_state = SEARCHING_FIRST_BOUNDARY_CHARACTER ; } } else if ( string_where_to_extract[ string_index ] == STRING_END_INDICATOR ) { string_inspection_state = STRING_SEARCH_NOT_SUCCESSFUL ; } string_index ++ ; } } else { string_inspection_state = SEARCHING_FIRST_BOUNDARY_CHARACTER ; } while ( string_inspection_state == SEARCHING_FIRST_BOUNDARY_CHARACTER ) { if ( string_where_to_extract[ string_index ] == first_boundary_character ) { string_inspection_state = SEARCHING_SECOND_BOUNDARY_CHARACTER ; first_boundary_index = string_index ; } else if ( string_where_to_extract[ string_index ] == STRING_END_INDICATOR ) { string_inspection_state = STRING_SEARCH_NOT_SUCCESSFUL ; } string_index ++ ; } while ( string_inspection_state == SEARCHING_SECOND_BOUNDARY_CHARACTER ) { if ( string_where_to_extract[ string_index ] == second_boundary_character ) { string_inspection_state = STRING_SEARCH_IS_READY ; second_boundary_index = string_index ; } else if ( string_where_to_extract[ string_index ] == STRING_END_INDICATOR ) { string_inspection_state = STRING_SEARCH_NOT_SUCCESSFUL ; } string_index ++ ; } if ( string_inspection_state == STRING_SEARCH_NOT_SUCCESSFUL ) { extracted_string[ 0 ] = 0 ; // make it an empty string } else { string_index = first_boundary_index + 1 ; index_for_extracted_string = 0 ; while ( string_index < second_boundary_index ) { extracted_string[ index_for_extracted_string ] = string_where_to_extract[ string_index ] ; index_for_extracted_string ++ ; string_index ++ ; } extracted_string[ index_for_extracted_string ] = STRING_END_INDICATOR ; } } /***** char hit_any_key_to_continue() { cout << "\n Hit any key to continue. " ; char character_from_keyboard = getche() ; return character_from_keyboard ; } ****/ /***** int Date::number_of_days_in_this_month() { int number_of_days ; if ( this_month == 1 || this_month == 3 || this_month == 5 || this_month == 7 || this_month == 8 || this_month == 10 || this_month == 12 ) { number_of_days = 31 ; } else if ( this_month == 4 || this_month == 6 || this_month == 9 || this_month == 11 ) { number_of_days = 30 ; } else if ( this_is_a_leap_year() ) { number_of_days = 29 ; } else { number_of_days = 28 ; } return number_of_days ; } *****/ void Date::get_days_between( Date another_date, long& number_of_days ) { number_of_days = 0 ; if ( *this < another_date ) { while ( *this != another_date ) { another_date.decrement() ; number_of_days ++ ; } } else { while ( *this != another_date ) { another_date.increment() ; number_of_days ++ ; } } } void eat_white_space( istream& input_stream ) { char character_from_stream ; while ( input_stream.get( character_from_stream ) ) { if ( character_from_stream != ' ' && character_from_stream != '\t' && character_from_stream != '\n' && character_from_stream != '\r' && character_from_stream != '\f' ) { input_stream.putback( character_from_stream ) ; break ; } } } void ask_char( char char_asking_text[], char& char_from_user ) { cout << char_asking_text ; eat_white_space( cin ) ; cin >> char_from_user ; } void ask_int( char int_asking_text[], int& int_from_user ) { cout << int_asking_text ; eat_white_space( cin ) ; cin >> int_from_user ; } void ask_string( char string_asking_text[], char string_from_user[], int maximum_string_length ) { cout << string_asking_text ; eat_white_space( cin ) ; cin.getline( string_from_user, maximum_string_length ) ; }