当前位置: 代码迷 >> C语言 >> 给出年,月,日,计算该日是该年的第n天
  详细解决方案

给出年,月,日,计算该日是该年的第n天

热度:181   发布时间:2006-11-30 18:44:25.0
给出年,月,日,计算该日是该年的第n天
1给出年,月,日,计算该日是该年的第N天
搜索更多相关的解决方案: 该日  

----------------解决方案--------------------------------------------------------

这个问题很简单呀!
年对四求余!!!!!!确定2月的值!!!!!!!!
对月的值进行判断!!!!!!!!!分出大月,月,2月(小月)
在用循环加
最后加上日!!!!!!!!!
就OK了
是不是很简单呀!!!!!!!!!!!!1


----------------解决方案--------------------------------------------------------

int i,days=0;
int p[12]={31,28,31,30,31,30,31,31,30,31,30,31}
if(闰年) p[1]++;
for(i=0;i<月份数;i++) days+=p[i];
printf("%d",days+天数);

闰年判断方法楼上已写,如不懂跟帖提问,闰年判断最好通过函数(这样结构化思想强,利于读懂程序),月份数与天数通过scanf读取,这是最基本的了,写好scanf后将读入数据的变量名替换这些文字就是纯C语言程序了

[此贴子已经被作者于2006-11-30 20:09:18编辑过]


----------------解决方案--------------------------------------------------------

int year
if(year%4==0&&year%100!=0)||(year%400==0);
是闰年
else
不是闰年

[此贴子已经被作者于2006-11-30 22:16:44编辑过]


----------------解决方案--------------------------------------------------------
卧龙孔明
----------------解决方案--------------------------------------------------------
main()
{
int year,month,day,N,i,tatol_day;
printf("input the year,month and day:\n");
{
scanf("%d,%d,%d",&year,&month,&day);
if(month<=0||month>12)
{ i=1;
printf("input error month,pls input ag !\n");
}
if(month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
if( day<1||day>29)
{i=1;
printf("input error day,pls input ag !\n");
}
}
else
if( day<1||day>28)
{i=1;
printf("input error day,pls input ag !\n");
}
}
else
{
if((month==1)
||(month==3)
||(month==5)
||(month==7)
||(month==8)
||(month==10)
||(month==12))
{if(day<1||day>31)
{i=1;
printf("input error day,pls input ag !\n");
}
}
else
{
if(day<1||day>30)
{i=1;
printf("input error day,pls input ag !\n");
}
}
}
}while(1);

switch(month)
{
case:1
N=day;
break;
case:2
N=31*(month-1)+day;
break;
case:3
N=31*(month-1)-2+day; // +29
break;
case:4
N=31*(month-1)-2+day;
break;
case:5
N=31*(month-1)-3+day;
break;
case:6
N=31*(month-1)-3+day;
case:7
N=31*(month-1)-4+day;
break;
case:8
N=31*(month-1)-4+day;
break;
case:9
N=31*(month-1)-4+day;
break;
case:10
N=31*(month-1)-5+day;
break;
case:11
N=31*(month-1)-5+day;
break;
case:12
N=31*(month-1)-6+day;
break;
default:
break;
}
if(((year%4==0&&year%100!=0)||(year%400==0))||(month<=2))
tatol_day=N;
else
tatol_day=N-1;
printf("the day is %d\n",tatol_day);
}
----------------解决方案--------------------------------------------------------
以下是引用hyxlz2008在2006-12-1 11:18:01的发言:
main()
{
int year,month,day,N,i,tatol_day;
printf("input the year,month and day:\n");
{
scanf("%d,%d,%d",&year,&month,&day);
if(month<=0||month>12)
{ i=1;
printf("input error month,pls input ag !\n");
}
if(month==2)
{
if((year%4==0&&year%100!=0)||(year%400==0))
{
if( day<1||day>29)
{i=1;
printf("input error day,pls input ag !\n");
}
}
else
if( day<1||day>28)
{i=1;
printf("input error day,pls input ag !\n");
}
}
else
{
if((month==1)
||(month==3)
||(month==5)
||(month==7)
||(month==8)
||(month==10)
||(month==12))
{if(day<1||day>31)
{i=1;
printf("input error day,pls input ag !\n");
}
}
else
{
if(day<1||day>30)
{i=1;
printf("input error day,pls input ag !\n");
}
}
}
}while(1);

switch(month)
{
case:1
N=day;
break;
case:2
N=31*(month-1)+day;
break;
case:3
N=31*(month-1)-2+day; // +29
break;
case:4
N=31*(month-1)-2+day;
break;
case:5
N=31*(month-1)-3+day;
break;
case:6
N=31*(month-1)-3+day;
case:7
N=31*(month-1)-4+day;
break;
case:8
N=31*(month-1)-4+day;
break;
case:9
N=31*(month-1)-4+day;
break;
case:10
N=31*(month-1)-5+day;
break;
case:11
N=31*(month-1)-5+day;
break;
case:12
N=31*(month-1)-6+day;
break;
default:
break;
}
if(((year%4==0&&year%100!=0)||(year%400==0))||(month<=2))
tatol_day=N;
else
tatol_day=N-1;
printf("the day is %d\n",tatol_day);
}

这段代码还需要修改:
在scanf("%d,%d,%d",&year,&month,&day);前要加上i=0;
既: ……
printf("input the year,month and day:\n");
{
i=0; // must add
scanf("%d,%d,%d",&year,&month,&day);
if(month<=0||month>12)
……
另外有一处笔误:while(1);应为while(i);


----------------解决方案--------------------------------------------------------

#include<stdio.h>
struct date
{
int year;
int month;
int day;
}a;
int fun(struct date);
void main()
{
while(printf("input the data(year/month/day):"),scanf("%d/%d/%d",&a.year,&a.month,&a.day))
printf("There are %d days\n",fun(a)+a.day);
}

int fun(struct date a)
{
int leap=0,sum=0;
int num[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
if((a.year%4==0&&a.year%100!=0)||a.year%400==0) leap=1;
for(int i=0;i<a.month;i++) sum+=num[leap][i];
return sum;
}


----------------解决方案--------------------------------------------------------
以下是引用unicorn在2006-12-1 11:47:44的发言:

#include<stdio.h>
struct date
{
int year;
int month;
int day;
}a;
int fun(struct date);
void main()
{
while(printf("input the data(year/month/day):"),scanf("%d/%d/%d",&a.year,&a.month,&a.day))
printf("There are %d days\n",fun(a)+a.day);
}

int fun(struct date a)
{
int leap=0,sum=0;
int num[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
if((a.year%4==0&&a.year%100!=0)||a.year%400==0) leap=1;
for(int i=0;i<a.month;i++) sum+=num[leap][i];
return sum;
}

unicorn 你太强了,有机会指点指点一下小弟。
其中有一点我不太明白,
while(printf("input the data(year/month/day):"),scanf("%d/%d/%d",&a.year,&a.month,&a.day))
是怎么判断的。谢谢!

请问你的联系方式,我要向你请教很多问题。


----------------解决方案--------------------------------------------------------

main()
{
char strp[100];
strp = "Test" ;
}


----------------解决方案--------------------------------------------------------
  相关解决方案