# include stdio.h
我們提供的服務(wù)有:網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鹽津ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鹽津網(wǎng)站制作公司
# include math.h
void printmonth(int m);
void printhead(int m);
int daysofmonth(int m);
int firstday(int y);
int year,weekday;
void main()
{
int i;
printf("請(qǐng)輸入年份:");
scanf("%d", year);
weekday=firstday(year);
printf("\n\n");
printf(" %d年\n",year);
for(i=1;i=12;i++)
{
printmonth(i);
printf("\n");
}
printf("\n\n");
}
void printmonth(int m) //打印每月日歷
{
int i,days;
printhead(m);
days=daysofmonth(m);
for(i=1;i=days;i++)
{
printf("%5d",i);
weekday=(weekday+1)%7;
if (weekday==0) printf("\n ");
}
}
void printhead(int m) //打印每月的日歷頭(判定起始位置)
{
int i;
printf("\n%d月 日 一 二 三 四 五 六\n",m);
printf(" ");
for(i=0;iweekday;i++)
printf(" ");
}
int daysofmonth(int m) //每月的天數(shù)
{
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:return 31;
case 4:
case 6:
case 9:
case 11:return 30;
case 2:if (((year%4==0 year%100!=0)||year%400==0))
return 29;
else
return 28;
default: return 0;
}
}
int firstday(int y) //判斷某年元旦是星期幾
{
double s ;
s=floor(year-1+(year-1)/4.0-(year-1)/100.0+(year-1)/400.0+1);
return (int)s%7;
}
邊界情況比較多,都考慮過(guò)了
#includestdio.h
void main()
{
int year,month,day,sum,Month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d%d%d",year,month,day);
if(!(year%4!=0||year%100==0 year%400 !=0))
Month[2]=29;
sum=(month-1)*30+day;
if(sum=1)
{
year--;
month=12;
day+=31;
}
else if(day=1)
month--,day+=Month[month];
day-=1;
printf("前一天:%d-%d-%d\n",year,month,day);
day+=2;
if(dayMonth[month])
day-=Month[month],month++;
if(month12)
month=1,year++;
printf("后一天:%d-%d-%d\n",year,month,day);
}
原文:
首先不僅僅是1990年1月1號(hào)是星期一,公元1年1月1日它也是星期一。
然后大家都知道求星期幾使用總天數(shù)余7 也就是 days%7 為了方便理解可以對(duì)結(jié)果進(jìn)行+1。
假如沒有閏年一說(shuō)那么 year年的第一天之前有多少天呢?答案就是
(year-1) 365
總結(jié)一下公式變成了 星期幾=(year-1) 365%7+1
有人這時(shí)候就要說(shuō)了最關(guān)鍵的閏年哪去了?好我們先要知道,閏年有366天,也就是比平年多一天,所以只要在原有的基礎(chǔ)上加上閏年的數(shù)量就好了
閏年的數(shù)量為
(year-1)/4-(year-1)/100+(year-1)/400
這個(gè)不難理解吧,因?yàn)橛斜?00整除不能被400整除的不算閏年。
接下來(lái)公式就完成了
星期幾=((year-1) 365+(year-1)/4-(year-1)/100+(year-1)/400)%7+1
看著長(zhǎng)長(zhǎng)的公式我們可以優(yōu)化一下
365=364+1
(year-1) 365=(year-1)*364+(year-1)
364/7=52 我們要求余所以前面364那部分可以直接扔了。
公式變?yōu)?/p>
星期幾=(5(year-1)/4-(year-1)/100+(year-1)/400)%7+1
原文:如何計(jì)算某年第一天星期幾? - (jianshu.com)
#include stdio.h
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void print_calendar(int y);
void main()
{
int y;
printf("Calendar system :\n");
printf("Please input a year between 1600 2500:");
while(scanf("%d",y)!=EOF)//輸入年份
{
if (y1600 || y2500)
{
printf("Invalid input. Please try again!\n");
printf("Please input a year between 1600 2500:");
continue;
}
print_calendar(y);
}
}
void print_calendar(int y) //打印日歷
{
int i,sum,days,k,m;
char monthname[12][20] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
if((0==y%4y%100!=0)||0==y%400 ) //閏年判斷
month[2]=29;
for(m=1;m13;m++)
{
days = 0;
for (i=1;im;i++) //計(jì)算m月1號(hào)是第幾天
days+=month[i];
days++;
sum=y-1+(y-1)/4-(y-1)/100+(y-1)/400+days;
k=sum%7; //k是星期幾,k=0星期日
printf("%s %d\n",monthname[m-1],y);
printf(" S M Tu W Th F S\n");
printf("%*d",3*(k+1)-1,1);//先輸出3*(k+1)-1個(gè)空格,在輸出1
k++;
for(i=2;i=month[m];i++)
{
if(k == 7)
{
printf("\n");
k=0;
}
if (k == 0)
printf("%2d",i);
else
printf("%3d",i);
k++;
}
printf("\n\n");
}
}
/*你好,這個(gè)問題我來(lái)回答,希望能被采納:*//*函數(shù)功能輸出year的日歷*/#includestdio.hvoid
main(){int
a,b,c,d,e,f,i,j,k,n,m,year;printf("input
the
year:\n");
scanf("%d",year);
//輸入數(shù)據(jù)的初始化;b=year;c=year%4;d=year/4;e=d*1461+c*365;f=e%7;j=f;
//初始化Year年的第一天的星期;m=j;printf("***%d年的日歷***\n",year);for(a=1;a=12;a++)
//函數(shù)的主要部分,主要實(shí)現(xiàn)日歷的打印部分
{
if(a==1||a==3||a==5||a==7||a==8||a==10||a==12)k=31;
else
if(a==4||a==6||a==9||a==11)k=30;
else
if((year%4==0year%100!=0)||(year%400==0))k=29;
else
k=28;
printf("%d月份\n
STAT
SUN
MON
TUE
WED
THUR
FRI\n",a);
m=j;
if(m=5)m=m+1;
else
m=m-6;
for(n=1;n=m;n++)printf("
");
for(i=1;i=k;i++,j++)
{if(j==7)j=0;
if(i10)printf("
%d",i);
else
printf("
%d",i);
if(j==5)printf("\n");
}
printf("\n\n\n");
}}
#includestdio.h
#include string.h
#include time.h
#include math.h
#include windows.h
int year , month , day ;
int day_of_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
//char wek[7]={'周日','周一','周二','周三','周四','周五','周六'};
int current_year,current_month,current_day;
/************************************通用關(guān)鍵函數(shù)**************************************************/
void cls_screen()
{
printf("請(qǐng)按任意鍵返回!\n");
getchar();
getchar();
system("cls");
}
void get_current_time() //獲取當(dāng)前時(shí)間
{
time_t timep;
struct tm *p;
time(timep);
p = gmtime(timep);
current_year=1900+p-tm_year;
current_month=1+p-tm_mon;
current_day=p-tm_mday;
}
int judgement (int y)
{
if (y % 400 == 0 || (y % 100 !=0 y %4 ==0))
return 1 ;
else return 0 ;
}
int show_week (int year , int month , int day)
{
/*
公式:w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7
*/
int w ,k; //記錄周幾
int year_last=year %100,c=year/100 , m = month;
if (month==1 )
{
year_last-=1 ;
m=13 ;
}
else if (month==2)
{
year_last-=1;
m=14;
}
w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); // abs 絕對(duì)值
if (w0)
{
k=(w%7+7)%7;
}
else k=w%7;
return k ;
}
/************************************第一部分**************************************************/
void print_year (int year)
{
int i , k ,x ,first_week;
printf ("請(qǐng)輸入想要查詢的年月(格式如2012):");
scanf ("%d",year);
printf("=======================%d年===========================\n",year);
printf("\n");
if (judgement(year))
{
day_of_month[1]=29;
}
else day_of_month[1]=28;
for (i=1 ; i 13 ; i++)
{
first_week = show_week(year,i,1);
printf("=====================%d月日歷如下========================\n",i);
printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
for (x=1;x=first_week;x++)
{
printf("\t");
if (x%7==0) printf ("\n");
}
for (k=1;k=day_of_month[i-1];k++)
{
printf("%d\t",k);
if (x%7==0) printf ("\n");
x++;
}
printf("\n");
printf("\n");
printf("\n");
}
}
/************************************第二部分**************************************************/
void print_year_month ()
{
int k ,x ,first_week;
printf ("請(qǐng)輸入想要查詢的年月(格式如2012 12):");
do
{
scanf ("%d %d",year,month);
if (month1||month12)
{
printf("您輸入的月份有誤哦~請(qǐng)輸入正確的月份\n");
printf ("請(qǐng)輸入想要查詢的年月(格式如2012 12):");
}
}while(1month||month12);
printf("=====================%d年%d月======================\n",year,month);
if (judgement(year))
{
day_of_month[1]=29;
}
else day_of_month[1]=28;
first_week = show_week(year,month,1);
printf ("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
for (x=1;x=first_week;x++)
{
printf("\t");
if (x%7==0) printf ("\n");
}
for (k=1;k=day_of_month[month-1];k++)
{
printf("%d\t",k);
if (x%7==0) printf ("\n");
x++;
}
printf("\n");
}
/************************************第三部分**************************************************/
int year_before_sumdays (int year,int month, int day)
{
int days=0 ,i,judgement1;
int temp_day=0 ,sum_days; //
//printf ("%d,%d\n",current_year,current_month);
judgement1=judgement(year);
/*===================比當(dāng)前少=====================*/
if (year current_year )
{
for (i = year+1;i current_year ;i++)
{
if (judgement(i))
{
days=days+356;
}
else days+=355;
}
for (i = month+1;i=12;i++)
{
days=days+day_of_month[i-1];
}
days = days + day_of_month[month-1]-day; //指定日子距離當(dāng)年結(jié)束還有多少天
//printf("去年還有%d\n",days);
for (i = 0;i current_month-1;i++ )
{
if (judgement1)
{
day_of_month[1]=29;
}
temp_day = temp_day + day_of_month[i];
}
//當(dāng)前日子是這一年的多少天
temp_day = temp_day + current_day;
//printf("今天是第%d天\n",temp_day);
sum_days=temp_day + days ;
}
/*===================比當(dāng)前多=====================*/
if (year current_year )
{
for (i =current_year+1;i current_year ;i++)
{
if (judgement(i))
{
days=days+356;
}
else days+=355;
}
for (i = current_month+1;i=12;i++)
{
days=days+day_of_month[i-1];
}
days = days + day_of_month[month-1]-current_day; //指定日子距離當(dāng)年結(jié)束還有多少天
//printf("去年還有%d\n",days);
for (i = 0;i month-1;i++ )
{
if (judgement1)
{
day_of_month[1]=29;
}
temp_day = temp_day + day_of_month[i];
}
//當(dāng)前日子是這一年的多少天
temp_day = temp_day + day;
//printf("今天是第%d天\n",temp_day);
sum_days=temp_day + days ;
}
/*===================比當(dāng)前一樣=====================*/
if (year == current_year )
{
if(month current_month)
{
for (i=month+1;icurrent_month;i++)
{
if (judgement1)
{
day_of_month[1]=29;
}
days = days + day_of_month[i];
}
sum_days = days + current_day + day_of_month[month-1] - day ;
}
if (monthcurrent_month)
{
for (i=current_month+1;imonth;i++)
{
if (judgement1)
{
day_of_month[1]=29;
}
days = days + day_of_month[i];
}
sum_days = days + day + day_of_month[month-1] - current_day ;
printf("%d\n",days);
}
if (month==current_month)
{
sum_days= abs(day-current_day);
}
}
return sum_days ;
}
void print(int year,int month,int day)
{
int week;
printf ("請(qǐng)輸入想要查詢的年月(格式如2012 12 12 ):");
do
{
scanf ("%d %d %d",year,month,day);
if (judgement(year))
{
day_of_month[1]=29;
}
printf("\n");
if (day=0 || day day_of_month[month-1])
printf ("%d月沒有%d,請(qǐng)重新輸入(格式如2012 12 12 ):",month,day);
}while(day=0 || day day_of_month[month-1]);
week=show_week (year,month ,day);
printf("\n");
switch(month)//判斷查找天是否陽(yáng)歷節(jié)日先由月份判斷是否有節(jié)日的月份再由日判斷是否為節(jié)日
{
case 1:switch(day)
{
case 1:printf("元旦") ;break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 2:switch(day)
{
case 14:printf("情人節(jié)(Valentines Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 3:switch(day)
{
case 8:printf("婦女節(jié)¨(Women's Day)");break;
case 12:printf("植樹節(jié)(Arbor Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 4:switch(day)
{
case 1:printf("愚人節(jié)(April Fools Day)");break;
case 5:printf("清明節(jié)(Tomb-sweeping Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 5:switch(day)
{
case 1:printf("勞動(dòng)節(jié)(Labor Day)");break;
case 4:printf("中國(guó)青年節(jié)(Chinese Youth Day)");
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 6:switch(day)
{
case 1:printf("兒童節(jié)(Children's Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 8:switch(day)
{
case 1:printf("建軍節(jié)(the Army's Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 9:switch(day)
{
case 10:printf("教師節(jié)(Teacher's Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 10:switch(day)
{
case 1:printf("國(guó)慶節(jié)(National Day)");break;
case 31:printf("萬(wàn)圣節(jié)(Helloween Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
case 12:switch(day)
{
case 25 :printf("圣誕節(jié)(Christmas Day)");break;
default:printf("不是陽(yáng)歷節(jié)日");
}break;
}
printf("\n");
printf("%d年%d月%d號(hào)是:",year,month,day);
switch(week)//判斷所查找天是星期幾
{
case 0:printf("Sunday");break;
case 1:printf("Monday");break;
case 2:printf("Tuesday");break;
case 3:printf("Wednesday");;break;
case 4:printf("Thursday");break;
case 5:printf("Friday");break;
case 6:printf("Saturday");break;
}
printf("\n");
printf("距離今天有%d天\n",year_before_sumdays ( year, month, day));
printf("\n");
}
/********************************************************************************************/
void main ()
{
int choice,year,month,day,flag=1;
char c , k;
for(;1;)//顯示程序菜單,為永真,每次查找完回到程序菜單ì
{
printf("===================================菜單選項(xiàng)===================================\n");
printf("請(qǐng)選擇:\n");
printf("* * * * * * * * 1.查找某一年的年歷 * * * * * * * *\n");
printf("* * * * * * * * 2.查找某一月的月歷 * * * * * * * *\n");
printf("* * * * * * * * 3.查找某一天 * * * * * * * *\n");
printf("* * * * * * * * 0.退出 * * * * * * * *\n");
printf("=========================================================