C语言函数练习题

题目:编写函数,计算某月某日是该年度的第几天。

include

/declare/
int how_many_days_before_the_day(int month, int day);
int day_of_year(int month, int day, int year);

int main()
{
int n = 0, MONTH, DAY;
printf("Month: ");
scanf("%d", &MONTH);
printf("Day: ");
scanf("%d", &DAY);
n = how_many_days_before_the_day(MONTH, DAY);
printf("DAYS = %d\n", n);
return 0;
}

/define/
int how_many_days_before_the_day(int month_input, int day_input){
int days_in_month[12];
int month;
for (month = 0; month < 12; ++month)
{
int month_plus;
month_plus = month + 1;
switch(month_plus){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days_in_month[month] = 31;
break;
case 4:
case 6:
case 9:
case 11:
days_in_month[month] = 30;
break;
case 2:
days_in_month[month] = 28;
break;
}
}
//every month now has its day-number, month[0-11].
int total_before_the_month = 0;
for (month = 0; month < (month_input - 1); ++month)
{
printf("Days in month %d = %d\n", month + 1, days_in_month[month]);
total_before_the_month = total_before_the_month + days_in_month[month];
}
int total;
total = total_before_the_month + day_input;
return total;
}

你可能感兴趣的:(C语言函数练习题)