自己遍了一个日历的软件,新手可以拿来看看,也请高手帮我指点一下
#include <stdio.h>void main()
{
void printcalendar(int year);
int year,choice;
printf("My First C Program: Calendar\n\n");
printf("Please Input The Year:\n");
printf("(Input '0' to exit)\t--\t");
while(1)
{
printf("Please Input The Year:\t");
scanf("%d",&year);
if(year==0)
break;
putchar('\n');
printcalendar(year);
putchar('\n');
}
}
void printmonth(int month,int year);
void printcalendar(int year)
{
int month;
for(month=1;month<=12;month++)
{printmonth(month,year);printf("\n");}
}
void printmonth(int month,int year)
{
char *month_name(int month);
int month_days(int month,int year);
int first_day(int month,int year);
int day,weekday,n,days;
char *p;
weekday=first_day(month,year);
p=month_name(month);
days=month_days(month,year);
printf(" ==%s %d年/%d月\n",p,year,month);
printf("Mon Tue Wen Thu Fri Sat Sun\n");
for(n=0;n<weekday;n++)
{printf(" ");}
for(day=1;day<=days;day++)
{
printf(" %-2d ",day);
weekday+=1;
if(weekday%7==0)
{
putchar('\n');
}
if(weekday==8)weekday=1;
}
}
char *month_name(int month)
{
char *p_name;
char *str[]={"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
p_name=*(str+(month-1));
return (p_name);
}
int isleapyear(int year);
int month_days(int month,int year)
{
switch(month)
{
case 1:return 31;
case 2:if(isleapyear(year))return 29;
return 28;
case 3:return 31;
case 4:return 30;
case 5:return 31;
case 6:return 30;
case 7:return 31;
case 8:return 31;
case 9:return 30;
case 10:return 31;
case 11:return 30;
case 12:return 31;
}
}
int isleapyear(int year)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
return 1;
return 0;
}
int first_day(int month,int year)
{
int i,l=0,monthdays,n=0;
static int sumdays;
monthdays=month_days(month-1,year);
if(month==1)
{
for(i=1;i<year;i++)
{
if(isleapyear(i))
l+=1;
else n+=1;
}
return (sumdays=(n*365+l*366)%7);
}
return (sumdays=(sumdays+monthdays)%7);
}