c语言输入某年某月某日 判断这一天是这一年的第几天用switch
3个回答
展开全部
#include "stdio.h"
#include "conio.h"
int main()
{
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);
getchar();
}
展开全部
参考代码如下:
#include <stdio.h>
int main()
{
int i, days = 0;
int year, month, day;
int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("输入年、月、日,用空格隔开:");
scanf("%d%d%d", &year, &month, &day);
for (i=1; i<month; i++)
{
days += day_tab[i];
}
days += day;
if ((year%4==0 && year%100!=0 || year%400==0) && month>=3)
days+=1;
printf("%d年%d月%d日是这一年的第%d天!\n", year, month, day, days);
return 0;
}
#include <stdio.h>
int main()
{
int i, days = 0;
int year, month, day;
int day_tab[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("输入年、月、日,用空格隔开:");
scanf("%d%d%d", &year, &month, &day);
for (i=1; i<month; i++)
{
days += day_tab[i];
}
days += day;
if ((year%4==0 && year%100!=0 || year%400==0) && month>=3)
days+=1;
printf("%d年%d月%d日是这一年的第%d天!\n", year, month, day, days);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
void isleap(int n)
{
if(n%400==0||n%100!=&&n%4==0)
return 1;
return 0;
}
scanf("%d%d%d",&year,&month,&day);
int result=day;
switch(month)
{
case 12:result+=30;
case 11:result+=31;
case 10:result+=30;
case 9:result+=31;
case 8:result+=30;
case 7:result+=31;
case 6:result+=31;
case 5:result+=30;
case 4:result+=31;
case 3:if(isleap(year)){result+=29;}else {result+=28;}
case 2:result+=31;
}
printf("%d\n",result);
核心代码,另外自己补充吧
{
if(n%400==0||n%100!=&&n%4==0)
return 1;
return 0;
}
scanf("%d%d%d",&year,&month,&day);
int result=day;
switch(month)
{
case 12:result+=30;
case 11:result+=31;
case 10:result+=30;
case 9:result+=31;
case 8:result+=30;
case 7:result+=31;
case 6:result+=31;
case 5:result+=30;
case 4:result+=31;
case 3:if(isleap(year)){result+=29;}else {result+=28;}
case 2:result+=31;
}
printf("%d\n",result);
核心代码,另外自己补充吧
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询