/*
定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题),要求写一个函数days,实现上面的计算。由主函数将年月日传递给days函数,计算后将日子传递回主函数输出。
*/
#include <stdio.h>
int days (int y,int m,int d) {
struct getDays{
int year;
int month;
int day;
}date = {y,m,d};
int Jan= 31,Feb= 0,Mar= 31,Apr= 30,May= 31,Jun= 30,Jul= 31,Aug= 31,Sep= 30,Oct= 31,Nov= 30,Dec= 31,sum= 0;
if (y % 400 == 0 || (y % 4 == 0 && y %100 != 0)) {
Feb = 29;
} else {
Feb = 28;
}
int arr[12] = {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec};
for (int i= 1; i != date.month; i++) {
sum = sum + arr[i-1];
}
return sum = sum + date.day;
}
int main(int argc, const char * argv[]) {
// insert code here...
printf("该日期是该年的第%d天!\n",days(2015, 11, 11));
printf("The end!\n");
return 0;
}
|
|