包含三文件w.c, date.h, date.c。其中w文件为主文件
w.c文件内容,包含date.h
#include "date.h"
void main()
{
Date date;
int choose;
Input(&date);
Show(&date,&choose);
}
date.h声明函数
/* 定义日期 */
typedef struct data
{
int year;
int month;
int day;
}Date;
int isr(int); /* 判断是否是闰年 */
int input(Date * ); /* 输入 */
int month_day(Date *); /* 确定每月的天数 */
int Isbig(Date * ,Date * ); /* 比较两个日期的大小 */
int returnday(Date * , Date * ); /* 算两个日期的天数差 */
void returnweek(Date * ); /* 算星期几 */
void calader(Date *); /* 日历 */
void show(Date *,int * ); /* 显示 */
date.c 其中写的是.h文件中函数的实现以及封装其他需要调用但不用主函数调用的子函数
#include "date.h"
int isr(Date * date)
{
int isR=0;
int year = date->year;
if(((date->year%4==0)&&(date->year%100!=0))||(date->year%400==0))
{
isR=1;
}
else
{
isR=0;
}
return isR;
}
int input(Date * date)
{
int _day;
int isok=0;
if((date->month>12)||(date->month<1))
{
isok=1;
}
else
{
_day=month_day(date);
if((date->day<1)||(date->day>_day))
{
isok=1;
}
}
return isok;
}
void Input(Date * date)
{
int IsOK = 0;
printf("Please input the first date:");
scanf("%d %d %d",&date->year,&date->month,&date->day);
IsOK = input(date);
while(IsOK)
{
printf("It is a error day,please input again:");
scanf("%d %d %d",&date->year,&date->month,&date->day);
IsOK=input(date);
}
}
int month_day(Date *date)
{
int isR=isr(date);
int day=0;
if((date->month==1)||(date->month==3)||(date->month==5)||(date->month==7)||(date->month==8)||(date->month==10)||(date->month==12))
{
day=31;
}
else
{
if(date->month==2)
{
if(isR)
{
day=29;
}
else
{
day=28;
}
}
else
{
day=30;
}
}
return day;
}
int Isbig(Date * date1,Date * date2)
{
int isbig=0;
if((date1->year) < (date2->year))
{
isbig=1;
}
else
{
if(date1->month < date2->month)
{
isbig=1;
}
else
{
if(date1->day < date2->day)
{
isbig=1;
}
}
}
return isbig;
}
void swap(Date * date1,Date * date2)
{
int year,month,day;
year = date1->year;
month = date1->month;
day = date1->day;
date1->year = date2->year;
date1->month = date2->month;
date1->day = date2->day;
date2->year = year;
date2->month = month;
date2->day = day;
}
int returnday(Date * date1, Date * date2)
{
Date temp;
int isR=0;
int date=0;
int i,j,isbig=0;
int year,month,day;
/* if(year1<year2)
{
isbig=1;
}
else
{
if(month1<month2)
{
isbig=1;
}
else
{
if(day1<day2)
{
isbig=1;
}
}
}
*/
isbig=Isbig(date1,date2);
if(isbig)
{
swap(date1,date2);
}
if(date1->year > date2->year)
{
for(i = date2->year + 1;i < date1->year;i++)
{
isR=isr(i);
if(isR)
{
date+=366;
}
else
{
date+=365;
}
}
for(j = 1;j < date1->month;j++)
{
temp.month = j;
temp.year = date1->year;
date += month_day(&temp);
}
for(j = 12;j > date2->month;j--)
{
temp.month = j;
temp.year = date2->year;
date += month_day(&temp);
}
date=date+month_day(date2) - date2->day + date1->day;
}
else
{
if(date1->month > date2->month)
{
for(j = date2->month + 1;j < date1->month;j++)
{
temp.month = j;
temp.year = date1->year;
date += month_day(&temp);
}
date = date + month_day(date2) - date2->day + date1->day;
}
else
{
date = date + date1->day - date2->day;
}
}
return date;
}
void returnweek(Date * date)
{
Date date1;
int isbig;
int ndate;
int week;
date1.year = 2012;
date1.month = 1;
date1.day = 1;
isbig=Isbig(date,&date1);
ndate=returnday(date,&date1);
week=ndate%7;
if(isbig)
{
switch(week)
{
case 0:
printf("Sunday\n");
break;
case 6:
printf("Monday\n");
break;
case 5:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 3:
printf("Thursday\n");
break;
case 2:
printf("Friday\n");
break;
case 1:
printf("Saturday\n");
break;
}
}
else
{
switch(week)
{
case 0:
printf("Sunday\n");
break;
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
}
}
}
void calader(Date * date)
{
int days = month_day(date);
int i=1;
int weekday=0;
int isbig=0;
Date date1;
Date ntemp;
date->day=1;
ntemp.year = date->year;
ntemp.month = date->month;
date1.year = 2012;
date1.month = 1;
date1.day = 1;
weekday=(returnday(date,&date1))%7;
isbig=Isbig(date,&date1);
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
if(isbig)
{
switch(weekday)
{
case 0:
printf(" %2d",i);
break;
case 6:
printf(" %2d",i);
break;
case 5:
printf(" %2d",i);
break;
case 4:
printf(" %2d",i);
break;
case 3:
printf(" %2d",i);
break;
case 2:
printf(" %2d",i);
break;
case 1:
printf(" %2d\n",i);
break;
}
}
else
{
switch(weekday)
{
case 0:
printf(" %2d",i);
break;
case 1:
printf(" %2d",i);
break;
case 2:
printf(" %2d",i);
break;
case 3:
printf(" %2d",i);
break;
case 4:
printf(" %2d",i);
break;
case 5:
printf(" %2d",i);
break;
case 6:
printf(" %2d\n",i);
break;
}
}
for(i=2;i<=days;i++)
{
ntemp.day = i;
weekday=(returnday(&ntemp,&date1))%7;
if(isbig)
{
if(weekday==0)
{
printf(" %2d",i);
}
else
if(weekday>1&&weekday<7)
{
printf(" %2d",i);
}
else
{
printf(" %2d\n",i);
}
}
else
{
if(weekday==0)
{
printf(" %2d",i);
}
else
if(weekday>0&&weekday<6)
{
printf(" %2d",i);
}
else
{
printf(" %2d\n",i);
}
}
}
printf("\n");
}
void Show(Date * date,int *choose)
{
int show=1;
int ndate;
Date date1;
Date d=*date;
printf("Please choose : 1,Weekday 2,calader 3,相差天数\n");
scanf("%d",choose);
switch(*choose)
{
case 1:
returnweek(&d);
break;
case 2:
calader(&d);
break;
case 3:
Input(&date1);
ndate=returnday(&d,&date1);
printf("%d\n",ndate);
break;
default:
break;
}
}
这样写可以减少主文件中的代码,将各个函数封装,而且调用时只要知道.h的头文件就行了。