標籤:include 原始碼 c程式設計 return
題目:輸入某年某月某日,判斷這一天是這一年的第幾天?
1.程式分析:以3月5日為例,應該先把前兩個月的加起來,然後再加上5天即本年的第幾天,特殊情況,閏年且輸入月份大於3時需考慮多加一天。
2.程式原始碼:
#include <stdio.h>
int main(void)
{
int day, month, year, countDay, leap;
printf("\nPlease input year, month and day\n");
scanf("%d %d %d", &year, &month, &day);
if((year % 4 == 0 && year % 100 != 0) || (year % 400 ==0))
leap = 1;
else
leap = 0;
switch(month)
{
case 1:
countDay = 0;
if(day <= 0 || day > 31)
return 1;
break;
case 2:
countDay = 31;
if(leap != 1)
{
if(day <= 0 || day > 28)
return 1;
}
else
{
if(day <= 0 || day > 29)
return 1;
}
break;
case 3:
countDay = 59;
if(day <= 0 || day > 31)
return 1;
break;
case 4:
countDay = 90;
if(day <= 0 || day > 30)
return 1;
break;
case 5:
countDay = 120;
if(day <= 0 || day > 31)
return 1;
break;
case 6:
countDay = 151;
if(day <= 0 || day > 30)
return 1;
break;
case 7:
countDay = 181;
if(day <= 0 || day > 31)
return 1;
break;
case 8:
countDay = 212;
if(day <= 0 || day > 31)
return 1;
break;
case 9:
countDay = 243;
if(day <= 0 || day > 30)
return 1;
break;
case 10:
countDay = 273;
if(day <= 0 || day > 31)
return 1;
break;
case 11:
countDay = 304;
if(day <= 0 || day > 30)
return 1;
break;
case 12:
countDay = 334;
if(day <= 0 || day > 31)
return 1;
break;
default:
printf("Input month error!\n");
return 1;
break;
}
countDay = countDay + day + 1;
printf("This is the %dth of this years!\n", countDay);
return 0;
}