c语言编写的查询某年某月某日是星期几的万年历。我写的这个怎么改啊?急求
#include<stdio.h>voidmain(){intday,month,year,sum,leap,s;printf("\npleaseinputyear,mo...
#include<stdio.h>
void main()
{
int day,month,year,sum,leap,s;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error\n");break;
}
sum=sum+day;
if(year%400==0||(year%4==0&&year%100!=0))
leap=1;
else leap=0;
if(leap==1&&month>2)
sum++;
for(year=1900;;year++)
{
s=(year-1900+1)*365+(year-1)/4+(year-1)/100+(year-1)/400;
s+=sum;
}
if(s%7==1) printf("The day is Monday\n");
if(s%7==2) printf("The day is Tuesday\n");
if(s%7==3) printf("The day is Wednesday\n");
if(s%7==4) printf("The day is Thursday\n");
if(s%7==5) printf("The day is Friday\n");
if(s%7==6) printf("The day is Saturday\n");
if(s%7==0) printf("The day is Sunday\n");
} 展开
void main()
{
int day,month,year,sum,leap,s;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error\n");break;
}
sum=sum+day;
if(year%400==0||(year%4==0&&year%100!=0))
leap=1;
else leap=0;
if(leap==1&&month>2)
sum++;
for(year=1900;;year++)
{
s=(year-1900+1)*365+(year-1)/4+(year-1)/100+(year-1)/400;
s+=sum;
}
if(s%7==1) printf("The day is Monday\n");
if(s%7==2) printf("The day is Tuesday\n");
if(s%7==3) printf("The day is Wednesday\n");
if(s%7==4) printf("The day is Thursday\n");
if(s%7==5) printf("The day is Friday\n");
if(s%7==6) printf("The day is Saturday\n");
if(s%7==0) printf("The day is Sunday\n");
} 展开
3个回答
展开全部
#include <stdio.h>
int runnian(int year )
{
if ((year %4 == 0) && (year % 100 != 0) //判断是否是闰年
|| (year % 400 == 0))
{
return 1;
}
return 0;
}
void show(int year,int month) //显示函数
{
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,wdays,mdays,days;
for(i=1,days=0;i<year;i++) //根据是否是闰年判断一年有几天
{
if(runnian(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && runnian(year)) //根据闰年判断2月是28还是29天
{
days+=29;
}
else
{
days+=month_day[i];
}
}
printf(" %s (%d)\n",month_str[month],year); //输出月份和年份的标识
printf(" Mon Tue Wed Thu Fri Sat Sun\n"); //输出星期的标识
wdays = days % 7;
for( j = 0; j < wdays; j++)
{
printf("%4c",' ');
}
if(month == 2 && runnian(year))
{
mdays=29;
}
else
{
mdays= month_day[month];
}
for(i=1;i<=mdays;i++)
{
if( i > 1 && days % 7 == 0 )
{
printf("\n");
}
if(month_day[month]==5&&days==1)
printf("劳动节");
else
printf("%4d",i);
days=days+1;
}
printf("\n----------------------------\n");
}
main()
{
int year,month;
printf("请输入年份和月份:");
scanf("%d %d",&year,&month);
show(year,month);
}
运行结果:
请输入年份和月份:2006 09 27
Sep (2006)
Mon Tue Wed Thu Fri Sat Sun
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
----------------------------
请按任意键继续. . .
int runnian(int year )
{
if ((year %4 == 0) && (year % 100 != 0) //判断是否是闰年
|| (year % 400 == 0))
{
return 1;
}
return 0;
}
void show(int year,int month) //显示函数
{
const char month_str[][4]={"","Jan","Feb","Mar","Apl",
"May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
const int month_day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,wdays,mdays,days;
for(i=1,days=0;i<year;i++) //根据是否是闰年判断一年有几天
{
if(runnian(i))
{
days += 366;
}
else
{
days += 365;
}
}
for(i=1;i<month;i++)
{
if(i==2 && runnian(year)) //根据闰年判断2月是28还是29天
{
days+=29;
}
else
{
days+=month_day[i];
}
}
printf(" %s (%d)\n",month_str[month],year); //输出月份和年份的标识
printf(" Mon Tue Wed Thu Fri Sat Sun\n"); //输出星期的标识
wdays = days % 7;
for( j = 0; j < wdays; j++)
{
printf("%4c",' ');
}
if(month == 2 && runnian(year))
{
mdays=29;
}
else
{
mdays= month_day[month];
}
for(i=1;i<=mdays;i++)
{
if( i > 1 && days % 7 == 0 )
{
printf("\n");
}
if(month_day[month]==5&&days==1)
printf("劳动节");
else
printf("%4d",i);
days=days+1;
}
printf("\n----------------------------\n");
}
main()
{
int year,month;
printf("请输入年份和月份:");
scanf("%d %d",&year,&month);
show(year,month);
}
运行结果:
请输入年份和月份:2006 09 27
Sep (2006)
Mon Tue Wed Thu Fri Sat Sun
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
----------------------------
请按任意键继续. . .
展开全部
for(year=1900;;year++)
{
s=(year-1900+1)*365+(year-1)/4+(year-1)/100+(year-1)/400;
s+=sum;
}
这个循环有问题,检查一下吧(注意循环条件)
{
s=(year-1900+1)*365+(year-1)/4+(year-1)/100+(year-1)/400;
s+=sum;
}
这个循环有问题,检查一下吧(注意循环条件)
追问
这个死循环解决了,但是还有个问题,就是那个闰年个数不对
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
XXX年XX月XX日是南方泰科是专业的万年历芯片开发公司。可以解决以上问题 /* 注:本程序("1\\t根据日期查星期几\\n");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询