标题:请教以下一行表达式的问题【摘自Teach yourself C 21 Days】
只看楼主
xzwcn
Rank: 1
等 级:新手上路
帖 子:31
专家分:3
注 册:2011-10-15
结帖率:100%
已结贴  问题点数:20 回复次数:1 
请教以下一行表达式的问题【摘自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.

中文:
搜索更多相关主题的帖子: yourself 表达式 
2012-03-13 01:05
lonmaor
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:郑州
等 级:版主
威 望:75
帖 子:2637
专家分:6423
注 册:2007-11-27
得分:20 
数据测试,当seconds=61的时候,对SECS_PER_HOUR取余是61,对SECS_PER_MINUTE取余是1。
然后考虑自己的逻辑错误。
收到的鲜花
  • xzwcn2012-03-13 12:59 送鲜花  3朵   附言:多谢提醒。

从不知道到知道,到知道自己不知道,成长的道路上脚步深深浅浅
2012-03-13 10:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-362963-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.639599 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved