请教以下一行表达式的问题【摘自Teach yourself C 21 Days】
程序代码:
/* Illustrates the modulus operator. */ /* Inputs a number of seconds, and converts to hours, */ /* minutes, and seconds. */ #include <stdio.h> /* Define constants */ #define SECS_PER_MIN 60 #define SECS_PER_HOUR 3600 unsigned seconds, minutes, hours, secs_left, mins_left; int main( void ) { /* Input the number of seconds */ printf("Enter number of seconds (< 65000): "); scanf("%d", &seconds); hours = seconds / SECS_PER_HOUR; minutes = seconds / SECS_PER_MIN; mins_left = minutes % SECS_PER_MIN; secs_left = seconds % SECS_PER_MIN; printf("%u seconds is equal to ", seconds); printf("%u h, %u m, and %u s\n", hours, mins_left, secs_left); return 0; }
请教第23行为什么不是“mins_left = minutes % SECS_PER_HOUR”呢?
理由:Because the total number of minutes figured in line 22 also contains minutes for the hours, line 23 uses the modulus operator to divide the hours and keep the remaining minutes. Line 24 carries out a similar calculation for determining the number of seconds that are left. Lines 26 and 27 are similar to what you have seen before. They take the values that have been calculated in the expressions and display them. Line 29 finishes the program by returning0 to the operating system before exiting.
中文: