algorithm asistent!

hi,im trying to solve as many exersizes as i can
and iv stoped in the last part of the aquestion i couldnt develop the algorithm
for it.may any one guide me
this is the question:
i have class call dateType
there are many operations shuch :
1-return number of days of the month
2-return number of days passed in thr year for agiven date.
3-return number of days remaning in thr year for agiven date.
4-calculating the new date by adding a fixed number of days
exp:3-18-2009 + 25 days will be 4-12-2009...


the forth function is the part were i got stucked in..
If you create a separate method for each section of the date then you can just pass a number to one function to add it to the date.

1
2
3
4
void IncreaseDays(int num_oDays)
{
    itsDays += num_oDays;
}


If you wanted to actually use the + operator, then you would have to do some operator overloading: http://www.parashift.com/c++-faq-lite/operator-overloading.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int day, month, year;//note that counting starts from 0 here
int increase;

day += increase;
while(day >= days_in_month(month)){//may happen if increase > 0
    day -= days_in_month(month);
    month++;
}
while(day < 0){//may happen if increase < 0
    month--;
    day += days_in_month(month);
}
while(month >= 12){//I do the same here. number of months in year is constant,
    // so I could do this with % and /, but I figured this way it would be more clear
    //what happened with days.
    month -= 12;
    year++;
}
while(month < 0){
    year--;
    month += 12;
}
Topic archived. No new replies allowed.