被这个题搞自闭了
编写函数,输入年、月、日,输出当年剩余天数。在main中输入今天的日期,调用函数并输出今年已过的天数和剩余天数。


程序代码:void foo( unsigned y, unsigned m, unsigned d )
{
static const unsigned s[2][12] = { {0,31,59,90,120,151,181,212,243,273,304,334}
, {0,31,60,91,121,152,182,213,244,274,305,335} };
_Bool isleap = (y%400==0 || (y%4==0 && y%100!=0));
unsigned a = s[isleap][m-1] + d;
unsigned b = (isleap?366:365) - a;
printf( "%u, %u\n", a, b );
}
程序代码:int sumday(int y, int m, int d)
{
m--;
return m * 30 + d + (m > 1 ? -2 : 0) + (m > 7 ? m / 2 + 1 : (m + 1) / 2);
}
程序代码:
void theDiff(unsigned year, unsigned month, unsigned day) {
year -= 1900;
struct tm start = { 0 };
struct tm end = { 0 };
start.tm_year = year;
start.tm_mon = 1;
start.tm_mday = 1;
end.tm_year = year;
end.tm_mon = 12;
end.tm_mday = 31;
end.tm_hour = 23;
end.tm_min = 59;
end.tm_sec = 59;
time_t startT, currentT, endT;
startT = mktime(&start);
currentT = time(NULL);
endT = mktime(&end);
double diffPast = difftime(currentT ,startT);
double diffHas = difftime(endT , currentT);
printf("%f \t %f\n", diffPast / (24 * 3600), diffHas / (24 * 3600));
}
[此贴子已经被作者于2020-4-15 09:01编辑过]