📄 date.cc
字号:
/*********************************************************************** Gerald Carter cse 525 Spring 1994 date.cc DESCRIPTION : This file contains the class method definitions for the date class. All methods are documented separately.***********************************************************************/// INCLUDE FILES#include <iostream.h> // cout, cin, cerr, etc...#include <string.h> // strcmp()#include "logic.h" // boolean typedefs#include "date.h" // date class declaration// CONSTRUCTORS// each constructors simply assigns the integers used in construction// to the month, day and year fields of the object. The constructors // also perform bounds checking for the months and daysdate::date (int x, int y, int z){ if ((x < 1) || (x > 12)) { cout << "Illegal month.\n"; month = 1; } else month = x; switch (month) { case 1 : case 3 : case 5 : case 7 : case 9 : case 10 : case 12 : if ((y < 1) || (y > 31)) { cout << "Illegal day.\n"; day = 1; } else day = y; break; case 2 : if ((y < 1) || (y > 28)) { cout << "Illegal day.\n"; day = 1; } else day = y; break; case 4 : case 6 : case 8 : case 11 : if ((y < 1) || (y > 30)) { cout << "Illegal day.\n"; day = 1; } else day = y; break; default : cout << "Bad month.\n";} year = z;}; // end of date (int, int, int)date::date (const date& given_date){ month = given_date.month; day = given_date. day; year = given_date.year;}; // end of date (date&)/* DateToDay() ARGS : none RETURN : integer number of days represented by the month and day field of date object SIDE_EFFECTS : none*/int date::DateToDay (void){ int result; switch (month) { case 1 : result = day; break; case 2 : result = 31 + day; break; case 3 : result = 59 + day; break; case 4 : result = 90 + day; break; case 5 : result = 120 + day; break; case 6 : result = 151 + day; break; case 7 : result = 181 + day; break; case 8 : result = 212 + day; break; case 9 : result = 242 + day; break; case 10 : result = 273 + day; break; case 11 : result = 304 + day; break; case 12 : result = 334 + day; break; default : cout << "Bad month.\n";} return result;}; // end of DateToDay/* DayToDate() ARGS : num_days = date in current year in terms of days current_year = current_year RETURN : date object constructed by translating the num_days arg into month, day and year SIDE_EFFECTS : none*/date date::DayToDate (int num_days, int current_year){ int temp_month; if (num_days > 365) { ++current_year; num_days -= 365; } if (num_days > 334) return date (12, num_days - 334, current_year); else if (num_days > 304) return date (11, num_days -304, current_year); else if (num_days > 273) return date (10, num_days - 273, current_year); else if (num_days > 242) return date (9, num_days - 242, current_year); else if (num_days > 212) return date (8, num_days - 212, current_year); else if (num_days > 181) return date (7, num_days - 181, current_year); else if (num_days > 151) return date (6, num_days - 151, current_year); else if (num_days > 120) return date (5, num_days - 120, current_year); else if (num_days > 90) return date (4, num_days - 90, current_year); else if (num_days > 59) return date (3, num_days - 59, current_year); else if (num_days > 31) return date (2, num_days - 31, current_year); else return date (1, num_days, current_year);}; // end of DayToDate/* operator+ () ARGS : add_days = number of days to add to 'this' date RETURN : date object that is the sum of this and num_days SIDE_EFFECTS : none*/date date::operator+ (int add_days){ date temp; int num_days; num_days = DateToDay (); num_days += add_days; temp = DayToDate (num_days, year); return temp;}; // end of oprator+/* operator- () ARGS : sub_days = number of days to subtract from 'this' date RETURN : date object that is the difference of this and num_days SIDE_EFFECTS : none*/date date::operator- (int sub_days){ date temp; int num_days; num_days = DateToDay (); num_days -= sub_days; if (num_days < 0) num_days = -num_days; temp = DayToDate (num_days, year); return temp;}; // end of operator- (int) /* operator> () ARGS : cmp_date = date to compare 'this' to RETURN : integer that is 1 is the integer comparison is true and 0 otherwise SIDE_EFFECTS : none NOTE : The comparison is simply done by first comparing years. Then comparing months and finally days.*/int date::operator> (const date& cmp_date){ int result; if (year > cmp_date.year) result = TRUE; else if (year <= cmp_date.year) result = FALSE; else if (month > cmp_date.month) result = TRUE; else if (month <= cmp_date.month) result = FALSE; else if (day > cmp_date.day) result = TRUE; else result = FALSE; return result;}; // end of operator>/* operator< () ARGS : cmp_date = date to compare 'this' to RETURN : integer that is 1 is the integer comparison is true and 0 otherwise SIDE_EFFECTS : none NOTE : The comparison is simply done by first comparing years. Then comparing months and finally days.*/int date::operator< (const date& cmp_date){ int result; if (year > cmp_date.year) result = FALSE; else if (year < cmp_date.year) result = TRUE; else if (month >= cmp_date.month) result = FALSE; else if (month < cmp_date.month) result = TRUE; else if (day >= cmp_date.day) result = FALSE; else result = TRUE; return result;}; // end of operator</* operator== () ARGS : cmp_date = date to compare 'this' to RETURN : integer that is 1 is the integer comparison is true and 0 otherwise SIDE_EFFECTS : none NOTE : The comparison is simply done by first comparing years. Then comparing months and finally days.*/int date::operator== (const date& cmp_date){ if ((year == cmp_date.year) AND (month == cmp_date.month) AND (day == cmp_date.day)) return TRUE; else return FALSE;}; // end of operator==/* ChangDate () ARGS : x = new month y = new day z = new year RETURN : new date object with the values of x, y and z assigned to month, day and year respectively SIDE_EFFECTS : none*/date& date::ChangeDate (int x, int y, int z){ return *(new date(x, y, z));}; // end of ChangeDate;/* operator<< () ARGS : S = ostream to to print_date = date to be printed RETURN : reference to ostream that was put to SIDE_EFFECTS : print_date is put to stream S*/ostream& operator<< (ostream& S, const date& print_date){ char* month_string; switch (print_date.month) { case 1 : month_string = strdup ("January"); break; case 2 : month_string = strdup ("Febuary"); break; case 3 : month_string = strdup ("March"); break; case 4 : month_string = strdup ("April"); break; case 5 : month_string = strdup ("May"); break; case 6 : month_string = strdup ("June"); break; case 7 : month_string = strdup ("July"); break; case 8 : month_string = strdup ("August"); break; case 9 : month_string = strdup ("September"); break; case 10 : month_string = strdup ("October"); break; case 11 : month_string = strdup ("November"); break; case 12 : month_string = strdup ("December"); break; } return (S<<month_string<< " "<<print_date.day<<", "<<print_date.year);}; // end of operator<<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -