#include <stdio.h>
#include <string.h>
#define MIN_YEAR 1582
#define MAX_YEAR 9999
#define DAYS_PER_WEEK 7
#define MAX_FILE_NAME_SIZE 25
#define MAX_MONTH_NAME_SIZE 10
#define PREVIOUSDAYSPACE 3
//function declarations
int count_leap_years(int);
int find_jan_first(int);
int print_month(int, int, int);
void print_calendar(int);
void print_dayNames();
int main()
{
int year;
char fileName[MAX_FILE_NAME_SIZE];
FILE * inFile; //file pointer
printf("Please enter a date file: "); // user enters text file
scanf("%s",fileName);
inFile = fopen (fileName,"r");
while(!inFile) //error check if not valid file
{
printf("Try Again ");
printf("Please enter a date file: ");
scanf("%s",fileName);
inFile = fopen (fileName,"r");
}
while ( fscanf ( inFile, "%d", &year )!= EOF ) //error if date not in Gregorian calendar
{
if (year < MIN_YEAR || year > MAX_YEAR)
{
printf("\nCalendar may be incorrect,\n");
printf("the year may not exist in\n");
printf("the Gregorian Calendar range.\n");
}
print_calendar (year);
}
fclose (inFile);
}
void print_calendar(int year) //calling first day and year
{
int firstday; //first day of month
int month; // month of the year
printf("\n %d\n\n", year);
firstday = find_jan_first(year);
for(month= 1; month <= 12; month++)
{
firstday= print_month (month, firstday, year);
}
}
int print_month (int month, int day, int year) //length of month and name of month
{
int days, numspacefirst, spaces, datetoprint;
char monthName[MAX_MONTH_NAME_SIZE];
switch(month)
{
case 1:
days = 31;
strcpy(monthName,"January");
break;
case 3:
days = 31;
strcpy(monthName,"March");
break;
case 5:
days = 31;
strcpy(monthName,"May");
break;
case 7:
days = 31;
strcpy(monthName,"July");
break;
case 8:
days = 31;
strcpy(monthName,"August");
break;
case 10:
days = 31;
strcpy(monthName,"October");
break;
case 12:
days = 31;
strcpy(monthName,"December");
break;
case 4:
days = 30;
strcpy(monthName,"April");
break;
case 6:
days = 30;
strcpy(monthName,"June");
break;
case 9:
days = 30;
strcpy(monthName,"September");
break;
case 11:
days = 30;
strcpy(monthName,"November");
break;
case 2:
days = 28 +
1 * (count_leap_years(year+1) -count_leap_years(year));
strcpy(monthName,"February");
}
printf(" %s\n", monthName);
print_dayNames();
numspacefirst = PREVIOUSDAYSPACE * day; //spacing of date numbers
for (spaces=0;spaces<numspacefirst;spaces++)
{
printf("%c",' ');
}
for (datetoprint=1;datetoprint<=days;datetoprint++)
{
printf("%2d",datetoprint);
printf("%c", ' ');
day++;
if (day == DAYS_PER_WEEK)
{
day = 0;
printf("\n");
}
}
printf("\n\n");
return day;
}// find the day
void print_dayNames() // print day identations
{
printf("S M T W T F S \n");
printf("--------------------\n");
}
int find_jan_first
(int year) // the input year, must be >= MIN_YEAR
{ // 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
int count_leap_years
(int year) // user's choice of year, must be >= MIN_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