operator- in Date Class
-
Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much
-
Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much
In some programs they convert the date to a number. (eg. like CTime class) then you can detect real easy is bigger or smaller and just substract or add them. I would take in account that in the year 2004 february has 29 days... leap years. good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimmy Hendrix
-
Hello: I am writting a Date Class, and I want to overload operator - that can take a Date object, I wrote the code, but I felt it is stupid, Please help me: *************************************************************************** int Date::days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int Isbigger(int year, int right_year) { int bigger; bigger = year > right_year?year:right_year; returen bigger; }//get the bigger value of two int Issmaller(int year, int right_year) { int smaller; smaller = year < right_year? year:right_year; return smaller; }//get the smaller value of two int Date::operator -(Date right) { int subday; int subyear; if (year == right.year) { subyear = 0; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } } else //not in the same year { int yearDifference; yearDifference = Isbigger(year,right.year) - 1; subyear = yearDifference * 365; if(month == right.month) { subday = Isbigger(day, right.day) - Issmaller(day,right.day); //if the same year and same month, just subtract the date; } else if(month != right.month) //if same year different month, { subday = (days[month] - day) + right.day); //get the days in beginning and ending month, for(i = (Isbigger(day,right.day) - 1); i >issmaller(day,right.day) ; i++) { subday += days[i]; //then plus the intermedia month, } } subday += subyear; } return subday; } Thank You So Much
Hello, This mightn't be exactly what your asking but you should make the '-' operator a 'friend' rather than a class member. It means you can use it like the built-in subtraction operator instead of call a method. See http://guinness.cs.stevens-tech.edu/~asatya/courses/cs765IT/C++\_overview/lect10\_1.pdf John
-
In some programs they convert the date to a number. (eg. like CTime class) then you can detect real easy is bigger or smaller and just substract or add them. I would take in account that in the year 2004 february has 29 days... leap years. good luck. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimmy Hendrix
Hello, Think it goes something like this, but you should check on web ... if year is divisible by 4 then ....if year is not divisible by 100 then .........it's a leap year ....else .........if year is divisible by 400 then ...............it's a leap year .........endif ....endif endif John