用c语言编写一程序,用户输入日期,计算该日是今年的第几天
#include"stdio.h"
#include"stdlib.h"
intmain()
{
intday,month,year,sum,leap;
printf("\npleaseinputyear,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month)/*先计算某月以前月份的总天数*/
{
case1:sum=0;break;
case2:sum=31;break;
case3:sum=59;break;
case4:sum=90;break;
case5:sum=120;break;
case6:sum=151;break;
case7:sum=181;break;
case8:sum=212;break;
case9:sum=243;break;
case10:sum=273;break;
case11:sum=304;break;
case12:sum=334;break;
default:printf("dataerror\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)/*如果是闰年且月份大于2,总天数应该加一天*/
{
sum++;
}
printf("Itisthe%dthday.\n",sum);
return0;
}
扩展资料
C语言输入任意一个日期的年、月、日的值,求出是星期几并输出。
#include<stdio.h>
voidmain()
{
inty,m,d,c,s,w;
printf("请输入8位年月日,以空格隔开:");
scanf("%d%d%d",&y,&m,&d);
if(m==1)c=d;
elseif(m==2)c=31+d;
elseif(m==3)c=31+28+d;
elseif(m==4)c=31+28+31+d;
elseif(m==5)c=31+28+31+30+d;
elseif(m==6)c=31+28+31+30+31+d;
elseif(m==7)c=31+28+31+30+31+30+d;
elseif(m==8)c=31+28+31+30+31+30+31+d;
elseif(m==9)c=31+28+31+30+31+30+31+31+d;
elseif(m==10)c=31+28+31+30+31+30+31+31+30+d;
elseif(m==11)c=31+28+31+30+31+30+31+31+30+31+d;
elseif(m==12)c=31+28+31+30+31+30+31+31+30+31+30+d;
if(m>2)
{if(y%100==0){if(y%400==0)c=c+1;}
elseif(y%4==0)c=c+1;}
s=(y-1)*365+(y-1)/4-(y-1)/100+(y-1)/400+c;
w=s%7;
switch(w)
{
case0:printf("星期日");break;
case1:printf("星期一");break;
case2:printf("星期二");break;
case3:printf("星期三");break;
case4:printf("星期四");break;
case5:printf("星期五");break;
case6:printf("星期六");break;
printf("\n");
}
}
int mdays(int y,int m){
if (m==2) return (y%4==0 && (y%100==0 || y%400==0))?29:28;
else if (m==4 || m==6 || m==9 || m==11) return 30;
else return 31;
}
main(){
int y,m,d,days;
printf("Enter year month day:");
scanf("%d%d%d",&y,&m,&d);
days=d;
while(m>1){days+=mdays(y,m-1);m--;}
printf("%d\n",days);
}
程序在TURBOC下运行几次的界面如下:
Enter year month day:2008 1 2
2
Enter year month day:2008 2 2
33
Enter year month day:2008 3 2
61