计算每一年中的其中一天是第几天
#include<stdio.h>
#include<math.h>
#pragma warning(disable:4996)//因我的环境是vs2019,加上这个才不会在很多地方报错
int main()
{
int year, month, day, leap, sum;
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 errorj"); 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++;
}
printf("it is %dth day.", sum);
getch();
return 0; 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 errorj"); break;
这里明显还可以继续进化 #include <stdio.h>
int main()
{
int year, month, day, leap, sum;
printf("\nplease input year month day\n");
scanf("%d,%d,%d", &year, &month, &day);
int days = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
sum = (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) ? days+1 : days;
printf("it is %dth day.", sum);
return 0;
} #include <stdio.h>
int runnian(int a);
void main()
{
int year,month,day,tian=0;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
int i;
printf("输入年:");
scanf("%d",&year);
printf("输入月:");
scanf("%d",&month);
printf("输入日:");
scanf("%d",&day);
printf("日期为:%d年%d月%d日\n",year,month,day);
for(i=0;i<=(month-1);i++)
{
if(runnian(year)==0)
tian=a+tian;
else if(runnian(year)==1&&month>2)
tian=a+tian+1;
else
tian=a+tian;
}
printf("总天数为:%d\n",tian);
}
int runnian(int year)
{
if (year%100==0)
{
year=year%400;
if(year==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
year=year%4;
if(year==0)
{
return 1;
}
else
{
return 0;
}
}
}
页:
[1]