扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
# include stdio.h
我们提供的服务有:网站制作、网站设计、微信公众号开发、网站优化、网站认证、盐津ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的盐津网站制作公司
# 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("请输入年份:");
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) //每月的天数
{
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;
}
边界情况比较多,都考虑过了
#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号是星期一,公元1年1月1日它也是星期一。
然后大家都知道求星期几使用总天数余7 也就是 days%7 为了方便理解可以对结果进行+1。
假如没有闰年一说那么 year年的第一天之前有多少天呢?答案就是
(year-1) 365
总结一下公式变成了 星期几=(year-1) 365%7+1
有人这时候就要说了最关键的闰年哪去了?好我们先要知道,闰年有366天,也就是比平年多一天,所以只要在原有的基础上加上闰年的数量就好了
闰年的数量为
(year-1)/4-(year-1)/100+(year-1)/400
这个不难理解吧,因为有被100整除不能被400整除的不算闰年。
接下来公式就完成了
星期几=((year-1) 365+(year-1)/4-(year-1)/100+(year-1)/400)%7+1
看着长长的公式我们可以优化一下
365=364+1
(year-1) 365=(year-1)*364+(year-1)
364/7=52 我们要求余所以前面364那部分可以直接扔了。
公式变为
星期几=(5(year-1)/4-(year-1)/100+(year-1)/400)%7+1
原文:如何计算某年第一天星期几? - (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++) //计算m月1号是第几天
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个空格,在输出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");
}
}
/*你好,这个问题我来回答,希望能被采纳:*//*函数功能输出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);
//输入数据的初始化;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++)
//函数的主要部分,主要实现日历的打印部分
{
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;
/************************************通用关键函数**************************************************/
void cls_screen()
{
printf("请按任意键返回!\n");
getchar();
getchar();
system("cls");
}
void get_current_time() //获取当前时间
{
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 绝对值
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 ("请输入想要查询的年月(格式如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 ("请输入想要查询的年月(格式如2012 12):");
do
{
scanf ("%d %d",year,month);
if (month1||month12)
{
printf("您输入的月份有误哦~请输入正确的月份\n");
printf ("请输入想要查询的年月(格式如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);
/*===================比当前少=====================*/
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; //指定日子距离当年结束还有多少天
//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];
}
//当前日子是这一年的多少天
temp_day = temp_day + current_day;
//printf("今天是第%d天\n",temp_day);
sum_days=temp_day + days ;
}
/*===================比当前多=====================*/
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; //指定日子距离当年结束还有多少天
//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];
}
//当前日子是这一年的多少天
temp_day = temp_day + day;
//printf("今天是第%d天\n",temp_day);
sum_days=temp_day + days ;
}
/*===================比当前一样=====================*/
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 ("请输入想要查询的年月(格式如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,请重新输入(格式如2012 12 12 ):",month,day);
}while(day=0 || day day_of_month[month-1]);
week=show_week (year,month ,day);
printf("\n");
switch(month)//判断查找天是否阳历节日先由月份判断是否有节日的月份再由日判断是否为节日
{
case 1:switch(day)
{
case 1:printf("元旦") ;break;
default:printf("不是阳历节日");
}break;
case 2:switch(day)
{
case 14:printf("情人节(Valentines Day)");break;
default:printf("不是阳历节日");
}break;
case 3:switch(day)
{
case 8:printf("妇女节¨(Women's Day)");break;
case 12:printf("植树节(Arbor Day)");break;
default:printf("不是阳历节日");
}break;
case 4:switch(day)
{
case 1:printf("愚人节(April Fools Day)");break;
case 5:printf("清明节(Tomb-sweeping Day)");break;
default:printf("不是阳历节日");
}break;
case 5:switch(day)
{
case 1:printf("劳动节(Labor Day)");break;
case 4:printf("中国青年节(Chinese Youth Day)");
default:printf("不是阳历节日");
}break;
case 6:switch(day)
{
case 1:printf("儿童节(Children's Day)");break;
default:printf("不是阳历节日");
}break;
case 8:switch(day)
{
case 1:printf("建军节(the Army's Day)");break;
default:printf("不是阳历节日");
}break;
case 9:switch(day)
{
case 10:printf("教师节(Teacher's Day)");break;
default:printf("不是阳历节日");
}break;
case 10:switch(day)
{
case 1:printf("国庆节(National Day)");break;
case 31:printf("万圣节(Helloween Day)");break;
default:printf("不是阳历节日");
}break;
case 12:switch(day)
{
case 25 :printf("圣诞节(Christmas Day)");break;
default:printf("不是阳历节日");
}break;
}
printf("\n");
printf("%d年%d月%d号是:",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("===================================菜单选项===================================\n");
printf("请选择:\n");
printf("* * * * * * * * 1.查找某一年的年历 * * * * * * * *\n");
printf("* * * * * * * * 2.查找某一月的月历 * * * * * * * *\n");
printf("* * * * * * * * 3.查找某一天 * * * * * * * *\n");
printf("* * * * * * * * 0.退出 * * * * * * * *\n");
printf("=========================================================
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流