/*
* File : calender.c
* ----------------------
* This program is used to generate a calender for a year entered by user.
*/
#include
/*
* Constants:
* -----------
* Days of the week are represented by the integers 0-6.
* Month of the year are identified by the integers 1-12.
*/
#define Sunday 0
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6
#define IsALeapYear(year) ((year % 4 == 0) && (year % 100 !=0)) || (year % 400 == 0)
/* Fuction prototypes */
void GiveInstructions();
int GetYear();
void PrintCalender(int year);
void PrintCalenderMonth(int year,int month);
char * MonthName(int month);
int FirstDayOfMonth(int year,int month);
int MonthDays(int year,int month);
void IndentFirstLine(int weekday);
/* Main Program */
void main()
{
int year;
GiveInstructions();
year = GetYear();
PrintCalender(year);
}
/*
* Function: GiveInstructions
* Usage: GiveInstructions();
* ------------------------------------
* This program prints out instructions to user.
*/
void GiveInstructions()
{
printf("\n\n----------This program displays a calender for a full year.----------\n\n\n");
}
/*
* Function: GetYear
* Usage: year = GetYear();
* -----------------------------------
* This program will get a year from user.
*/
int GetYear()
{
int year;
printf("-----Please input a year:");
scanf("%d",&year);
printf("\n");
return (year);
}
/*
* Fuction: PrintCalender
* Usage: PrintCalender(year);
* ---------------------------------------
* This program will print out a calender for a entire year.
*/
void PrintCalender(int year)
{
int month;
for (month = 1; month <= 12; month++) {
PrintCalenderMonth(year, month);
}
}
/*
* Function: PrintCalenderMonth
* Usage: PrintCalenderMonth(int year, int month)
* -------------------------------------------------------------
* This program will print out a calender for a entire month.
*/
void PrintCalenderMonth(int year, int month)
{
int nDays,weekday,i;
printf(" %s %d\n",MonthName(month),year);
printf(" Su Mo Tu We Th Fr Sa\n");
nDays = MonthDays(year,month);
weekday = FirstDayOfMonth(year,month);
IndentFirstLine (weekday);
for (i = 1; i <= nDays; i++) {
printf(" %2d",i);
if (weekday == Saturday) printf("\n");
weekday = (weekday + 1) % 7;
}
if (weekday != Sunday) printf("\n");
}
/*
* Function: MonthName
* Usage: char * MonthName(month)
* ------------------------------------------
* This program prints out the monthname.
*/
char * MonthName(int month)
{
switch (month) {
case 1: return ("January");
case 2: return ("February");
case 3: return ("March");
case 4: return ("April");
case 5: return ("May");
case 6: return ("June");
case 7: return ("July");
case 8: return ("August");
case 9: return ("September");
case 10: return ("October");
case 11: return ("November");
case 12: return ("December");
defualt: return ("Illegal month");
}
}
/*
* Function: FirstDayOfMonth
* Usage: weekday = FirstDayOfMonth(year,month)
* -------------------------------------------------------
* This program returns the weekday of a month.
*/
int FirstDayOfMonth(int year,int month)
{
int i,weekday,nDays;
weekday = Monday;
if (year > 1900) {
for (i = 1900; i
weekday = (weekday + 365) % 7;
if (IsALeapYear(i)) weekday = (weekday + 1) % 7;
}
for (i = 1; i
nDays = MonthDays(year,i);
weekday = (weekday + nDays) % 7;
}
}
else{
for (i = 1900; i > year; i--) {
weekday = (weekday - 365 + 7 * 53 ) % 7;
if (IsALeapYear(i)) weekday = (weekday + 6) % 7;
}
for (i = 1; i
nDays = MonthDays(year,i);
weekday = (weekday + nDays) % 7;
}
}
return (weekday);
}
/*
* Function: MonthDays
* Usage: nDays = MonthDays(year,month)
* -----------------------------------------------------
* This program returns the number of days for a month.
*/
int MonthDays(int year,int month)
{
switch (month) {
case 2:
if (IsALeapYear(year)) return (29);
return (28);
case 4:case 6:case 9:case 11:
return (30);
default : return (31);
}
}
/*
* Function: IndentFirstLine
* Usage: IndentFirstLine(weekday)
* --------------------------------------
* This program prints out some blank spaces for the calender.
*/
void IndentFirstLine(int weekday)
{
int i;
for (i = 0; i
{
printf(" ");
}
}
/* 参考《C语言的科学与艺术》*/