I am trying towrite a C program which will print yearly calendars, given as input the year numbers. Input data will be taken from a text file, which will contain an unknown number of data lines, each containing a single integer giving a year,
I first created a function to find the first day of the year:
int find_jan_first
(int year) /* the input year, must be >= MIN_YEAR */
/*
Calculates the day of the week on which January 1 falls for given year.
We start the formula at 5, because January 1, 1582 was a Friday.
Then the day of the week is advanced by one for each year, adding the
number of included leap years, because in leap years January 1st
advances by 2 days and in regular years it advances by 1 day.
*/
{ /* find_jan_first */
int firstDayInJan; /* 0 is Sunday, 1 is Monday, ..., 6 is Saturday */
/* starting on January 1, 1582, add days to find the first day */
/* in January of the year chosen */
firstDayInJan = ((5 + (year - MIN_YEAR) +
count_leap_years(year)) % DAYS_PER_WEEK);
return (firstDayInJan);
} /* find_jan_first */
and also a function to find leap year:
int count_leap_years
(int year) /* user's choice of year, must be >= MIN_YEAR */
/*
Calculates the number of leap years since 1582 to the current year.
This function returns the numbers of leap years since the given year.
Count does not include the given year if it is a leap year.
*/
{ /* count_leap_years */
int leapYears; /* number of leap years to return */
int hundreds; /* number of years multiple of a hundred */
int fourHundreds; /* number of years multiple of four hundred */
/* determine number of years in interval that are a multiple of 4 */
leapYears = (year - (MIN_YEAR - 1)) / 4;
/* determine number of years in interval that are a multiple of 100 */
/* and subtract, because they are not leap years */
hundreds = (year - 1501) / 100;
leapYears -= hundreds;
/* determine number of years in interval that are a multiple of 400 */
/* and add these back in, since they are leap years */
fourHundreds = (year - 1201) / 400;
leapYears += fourHundreds;
return (leapYears);
} /* count_leap_years */
I know the MIN_Year is 1582
and I think that you would put in input by inFile = fopen (fileName, "r");.
What I am having trouble with is the actual loop to begin show the final product looks like this.
1985
January
S M T W T F S
--------------------
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
February
S M T W T F S
--------------------
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28