2013-11-16
先定义每个月的天数,2月按28天算
输入年月日后,根据年判断是否闰年(闰年加1天),再从1月加到当月的前一月,再加上日期就可以了
#include <stdio.h>
int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
main()
{
int yy,mm,dd, days;
int i;
printf("input year:"); scanf("%d", &yy);
printf("input month:"); scanf("%d", &mm);
printf("input day:"); scanf("%d", &dd);
/* 如果大于2月,要做闰年的判断,忘了,不一定准 */
if( mm > 2 && ((year%4==0 && year%100!=0) || year%400==0) ) days = 1;
else days = 0;
/* 加上前面各整月的天数 */
for(i = 0; i < mm-1; i++) days += month[i];
/* 加上日数 */
days += dd;
printf("This is the %d day of year %d!!\n", days, yy);
}
比如输入 1999 2 1
应该输出:This is the 32 day of year 1999!!
我大概给个思路,代码是现写的 没调试
先定义每个月的天数,2月按28天算
输入年月日后,根据年判断是否闰年(闰年加1天),再从1月加到当月的前一月,再加上日期就可以了
#include <stdio.h>
int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
main()
{int yy,mm,dd, days;
int i;
printf("input year:"); scanf("%d", &yy);
printf("input month:"); scanf("%d", &mm);
printf("input day:"); scanf("%d", &dd);
/* 如果大于2月,要做闰年的判断,忘了,不一定准 */
if( mm > 2 && ((year%4==0 && year%100!=0) || year%400==0) ) days = 1;
else days = 0;
/* 加上前面各整月的天数 */
for(i = 0; i < mm-1; i++) days += month[i];
/* 加上日数 */
days += dd;
printf("This is the %d day of year %d!!\n", days, yy);
}
比如输入 1999 2 1
应该输出:This is the 32 day of year 1999!!
2013-11-16
# include<stdio.h>
# define M 10
void main()
{
int a[]={31,29,31,30,31,30,31,31,30,31,30,31},*p,sum=0,n,i=0,day;
p=a;
printf("请你输入月份1-12\n");
scanf("%d",&n);
printf("请你输入%d月份的第几天\n",n);
scanf("%d",&day);
while(i<n-1)
{
sum+=*p++;
i++;
}
sum+=day;
printf("%d月%d号属于该年第%d天\n",n,day,sum);
}
2013-11-16
{
int day,month,year,sum,leap;
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");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("It is the %dth day.",sum);}
源代码 最好自己敲一次